#!/usr/bin/perl

#################
##### Author:	Sebastian Enger 
##### eMail:	sebastian.enger@gmail.com
##### Web:		http://www.sebastian-enger.com
##### Licence:	Its only mine!
#################

system("renice 20 $$");
system("clear");

# https://www.google.com/webmasters/sitemaps/addsitemap?siteUrl=http://www.zoozle.net/emule-bittorrent-download/&amp;hl=en&type=0&sitemapUrl0= PLUS SITEMAP URI

# first do a to install needed modules: perl -MCPAN -e 'force install "WWW::Google::SiteMap"'

use WWW::Google::SiteMap::Ping;
use URI::Escape qw(uri_escape);
use POSIX qw(strftime);
use IO::Handle;
use strict;
use DBI;
use Mysql;
use LWP::Simple;


my $drh	= DBI->install_driver("mysql");

my $DBHOST = "localhost";
my $DBNAME = "zoozle";
my $DBUSER = "zoozle";	
my $DBPASS = "zoozle23!99";

my $VERSION = "24.10.2006 - V.2";

my $StatementHandle;
my $SiteMapFile;
my $KeyCounts;
my @data = ();
my $SiteMap;
my $data;

my $CurrentDate			= getLastModTimeStamp();
my $StoreSiteMapPath	= '/server/wwwroot/emule-bittorrent-download';	# no tailing /
my $StoreSiteMapWebPath	= 'http://www.zoozle.org/emule-bittorrent-download';		# no tailing /


my %TABLES = (
	"emule7"			=> "sitemap_emule",
	"torrent7"			=> "sitemap_torrent",
	"serials7"			=> "sitemap_serials",
	"songtext7"			=> "sitemap_songtext",
);


#unlink "sitemapurls.html";

GenerateSiteMaps();
sub GenerateSiteMaps(){

	my $sitemapUp = 0;
	my $count = 0;

	while ( my ($key, $value) = each(%TABLES) ) {
        # print "$key => $value\n";
		
		$SiteMapFile	= "$StoreSiteMapPath/$value.$sitemapUp.xml";
		open(SITEMAP,">$SiteMapFile");
			binmode SITEMAP, ":utf8";
			flock(SITEMAP, 2);
		SITEMAP->autoflush(1);
		print SITEMAP PreXMLHeader();

		print "Generating: $SiteMapFile \n";

		#my $dbh = Mysql->connect($DBHOST, $DBNAME, $DBUSER, $DBPASS);
		my $dbh			= DBI->connect("DBI:mysql:database=$DBNAME:host=$DBHOST", "$DBUSER", "$DBPASS", {'RaiseError' => 0});
		# my $dbh	   = DBI->connect("DBI:mysql:database=$DBNAME;host=$DBHOST", "$DBUSER", "$DBPASS", {'RaiseError' => 0, AutoCommit => 0});
		# my $dbh	   = DBI->connect("DBI:mysql:database=$DBNAME;host=$DBHOST;socket=/var/lib/mysql/mysql.sock", "$DBUSER", "$DBPASS", {'RaiseError' => 0, AutoCommit => 0});

		######
		# hole immer die aktuellsten einträge zuerst
		# SELECT `DESC`,`DATE` FROM `torrent7` ORDER BY `DATE` DESC 
		######

		my $StatementHandle = $dbh->prepare("SELECT `DESC` FROM $key ORDER BY `DATE` DESC");
		$StatementHandle->execute();

		my $sth_count = $dbh->prepare("SELECT count(`DESC`) AS COUNT FROM $key");
		$sth_count->execute();
		my $KeyCounts = $sth_count->fetchrow;

		$key =~ s/(\d)//ig; # remove 7 from each entry
		$key =~ s/torrent_news/bittorrent/i;
		$key =~ s/emule_news/emule/i;
		$key =~ s/torrent/bittorrent/i;
		
		my $LinkCount = 0;
		my $lastmod	  = getLastModTimeStamp();

		while( @data = $StatementHandle->fetchrow_array()  ) {
			
			foreach my $data ( @data ) {
				
				# $data is the description of on entry
				# $key is the category

				$data =~ s/(\s)/\+/g;
				$data =~ s/\&(\w);//ig;
				# $data =~ s/\&//ig;
				$data = uri_escape($data);
				# print "$data\n";
				
				print SITEMAP "<url><loc>http://www.zoozle.org/emule-bittorrent-download/$data,$key,en,0.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";
				
				$LinkCount++;
				$KeyCounts--;

				if ( $LinkCount >= 20000 ) {
				
					print "$LinkCount Links Added\n";
					# close sitemap
					print SITEMAP SuffixXMLHeader();
					close SITEMAP;
					
					print "\tCompressing: $SiteMapFile.gz\n";

					# compressing file
					system("/usr/bin/gzip -f -b 9 $SiteMapFile");
					system("/bin/chmod 755 $SiteMapFile.gz");
					PingGoogleForSitemap("$StoreSiteMapWebPath/$value.$sitemapUp.xml.gz");
															
					# start new sitemap
					$sitemapUp++;
					
					$SiteMapFile	= "$StoreSiteMapPath/$value.$sitemapUp.xml";
					print "Generating: $SiteMapFile \n";

					open(SITEMAP,">$SiteMapFile");
						binmode SITEMAP, ":utf8";
						flock(SITEMAP, 2);
					SITEMAP->autoflush(1);
					print SITEMAP PreXMLHeader();

					print "Generating: $SiteMapFile \n";
					print SITEMAP "<url><loc>http://www.zoozle.org/emule-bittorrent-download/$data,$key,en,0.html</loc><lastmod>$lastmod</lastmod><changefreq>daily</changefreq><priority>0.90</priority></url>\n";
			
					$LinkCount = 0;

				} elsif ( $KeyCounts <= 0 ) {  # if ( $LinkCount >= 49998 ) {
		
					# close sitemap
					print SITEMAP SuffixXMLHeader();
					close SITEMAP;
					
					print "\tCompressing: $SiteMapFile.gz\n";
					# compressing file
					system("/usr/bin/gzip -f -b 9 $SiteMapFile");
					system("/bin/chmod 755 $SiteMapFile.gz");
					PingGoogleForSitemap("$StoreSiteMapWebPath/$value.$sitemapUp.xml.gz");
					
					$LinkCount = 0;
		
				}; # } elsif ( $KeyCounts <= 0 ) { 

			}; # foreach my $data ( @data ) {
			
			@data = ();

		}; # while( $sth->fetch() ) {

		$StatementHandle->finish();
		eval {
			$dbh->finish();
			$dbh->disconnect();
		};
		$sitemapUp = 0;

    }; # while ( my ($key, $value) = each(%TABLES) ) {

}; # sub GenerateSiteMaps(){


sub PingGoogleForSitemap(){

	my $SiteMapUrl = shift;	

	open(WH,">>sitemapurls.html");
		print WH "<a href=\"https://www.google.com/webmasters/sitemaps/addsitemap?siteUrl=http://www.zoozle.org/emule-bittorrent-download/&amp;hl=en&type=0&sitemapUrl0=$SiteMapUrl\">$SiteMapUrl</a><br><br>\n";
	close WH;
  
	my $ping = WWW::Google::SiteMap::Ping->new(
		"$SiteMapUrl",
	);

	$ping->submit;
	# print "\tThese pings succeeded:\n";
	foreach($ping->success) {
		print "\tSUCESS: $_ \n" if ( $_ =~ /\w/ );
	}

	# print "\tThese pings failed:\n";
	foreach($ping->failure) {
		print "\t\tFAILURE $_\n" if ( $_ =~ /\w/ );
	}

	my $yahoo		= get("http://www.yahoo.com/ping?sitemap=$SiteMapUrl");
	my $msn			= get("http://search.msn.com/ping?sitemap=$SiteMapUrl");
	my $msn1		= get("http://search.live.com/ping?sitemap=$SiteMapUrl");
	my $yahoo1		= get("https://siteexplorer.search.yahoo.com/submit/ping?sitemap=$SiteMapUrl");
	   
	return 1;

}; # sub PingGoogleForSitemap

sub PreXMLHeader(){

	my $returnString = <<"END";
<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.4" -->
<!-- sitemap-generator-url="http://www.zoozle.net/" sitemap-generator-version="3.0b1" -->
<!-- generated-on="19. October 2006 20:21" -->
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">

END
	
	return $returnString;

}; # sub PreXMLHeader(){


sub SuffixXMLHeader(){

	return "</urlset>";

}; # sub SuffixXMLHeader(){


sub getLastModTimeStamp(){

	my $now	= time();

	# We need to munge the timezone indicator to add a colon between the hour and minute part
	my $tz = strftime("%z", localtime($now));
	$tz =~ s/(\d{2})(\d{2})/$1:$2/;

	# ISO8601
	return  strftime("%Y-%m-%dT%H:%M:%S", localtime($now)) . "+00:00\n";
	
	# simple version, because WWW::Google::SiteMap ads tail automatically
	# return  strftime("%Y-%m-%d", localtime($now));

}; # sub getLastModTimeStamp(){


exit;