All pastes #730009 Raw Edit

Braindead

public text v1 · immutable
#730009 ·published 2007-10-08 20:01 UTC
rendered paste body
#!/usr/bin/perl
#
# rss-torrent.pl 0.1 listens to RSS-Feeds, tries to match to Regular
# Expressions, downloads the torrents to a 'watch' directory (the one
# managed by your favorite torrent client).
# Copyright (C) 2007, Gianluca Trimarchi <gtrimarchi@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

###################### Documentation ###################################### 
#
# Mostyl based on the work of Sebastian Sproesser
# <sebastian@sproesser.org> for rss-torrentflux.pl 0.9c
#
# The following Perl-Modules need to be installed:
# * Getopt::Long (perl-base)
# * LWP::Simple (libwww-perl)
# * XML::RSS (libxml-rss-perl)

use warnings;
use strict;
use vars qw/@rssfeeds @regexp @seentorrents $seentorrentsfile $watchdirectory/;

#################### Options you really should set :) ####################

# Provide RSS-Feeds to grab Torrents from:
#@rssfeeds = ('http://rssfeed1.example/','http://nextfeed.example');
@rssfeeds = ('http://tvrss.net/feed/eztv/');

# Provide Perl-Regular-Expressions to match the wanted Titles. Case is ignored:
#@regexp = ('simpsons.*(pdtv|hdtv)','^24');
@regexp = ('^Lost.*HDTV','^Battlestar.*ORENJi','^Rome.*NoTV','Top Gear.*FoV','^Heroes.*HDTV');

# The file containing the name of already downloaded .torrent
#$seentorrentsfile = $ENV{HOME}.'/.seentorrents'
$seentorrentsfile = '/home/ryo/torrent/.seentorrents';

# The directory configured in your torrent client as 'watch directory' 
$watchdirectory = '/home/ryo/torrent/.watch/';

############# You shouldn't need to edit anything below here #############

use Getopt::Long;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use XML::RSS;

my ($verbose, $help);

die "rss-torrent.pl version 0.1, Copyright (C) 2007 Gianluca Trimarchi
rss-torrent.pl comes with ABSOLUTELY NO WARRANTY; 
This is free software, and you are welcome to redistribute it under certain conditions.
\nUsage: $0 [-v] [-h]
\t-v\tBe verbose
\t-h\tDisplay this help screen"
    unless GetOptions('v|verbose' => \$verbose,'h|help' => \$help,) and !$help;

my $rss = new XML::RSS;
my $browser = LWP::UserAgent->new();
$browser->timeout(20);

# Get list of downloaded torrents
if (open(TORRENTS, '<', $seentorrentsfile)) {
    push(@seentorrents,$_) while(<TORRENTS>);
    chomp(@seentorrents);
    close(TORRENTS);
}

 FEED: for my $feed (@rssfeeds) {
     my $feedcontent = get($feed);
     if (!$feedcontent) {
         warn "Could not get RSS-Feed $feed";
         next FEED;
     }
     $rss->parse($feedcontent);

   ITEM: foreach my $item (@{$rss->{'items'}}) {
       my $title = $item->{'title'};
       my $url = $item->{'link'};
       # Keep only the basename
       my $filename = $url;
       $filename =~ s!^.*/!!;
       for (@regexp) {
           if ($title =~ /$_/i) {
               # Check if download has already started
               for (@seentorrents) {
                   if ($filename eq $_) {
                        print "Skipping ".$title." because already downloaded\n" if ($verbose);
                        next ITEM;
                   }
               }
               print "Found new Match: $title\n" if ($verbose);
               # Download .torrent
               my $response = $browser->request(HTTP::Request->new(GET => $url)) or die "Unable to download ".$url;
               if ($response->is_success) {
                       open(NEWTORRENT, '>', $watchdirectory.$filename)
                                or die 'Could not write to '.$watchdirectory.$filename;
                       print NEWTORRENT $response->content;
                       close(NEWTORRENT);
                       # Remember last downloaded torrent file
                       push(@seentorrents, $filename);
                       print " Successfully downloaded torrent from torrent: ".$url."\n" if ($verbose);
              }
           }
       }
   }
 }

# Update ~/.seentorrents
open(TORRENTS, '>',$seentorrentsfile) or die 'Could not write to '.$seentorrentsfile;
print TORRENTS join("\n",@seentorrents);
close(TORRENTS);