#!/usr/bin/perl
# Automatically download TV shows I like.
# Usage: perl nzb.pl
# Requires: LWP, and XML::RSS::Parser, both available in cpan.
# Configuration:
# - set @showids to the show_id's of the shows you like on tvnzb.com.
# You can find the show_id's by going to the list of shows and finding
# it's id from the URL. It will be the numeric part of tvnzb.com/shows/72
# - Set the $folder and $cache variables to suit your file system.
use XML::RSS::Parser;
use LWP;
use strict;
# show_id from tvnzb, as shown on show's page
my @showids = qw/366 393 406 394 29 336 460 91 388 30 294 164 51 379 34 144 136 39 43 322 302 205 103 299 386 123 45 464 459 155 455/;
#my $folder = "/home/peterzal/windoze/downloads/tvnzb/"; # store NZB files here
my $folder = "/home/peterzal/bens/hella/nzb/daemon.queue/";
my $cache = "/home/peterzal/bens/tvnzb/cache"; # store cache file here
my @matches;
my $debug = 0;
my $p = XML::RSS::Parser->new;
my $feed = $p->parse_uri("http://www.tvnzb.com/tvnzb_new.rss");
foreach my $item ($feed->query('//item') )
{
my $title = ($item->query('description'))->text_content;
my $link = ($item->query('link'))->text_content;
my $season = ($item->query('season'))->text_content;
my $ep = ($item->query('episode'))->text_content;
my $showid = ($item->query('show_id'))->text_content;
my $rel = ($item->query('title'))->text_content;
$title =~ s/(?:^\s+|\s{2,})//g;
$season =~ s/\s+//g;
$ep =~ s/\s+//g;
$link =~ s/\s+//g;
$showid =~ s/\s+//g;
$rel =~ s/\s+//g;
#next if $title =~ /^#/;
next if $rel =~ /HR/;
next if $rel =~ /720p/;
if (grep {$_ eq $showid} @showids)
{
#next if grep {$_->{showid} eq $showid} @matches;
next if grep {$_->{showid} eq $showid } @matches;
push @matches, { showid => $showid, url => $link, ep => $ep, season => $season, rel => $rel };
print "MATCH: $title [${season}x$ep]: $rel : $link\n" if $debug;
}
}
# load up the cache file if it exists, otherwise create it.
my @cache = ( );
if(open(CACHE, "+<$cache"))
{
while(<CACHE>)
{
chomp;
push @cache, $_;
}
}
else
{
print "Creating cache file.\n" if $debug;
open(CACHE, ">$cache");
}
foreach my $match (@matches)
{
my $line = "$match->{showid}:$match->{ep}:$match->{season}";
# skip anything that is cached
next if ( grep {$_ eq $line} @cache );
my $ua = LWP::UserAgent->new;
my $nzb = $ua->get($match->{url});
if($nzb->is_success)
{
if(!open(FD, ">$folder/$match->{rel}.nzb"))
{
die "CRITICAL: Can not open output file.\n";
}
print FD $nzb->content;
print "Downloaded $match->{rel}\n";
close FD;
print CACHE "$line\n";
}
else
{
print "ERROR: Could not download requested file: $match->{url}\n";
}
}