I rewrote the TV Torrent downloading script in Python and Ruby and I must say for a simple script it doesn't really matter which language you use. Although I would think Ruby has an advantage in that it doesn't need an external module since RSS support is built into the core libraries.
Both scripts can be used like so in a cronjob:
20 * * * * /Users/abhi/bin/tvtorrent.py | xargs open
20 * * * * /Users/abhi/bin/tvtorrent.rb | xargs open
Here is the code for Python:
#!/usr/bin/env python
import feedparser
import urllib2
DownloadPath = '/Users/abhi/Downloads/Feeds/'
DownloadLog = '/Users/abhi/.tvdownloads'
FeedUrl = 'http://pipes.yahoo.com/pipes/GBPk1Pm82xGRSPpPJxOy0Q/run?_render=rss'
def is_downloaded(link):
return link in open(DownloadLog, 'r+').read()
def write_log(link):
open(DownloadLog, 'a+').write('%s\\n' % link)
def get_tvtorrents():
parser = feedparser.parse(FeedUrl)
for item in parser['items']:
if not is_downloaded(item['link']):
torrentfile = DownloadPath + item['title'].replace(' ', '_') + '.torrent'
torrentdata = urllib2.urlopen(item['link']).read()
open(torrentfile, 'wb').write(torrentdata)
write_log(item['link'])
print torrentfile
if __name__=='__main__':
get_tvtorrents()
Here is the code for Ruby:
#!/usr/bin/env ruby
require 'rss'
require 'open-uri'
DownloadPath = '/Users/abhi/Downloads/Feeds/'
DownloadLog = '/Users/abhi/.tvdownloads'
FeedUrl = 'http://pipes.yahoo.com/pipes/GBPk1Pm82xGRSPpPJxOy0Q/run?_render=rss'
def is_downloaded?(link)
open(DownloadLog, 'r+').read.to_s.include?(link)
end
def write_log(link)
open(DownloadLog, 'a+').write("#{link}\\n")
end
def get_tvtorrents
open(FeedUrl) do |rss|
result = RSS::Parser.parse(rss.read, false)
result.items.each do |item|
unless is_downloaded?(item.link)
torrentfile = DownloadPath + item.title.gsub(' ', '_') + '.torrent'
torrentdata = open(item.link).read()
open(torrentfile, 'w+').write(torrentdata)
write_log(item.link)
puts torrentfile
end
end
end
end
get_tvtorrents
As you can see not only are they both better scripts then my last tv torrent script, but they are about the same size and look pretty much the same. The only real reason that Ruby is longer is because of the end statements. So which language do I like? I like both languages and will continue to learn both languages each has its strengths and weaknesses and both are evolving and it is really hard to say based on such a short script anyways.
Comments [0]