#!/usr/bin/perl

use DBI;
use strict;
use LWP::Simple;


# http://www.kickasstorrents.com/dailydump.txt.gz
# http://www.kickasstorrents.com/hourlydump.txt.gz

my $DBHOST		= "localhost";
my $DBNAME		= "torrentrage";
my $DBUSER		= "root";	
my $DBPASS		= "iseeanape!23";	

my $status 		= getstore("http://www.kickasstorrents.com/hourlydump.txt.gz", "/tmp/hourlydump.txt.gz");
system("/bin/gunzip -f /tmp/hourlydump.txt.gz");

my $file		= "/tmp/hourlydump.txt";
my $date		= getDATE();
my $count		= 0;

open(R,"<$file");
	while(<R>){
		chomp;
		my $line = $_;
		$line =~ s/'//ig;
		$line =~ s/\'//ig;
		
		# 405891cb4a7dc21ffdfb682dfe9e34a50a51ea6a|Aliens In The Attic.avi|Movies|http://www.kickasstorrents.com/aliens-in-the-attic-avi-t4142859.html|http://torcache.com/torrent/405891CB4A7DC21FFDFB682DFE9E34A50A51EA6A.torrent
		my ($hash,$desc,$catg,$detail,$torrent) = split(/\|/ig, $line); 
		#print "$hash,$desc,$catg,$detail,$torrent\n";
		
		my $dbh		= DBI->connect("DBI:mysql:database=$DBNAME;host=$DBHOST", "$DBUSER", "$DBPASS", {'RaiseError' => 0});
		$dbh->do( 
			qq {
				INSERT DELAYED INTO `torrentrage` ( `DATE` ,`CATG`,`DESC`,`LINK`,`TORRENT`,`LEECHER`,`SEEDER`,`HASH` )
					VALUES (
						'$date' ,'$catg','$desc','$detail','$torrent','n/a','n/a','$hash' );
			});
		$dbh->disconnect();
		$count++;
	};
close R;

print "$count kickasstorrent.com entries added\n";
unlink $file;
exit(0);


sub getDATE(){

	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
	$year = $year+1900;
	$mon = $mon + 1;

	if ( length($mon) == 1 ) {
		$mon = "0". $mon;
	};
	if ( length($mday) == 1 ) {
		$mday = "0". $mday;
	};

	my $date = $year ."-". $mon ."-". $mday;

	return $date;

}; # GetDate()


