Skip to content

Instantly share code, notes, and snippets.

@edbond
Created April 14, 2009 14:48
Show Gist options
  • Select an option

  • Save edbond/95216 to your computer and use it in GitHub Desktop.

Select an option

Save edbond/95216 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
#
# fetch new torrents from torrents.ru
# and display them as notifications
$KCODE='u'
require 'jcode'
require 'rubygems'
require 'net/http'
require 'hpricot'
require 'cgi'
require 'iconv'
require 'sqlite3'
def get_firefox_cookies
home=ENV['HOME']
profiles_home = File.join(home,".mozilla","firefox")
profiles = File.read( File.join(profiles_home,"profiles.ini") )
path = profiles.split(/\[.*\]/).find {|p| p.include?("Default=1")}.match(/Path=(.+)/)[1]
# get into db
db_file = File.join(profiles_home, path, "cookies.sqlite")
db = SQLite3::Database.new( db_file )
cookies = db.execute("SELECT name, value FROM moz_cookies WHERE host=?", ".torrents.ru")
end
def get_html
url = URI.parse('http://torrents.ru/forum/tracker.php')
http = Net::HTTP.new(url.host, url.port)
#http.set_debug_output $stderr
cookies = get_firefox_cookies.map {|kv| kv.join('=') }.join(';')
request = Net::HTTP::Get.new(url.request_uri,'Cookie' => cookies)
response = http.request(request)
response.body.strip
end
old=[]
i = Iconv.new "utf-8", "windows-1251"
loop do
doc = Hpricot(get_html)
categories = (doc/".f").map {|a| a.inner_text}
titles = (doc/".tLink b").map {|a| a.inner_text}
sizes = (doc/".dl-stub").map {|a| a.inner_text}
categories.zip(titles,sizes).each do |row|
summary = row[0]
body = "#{row[1]} [#{row[2]}]"
summary = i.iconv(summary)
body = i.iconv(body)
next if old.include?(body)
`notify-send -t 5000 '#{summary.gsub(/'/,"\\\\#{$1}")}' '#{body.gsub(/'/,"\\\\#{$1}")}'`
old << body
sleep 5
end
sleep 30
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment