#!/usr/bin/perl

use LWP::Simple;

my $DBG = 1;
my $URL = 'http://www.stileproject.com';
my $TOP = 'hfv.gif';
my $BOT = '<div align="center" class="forum">';
my $LNK = 'http://www.stilemedia.com';

my $OUTPUT_PATH = "./"; #remember the slash at the end of the path!

my $content = get ($URL);

if ($content =~ /$TOP(.*?)$BOT/si){
	$videoz_slice = $1;
	while ($videoz_slice =~ /"($LNK.*?)"/gsi){
		push @links, $1;
	}
}

foreach $link (@links){
	if ($link =~ /$LNK\/(.*)/){
		my $search = $1;
		print "Downloading page $link...\n" if $DBG;
		my $content = get ($link);
		print " Searching $search... " if $DBG;
		if ($content =~ /"(\Q$search\E.*?)"/i){
			print "Found!\n";
			my $link2 = "$LNK/$1";
			if ($link2 =~ /v=(.*?)\&/){
				my $search = $1;
				print " Downloading page $link2...\n" if $DBG;
				my $content = get ($link2);
				print "  Searching $search... " if $DBG;
				if ($content =~ /href="(.*?$search.*?)">Click/i){
					print "Found!\n" if $DBG;
					my $file_url = $1;
					print "  $file_url\n" if $DBG;
					dload ($file_url);
				}
			}
		}
	}
}

sub dload {
	my $url = shift;
	my $filename;
	if ($url =~ /([^\/]*?)$/){
		$filename = $1;
		print "Saving to $OUTPUT_PATH$filename... " if $DBG;
	}

	getstore ($url, $OUTPUT_PATH.$filename);
	print "Done.\n" if $DBG;
}
