Perl Sitemap Generator

I think everyone has the problem to make a sitemap if you have more than 1M videos …this script counts the packets (40k) in line 19 and then make the packets to generate a sitemap.

#!/usr/bin/perl
use strict;
use warnings;
use DBI;

# MYSQL CONFIG VARIABLES
my $host = "127.0.0.1";
my $database = "db";
my $tablename = "table";
my $user = "root";
my $pw = "pwd1234";


my $dbh = DBI->connect('DBI:mysql:'.$database , $user, $pw
	           ) || die "Could not connect to database: $DBI::errstr";


my $sitemap_counter = 0;
my $th = $dbh->prepare('SELECT round(count(*)/40000+0.5) FROM `video` WHERE `bw` <=80');
$th->execute();
while (my @row = $th->fetchrow_array()) {
   $sitemap_counter = $row[0];
}



for my $SMP (0..$sitemap_counter){
	print "build midd".$SMP.".xml ...".$/;
	open (DATEI, "> /var/www/MAP/midd".$SMP.".xml") or die $!;
	print DATEI '<?xml version="1.0" encoding="UTF-8"?>'.$/;
	print DATEI '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.$/;
	$th = $dbh->prepare('SELECT CONCAT( "<url><loc>http://www.example.com/view.php?ID=", `id` , "</loc><priority>0.8</priority></url>" ) FROM `video` WHERE `bw` <=80 ORDER BY `id` LIMIT '.(40000*$SMP).' , 40000;');
	$th->execute();
	while (my @row = $th->fetchrow_array()) {
	   print DATEI $row[0].$/;
	}
	print DATEI '</urlset>';
	close (DATEI);
	print "[DONE]".$/;
}


$dbh->disconnect;
exit;

Leave a Reply

Your email address will not be published.

nineteen − three =