Skip to content

Instantly share code, notes, and snippets.

@danshearmur
Created August 29, 2010 22:32
Show Gist options
  • Save danshearmur/556766 to your computer and use it in GitHub Desktop.
Save danshearmur/556766 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'json'
require 'cgi'
require 'find'
require 'net/http'
class Movie
def initialize(name, imdb_link, torrent_link=nil, year=nil, downloaded=nil)
@name = name
@imdb_link = imdb_link
@torrent_link = torrent_link
@year = year
@downloaded = downloaded
end
attr_reader :name, :imdb_link, :torrent_link, :year, :downloaded
attr_writer :year, :torrent_link, :downloaded
def get_year
imdb_data = Nokogiri::HTML(open(@imdb_link))
imdb_data.css("a[href*='/year/']").each do |link|
@year = link.content
end
end
def to_json(*a)
{
"title" => @name,
"info" => { "imdb_link" => @imdb_link, "torrent_link" => @torrent_link, "year" => @year, "downloaded" => @downloaded }
}.to_json(*a)
end
def find_torrent
if @torrent_link.nil?
if @year.nil?
self.get_year
end
btjunkie_url = "http://btjunkie.org/search?q=" + CGI.escape(@name) + "+" + @year
btjunkie_url = btjunkie_url.to_str.gsub("%E2%80%99","%27")
html_data = Nokogiri::HTML(open(btjunkie_url))
torrents = Array.new
html_data.css("a[href^='http://dl.btjunkie.org/torrent/']").each do |torrent|
rank = torrent.parent.next.at("td>div[style*='vote_up.png']>font")
if !rank.nil?
#must have five positive reports
if rank.content.to_i > 4
#easiest way to grab all the torrent details (pretty nasty)
detail_str = torrent.parent.parent.parent.parent.parent.content.to_str
details = detail_str.split
file_size = details[-4].to_i
seeders = details[-2].to_i
leechers = details[-1].to_i
if (650 < file_size) and (file_size < 1401) and (seeders > 1)
#stick them in an array, not really altogether sure why, cuz btjunkie sorts 'em pretty well ¬_¬
torrents.push [torrent["href"], file_size, seeders, leechers]
end
end
end
end
#possibly some crazy sorting, but for now quick and simplez
if torrents.length > 0
@torrent_link = torrents[0][0]
end
#puts torrents.length
end
end
def download_torrent(folder)
file_name = folder + @name.gsub(/[^[:alnum:]]/, '') + ".torrent"
if !@torrent_link.nil?
tor = @torrent_link.gsub('http:\/\/dl.btjunkie.org\/torrent\/', '')
Net::HTTP.start("dl.btjunkie.org") { |http|
resp = http.get(tor)
open(file_name, "wb") { |file|
file.write(resp.body)
}
}
@downloaded = true
end
end
end
class MovieList
def initialize
@movies = Array.new
@file = 'movies.js'
@dir = 'torrents/'
puts "\n\nOpening save file..."
self.open_from_file
print self.length
puts " Movies loaded OK","Getting new film list..."
self.parse_feed
puts "OK", "Finding torrent files..."
self.find_torrents
puts "OK","Downloading Torrents..."
self.grab_torrents
puts "All done."
self.write_to_file
end
def append(aMovie)
findit = @movies.find { |bMovie| bMovie.name == aMovie.name }
if findit.nil?
@movies.push(aMovie)
self
end
end
def [](key)
return @movies[key] if key.kind_of?(Integer)
return @movies.find { |aMovie| aMovie.imdb_link == key }
end
def parse_feed
feed_url = "http://torrentfreak.com/category/dvdrip/feed/"
xml_data = Nokogiri::HTML(open(feed_url))
xml_data.css("a[href^='http://www.imdb.com/']").each do |link|
#check regex for (TC), (CAM) or (TS)
if !link.parent.content.match("\(TS\)|\(TC\)|\(CAM\)")
cont = link.content
self.append Movie.new(cont, link.attribute("href"))
end
end
end
def length
@movies.length
end
def list
str = ""
@movies.each do |mov|
str += mov.name + " (" + mov.year + ") " + mov.imdb_link + "\n"
end
str
end
def find_torrents
@movies.each do |mov|
if mov.torrent_link.nil?
mov.find_torrent
self.write_to_file
end
end
end
def grab_torrents
@movies.each do |mov|
if mov.downloaded.nil?
mov.download_torrent(@dir)
end
end
end
def write_to_file
File.open(@file, 'w') { |f| f.write(JSON.pretty_generate(@movies)) }
end
def open_from_file
str =""
file = File.open(@file, 'r')
file.each_line { |line|
str += line
}
if str.length != 0
JSON.parse(str).each do |mov|
self.append Movie.new(mov["title"], mov["info"]["imdb_link"], mov["info"]["torrent_link"], mov["info"]["year"], mov["info"]["downloaded"])
end
end
end
end
list = MovieList.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment