Who needs Haskell anyway.

I switched to rtorrent from Azureus a while back, there's no reason for a bittorrent client to take a half-gig of ram. Unfortunately, that meant I had to find a replacement for the RSS feed reader plugin that I had been using. There are a metric ass-ton of these, but they seem to fall into two categories: big, windows GUI apps for the masses, and tiny scripts written by hackers. The first is useless to me, and if I'm going to parse obscure hacker code, I'll damn well write one myself.

So I did.

Because I'm strange, I tried to write it as Haskell in Ruby, i.e. with purely side-effect-free code. I mostly succeeded. I would have liked to get rid of the multiple references to the shows array, but alas I lost interest, so it's not quite a proper one-liner.

It's pretty cool if I do say so myself. It grabs the enclosure's url for matching shows, only grabs any single match once, sorted by how many optional flags it matches (HDTV, 720p, etc), works on an arbitrary number of feeds, shows, and flags, and spits out a wget line at the end so you can pipe it to sh as a cron job.

Of course, this assumes you have a bittorrent client that can monitor a directory for new torrent additions, like rtorrent

Here's the code:

::#!/usr/bin/ruby require 'rubygems' require 'hpricot' require 'open-uri' require 'pp' br/> feeds = %w( http://tvrss.net/feed/unique/ ) #season=/(\d{1,2})x(\d{1,2})/ #Doesn't do anything yet br/> shows = [ /top gear/i, /chaser/i, /weeds/i, /top gear/i ].insert(0, nil) flags = [ /720p/i, /HDTV/ ] br/> save_location = "/home/skorgu/data/torrents" br/> feeds.map{|f| Hpricot(open(f)).search("item").collect{|i| {'title' => i.at("title").inner_text, 'link' => i.at("enclosure")['url'], 'description' => i.at("description").inner_text }}}.flatten.sort_by{|i| flags.select{|f| i['title'] =~ f}.length }.inject([]){|memo, i| memo[shows.index(shows.detect{|s| i['title'] =~ s}) || 0] = i memo }[1..-1].each{|i| if i then fname = "#{save_location}/#{i['title'].tr(" ", "_").tr("^a-zA-Z0-9-_.", "")}.torrent" puts "wget -O \"#{fname}\" #{i['link']} || rm -rf #{fname}" if !File.exist?(fname) end }