#!/usr/bin/perl
# 2011/07/19 Created TAS
use LWP::UserAgent;
sub waitkey
{
local $k;
chomp ($k = <STDIN>);
$k =~ s/\&//g;
$k =~ s/\[//g;
$k =~ s/\]//g;
$k =~ s/\\//g;
$k =~ s/\;//g;
#$k =~ s/\'//g;
$k =~ s/\`//g;
#$k =~ s/\(//g;
#$k =~ s/\)//g;
$k =~ s/\|//g;
$k =~ s/\>//g;
$k =~ s/\<//g;
$k =~ s/\$//g;
$k =~ s/\=//g;
return $k;
}
sub fileout
{
$OUTFILE=">TEMPfile";
open OUT, $OUTFILE or die "cannot open TEMPfile for writing\n";
print OUT "@_\n";
close OUT;
}
sub fileout2
{
$OUTFILE2=">TEMP2file";
open OUT2, $OUTFILE2 or die "cannot open TEMP2file for writing\n";
print OUT2 "@_\n";
close OUT2;
}
sub fileout3
{
$OUTFILE3=">>Processed";
open OUT3, $OUTFILE3 or die "cannot open Processed for writing\n";
print OUT3 "@_\n";
close OUT3;
}
sub fanartOUT
{
$OUTFILE=">$listitem2/fanart.jpg";
print "$OUTFILE\n";
open OUT, $OUTFILE or die "cannot open fanart.jpg for writing\n";
print OUT "@_\n";
close OUT;
print "Saved $listitem2/fanart.jpg\n";
fileout3 ("$listitem2");
}
sub GetSeriesInfo
{
$name = shift;
chomp ($name);
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");
# Create a request
my $req = HTTP::Request->new(POST => 'http://www.thetvdb.com/api/GetSeries.php');
$req->content_type('application/x-www-form-urlencoded');
$req->content("seriesname=$name");
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
return $res->content;
}
else {
return $res->status_line, "\n";
}
}
sub GetSeriesBanners
{
$ID = shift;
chomp ($ID);
## Just replace with your API key - register for free @ thetvdb.com to get one
$APIKEY = "EEEEEEEEEEEEEEEEEE";
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");
# Create a request
my $req = HTTP::Request->new(GET => "http://www.thetvdb.com/api/$APIKEY/series/$ID/banners.xml");
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
return $res->content;
}
else {
return $res->status_line, "\n";
}
}
sub GetThePicture
{
$Picture = shift;
chomp ($Picture);
#print "Got HERE -
http://www.thetvdb.com/banners$Picture\n";
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");
# Create a request
my $req = HTTP::Request->new(GET => "http://www.thetvdb.com/banners$Picture");
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
return $res->content;
}
else {
return $res->status_line, "\n";
}
}
system ("clear");
@list_of_directories = `find . -type d`;
shift(@list_of_directories);
print "Manual(m) or Auto(a) lookup? ";
$feedback = waitkey();
chomp ($feedback);
if ($feedback eq "m")
{
print "Enter Series Name: ";
$Name_of_Series = waitkey();
chomp ($Name_of_Series);
}
foreach $listitem (@list_of_directories)
{
@splititem = ();
@splititem = split ("/",$listitem);
$listitem2 = $splititem[1];
chomp ($listitem2);
if ($Name_of_Series ne "")
{
$listitem2 = $Name_of_Series;
}
print "Processing $listitem2\n";
$result = GetSeriesInfo ("$listitem2");
fileout ("$result");
# open the tempfile for reading
$INFILE="<TEMPfile";
open IN, $INFILE or die "cannot open TEMPfile for reading\n";
my @SeriesNames = ();
%IDNAMES = ( );
$count = 0;
foreach $line (<IN>)
{
if ($line =~ /seriesid/)
{
$SERIESID = $line;
@SERIESID1 = split(">",$SERIESID);
@SERIESID2 = split("<",$SERIESID1[1]);
$SERIESIDD = $SERIESID2[0];
$count++;
}
if ($line =~ /SeriesName/)
{
$SERIESNAME = $line;
@SERIESNAME1 = split(">", $SERIESNAME);
@SERIESNAME2 = split("<", $SERIESNAME1[1]);
$SERIESNAMEE = $SERIESNAME2[0];
$SERIESNAMEE =~ s/&/&/g;
push(@SeriesNames, "$SERIESNAMEE");
$IDNAMES{$SERIESNAMEE} = $SERIESIDD;
}
}
$Cnt = 0;
## uncomment if you want a choice
# foreach $item (@SeriesNames)
# {
# print "$Cnt - $item\n";
# $Cnt++;
# }
# print "Please choose your Series: ";
# $answer = waitkey();
$answer = 0; # hard coded first choice, comment this and uncomment above to get a choice
$seriesID = $IDNAMES{@SeriesNames[$answer]};
$result2 = GetSeriesBanners ("$seriesID");
fileout2 ("$result2");
# open the temp2file for reading
$INFILE2="<TEMP2file";
open IN2, $INFILE2 or die "cannot open TEMP2file for reading\n";
$check_for_HD = `grep 1920x1080 TEMP2file`;
chomp ($check_for_HD);
if ($check_for_HD eq "")
{
print "Series has no HD content\n";
}
else
{
my @
Lines = <IN2>;
$COUNTER = 1;
foreach $line2 (@lines)
{
if ($line2 =~ /1920x1080/)
{
$bannerhd = $lines[$COUNTER+5];
@bannerhd1 = split(">", $bannerhd);
@bannerhd2 = split("<", $bannerhd1[1]);
$bannername = $bannerhd2[0];
@bansplit = split("/", $bannername);
$bannerpath = "/$bansplit[1]/$bansplit[2]/$bansplit[3]";
$result3 = GetThePicture ("$bannerpath");
fanartOUT ("$result3");
goto keep_going;
}
$COUNTER++;
}
}
keep_going:;
if ($Name_of_Series ne "")
{
goto TheEnd;
}
}
TheEnd:;