#!/usr/bin/perl -Imodules

#########################################
##### Author:		Sebastian Enger / B.Sc
##### CopyRight:	Sebastian Enger
##### LastModified	16.07.2006
##### Function:		
##### Todo:			
########################################

# perl -MCPAN -e 'force install "XML::TreePP"'

system("clear");

use Data::Dumper;

use BitTorrent::BDecode;
use TorrentIndexer::HTTP::Get;
use TorrentIndexer::HTTP::Head;
use TorrentIndexer::LINK::Extractor;


# my $domain	= "http://www.zoozle.net/zoozle.php?q=test&s=bittorrent&l=de";
# my $domain  = "http://eselfilme.com/parents/home.htm";
# my $domain	= "http://fenopy.com/rss.xml?row=10&cat=5&type=2";
# my $domain	= "http://www.p2p-blog.de/feed/";
my $domain = "http://torrent.to/torrent/upload/serien/713/150713/1308-www.torrent.to...CSI.Miami.S04E08.Wunschmoerder.German.Dubbed.DVDRiP.WS.XviD-TvR.torrent";

###########
### objekte initialisieren
###########

my $HEAD	= TorrentIndexer::HTTP::Head->new();
my $GET		= TorrentIndexer::HTTP::Get->new();
my $LINK	= TorrentIndexer::LINK::Extractor->new();


########
#### Request senden
########

my $HeadHashResultRef	= $HEAD->Head($domain);
my $HeadStatusCode		= $HeadHashResultRef->{ 'STA' };
my $LinkHashRef;


if ( $HeadStatusCode == 1 ) {

	my $ContentType			= $HeadHashResultRef->{ 'TYP' };
	my $GetHashResultRef	= $GET->Get($domain);
	
	if ( $ContentType =~ /html/i ) {
		$LinkHashRef = $LINK->HtmlExtractor( $GetHashResultRef->{ 'OBJ' }, $GetHashResultRef->{ 'CNT' } );

	} elsif ( $ContentType =~ /xml/i ) {
		$LinkHashRef = $LINK->XmlExtractor( $GetHashResultRef->{ 'CNT' } );

	} elsif ( $ContentType =~ /plain/i ) { 
		$LinkHashRef = $LINK->TextExtractor( $GetHashResultRef->{ 'CNT' } );

	} elsif ( $ContentType =~ /bittorrent/i  ) {
		print "Bittorrent Link\n";
		print "LEN " . $HeadHashResultRef->{ 'LEN' } . "\n";
		print "TYP " . $HeadHashResultRef->{ 'TYP' } . "\n";
		print "MOD " . $HeadHashResultRef->{ 'MOD' } . "\n";
		print "COD '" . $HeadHashResultRef->{ 'COD' } . "'\n";
		print "STA " . $HeadHashResultRef->{ 'STA' } . "\n";
		my $TorrentContent = $GetHashResultRef->{ 'CNT' };
		# print \$TorrentContent;
		# use LWP::Simple;
		# my $doc = get($domain);
		# my $t = BitTorrent::BDecode::bdecode(\$TorrentContent);
	
		print Dumper($t);
	};

	if ( $LinkHashRef->{'COUNT'} >= 1) {

		foreach my $links ( @{$LinkHashRef->{'LINKS'}} ){
			chomp($links);
			print "$links\n";
		}; # foreach

	}; # if ( $LinkHashRef->{'COUNT'} >= 1) {

}; # if ( $HeadHashResultRef->{ 'STA' } == 1 ) {