Automatically download torrent files from a RSS feed

I was looking for a way to parse a RSS feed I built using yahoo pipes and add new torrents to Transmission to download them automatically.
I couldn’t find anything useful, so I just wrote a python script to do just that.
If you want to use it, you’ll need to configure the first lines of this file to suit your needs.
The script is pretty self explanatory.

#!/usr/bin/python2.6
#
# Python script to parse a RSS feed containing torrent files urls and
# add new torrent files to transmission based on the time of the last added
# torrent. The script stores the RSS item date of the last added
# torrent and adds a new torrent to the BT client if the RSS contains
# one or more items with a later date
#
# Sat Jan 23 21:30:00 WET 2010
#

DATEFILE = "rsstorrents.pid"
RSSFILE = "http://pipes.yahoo.com/pipes/......" # Change RSSFILE to point to your RSS file URL
TORRENTCOMMAND = "transmission-remote -n username:password -a "

import feedparser
import pickle
from datetime import datetime
from datetime import timedelta
import time
import commands

# Read the date, download shows from last 3 weeks if we can't read any date
try:
    with open(DATEFILE, "rb") as f:
        lastdate = pickle.load(f)
        print "Read date %s" % lastdate
except Exception:
    lastdate = datetime.now() - timedelta(weeks=3)
    print "Could not read date of last feed, using last 3 weeks %s" % lastdate

# Fetch RSS File
feedInfo = feedparser.parse(RSSFILE)

# Fetch all items until date is later than the stored date
# Add all files to deluge
n = 0
for entry in feedInfo.entries:
    feedDate = datetime.fromtimestamp(time.mktime(entry.modified_parsed))

    if feedDate > lastdate:
        torrentURL = entry.enclosures[0]['href']
        print "Adding torrent %s" % entry.title

        outputstatus  = commands.getstatusoutput(TORRENTCOMMAND + torrentURL)
        if(outputstatus[0] != 0):
            print "Error adding torrent: %s" % outputstatus[1]
        else:
            n = n + 1
    else:
        break

# Set the last date to the date of the most recent item of the RSS feed
lastdate = datetime.fromtimestamp(time.mktime(feedInfo.entries[0].modified_parsed))

try:
    with open(DATEFILE, "wb") as f:
        pickle.dump(lastdate, f)
        print "Saved date %s" % lastdate
except Exception:
    print "Could not save date of last feed"

# Feedback user
print "Finished. Added %d torrents" % n

Disclaimer:
I use this script to download legal torrents ;)

Comments 2

  1. DurĂ£o wrote:

    there are illegal torrents on the net? ;)

    Posted 27 Jan 2010 at 11:31 pm
  2. admin wrote:

    Not that I am aware of ;)

    Posted 28 Jan 2010 at 2:11 am

Post a Comment

Your email is never published nor shared. Required fields are marked *