#!/usr/bin/perl -Imodules

use IO::Handle;

# SELECT * FROM `torrent7` WHERE `LINK` LIKE ('http://fenopy.com%')

my $CatLanguage = "de";
my $StartUrl = "http://torrent.to/";

my (undef,undef,$Hostname,undef) = split('/', $StartUrl);

my $CachePath				= "/root/zoozle/indexer/torrentindexer/cache";
my $TorrentStoreFlatFile	= "$CachePath/$Hostname-torrent.txt";
my $LinkScannedFile			= "$CachePath/$Hostname-alreadyscannedlinks.txt"; 
my $LinksToScan				= "$CachePath/$Hostname-linksToScan.txt"; 

my $TorrentPath			= "/root/zoozle/indexer/torrentindexer/torrentgrabber/torrents";		# later check if dir exits if not create, siehe client.pm easyspider
my $FinalTorrentFile	= "$TorrentPath/$Hostname-finaltorrents.txt";
mkdir $TorrentPath;


open(TORRENT,">$FinalTorrentFile") or die;
	TORRENT->autoflush(1);
	TORRENT->blocking(0);
	binmode TORRENT, ":utf8";
	flock(TORRENT, 2);

open(RH,"<$LinksToScan") or die;
	while(<RH>){
		my ( $LinkDesc, $ParentLink, $LinkName ) = split(' ### ', $_);
		
		# http://torrent.to/torrent/upload/moviez/539/154539/555-www.torrent.to...DasLebendesBrian.torrent
		# if ( $LinkName =~ /http:\/\/torrent\.to\/torrent\/upload\//i && $LinkName =~ /(\w)+\.torrent/i ) {
		# my $Reverse = reverse($LinkName);
		# if ( $Reverse =~ /^tnerrot\./i ) {
		
		if ( $LinkName =~ /http:\/\/torrent\.to\/torrent\/upload\//i && $LinkName =~ /(\w)+\.torrent/i ) {
			
			my $Raw;

			my ( undef, $Raw) = split('\.\.\.', $LinkName);
			$Raw =~ s/\.torrent//gi;
			
			$Raw =~ s/\d+-+\d+-//ig;
			$Raw =~ s/^\.//og; $Raw =~ s/^\.//og; $Raw =~ s/^\.//og; $Raw =~ s/^\.//og; $Raw =~ s/^\.//og;

			my @FileName = split('', $Raw);
			my $FileNameCount = 0;
			my $output;

			foreach $Entry ( @FileName ) {
				
				# matche ob es sich um a-z || A-Z || 0-9 || ' ' || äöü -> ae|oe|ue
				if ( ( ord($Entry) >= 97 && ord($Entry) <= 122 ) && ( ord(@FileName[$FileNameCount+1]) >= 65 && ord(@FileName[$FileNameCount+1]) <= 90 ) ) {	# Klein zu Großbuchstaben
					$output .= "$Entry ";
				#} elsif ( ( ord(@FileName[$FileNameCount+1]) >= 97 && ord(@FileName[$FileNameCount+1]) <= 122 ) && ( ord($Entry) >= 65 && ord($Entry) <= 90 ) ) { # Grossbuchstaben zu Kleinbuchstaben
				#	$output .= "$Entry ";
				} else {
					$output .= $Entry;
				};
				$FileNameCount++;

			};# foreach

			my $DESC = &delteSpecialChars($output);
			chomp($DESC); chomp($LinkName);
			print TORRENT "CATE=$CatLanguage#DESC=$DESC#LINK=$ParentLink\n";
			$count++;

		}; # if ( $LinkName =~ /http:\/\/torrent\.to\/torrent\/upload\//i && $LinkName =~ /(\w)+\.torrent/i ) {
	

		if ( $LinkName =~ /http:\/\/torrent\.to\/torrent\/popup\.php/i && $LinkName !~ /addcomment/i ) {
			#	print "LinkDesc: $LinkDesc\n";
			#	print "Linkname: $LinkName\n";
			#	next;

			if ( $LinkDesc =~ /\[img\]/i) {
			} else {

				$LinkDesc =~ s/- *tip* -//ig;
				$LinkDesc =~ s/(\d)\/(\d)//g;
				$LinkDesc =~ s/ - / /g;
				$LinkDesc =~ s/^\s/ /g;
				
				$LinkDesc = reverse($LinkDesc);
				
				chop($LinkDesc);
				chop($LinkDesc);
				chop($LinkDesc);
				chop($LinkDesc);
				chop($LinkDesc);
				chop($LinkDesc);
				chop($LinkDesc);
				chop($LinkDesc);
				chop($LinkDesc);
				chop($LinkDesc);
				chop($LinkDesc);

				$LinkDesc = reverse($LinkDesc);

				my $DESC = &delteSpecialChars($LinkDesc);
				chomp($DESC); chomp($LinkName);
				$DESC =~ s/#\s//;
				print TORRENT "CATE=$CatLanguage#DESC=$DESC#LINK=$LinkName\n";
				$count++;

			};

		}; # if ( $LinkName =~ /http:\/\/fenopy.com\/torrent\//i ) {

	}; # while(<RH>){

close RH;
close TORRENT;

open(RH,"<$FinalTorrentFile") or die;
@links = <RH>;
@links = grep { ! $seen{$_} ++ } @links;
close RH;

open(WH,">$FinalTorrentFile") or die;
	foreach my $link (@links ) {
		chomp($link);
		print WH "$link\n";
	};
close WH;

print "$count Torrents\n";
print "$#links Torrents\n";


sub delteSpecialChars(){

	my $input	= shift;
		$input =~ s/\./ /g;
		$input =~ s/-/ /g;
		$input =~ s/\+/ /g;
		$input =~ s/_/ /g;
	return $input;

}; # sub delteSpecialChars(){



