Skip to content

Instantly share code, notes, and snippets.

@ahawkins
Created July 7, 2010 21:20
Show Gist options
  • Save ahawkins/467299 to your computer and use it in GitHub Desktop.
Save ahawkins/467299 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rss/2.0'
require 'open-uri'
require 'uri'
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'downloaded.sqlite3'
begin
ActiveRecord::Schema.define :version => 1 do
create_table "downloads" do |t|
t.string "title"
t.string "link"
t.datetime "created_at"
t.datetime "updated_at"
end
end
rescue
# nothing, table already exists
end
class Download < ActiveRecord::Base
end
## SETUP
# Feed is a hash where the key is the url of the rss feed, and the value is an array of regular expressions
# Example:
#
# FEEDS = {
# 'http://my.feed/torrent.rss' => [/house/i, /breaking.*bad/i]
# }
#
# Then configure DOWNLOAD_LOCATION using an absolute path. torrent files will be saved in this directory. Set up your torrent
# client to watch this directory so when something is added it will auto start. Transmission can do this
FEEDS = { }
DOWNLOAD_LOCATION = "/Users/adam/Downloads"
def matched_items
items = []
FEEDS.each_key do |feed|
open(feed) do |http|
rss = RSS::Parser.parse(http.read, false)
items += rss.items.select do |item|
flag = FEEDS[feed].inject(false) do |flag,regex|
puts "Checking #{item.title} aganist #{regex}"
flag || item.title =~ regex
end
flag && !Download.exists?(["title = ? AND link = ?", item.title, item.link])
end
end
end
items
end
def download_items(items)
items.each do |item|
puts "Downloding #{item.link}...."
%x(cd "#{DOWNLOAD_LOCATION}" ; wget "#{item.link}")
Download.create :title => item.title, :link => item.link
end
end
download_items(matched_items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment