-
-
Save georgebashi/558933 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'json' | |
require 'cgi' | |
require 'find' | |
require 'net/http' | |
require 'progressbar' | |
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 = [] | |
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) && (file_size < 1401) && (seeders > 1) | |
#stick them in an array, not really altogether sure why, cuz btjunkie sorts 'em pretty well ¬_¬ | |
torrents << [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 | |
File.open(file_name, 'wb') { |f| f.write Net::HTTP.get(URI.parse tor) } | |
@downloaded = true | |
end | |
end | |
end | |
class MovieList | |
def initialize | |
@movies = [] | |
@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 <<(aMovie) | |
findit = @movies.find { |bMovie| bMovie.name == aMovie.name } | |
if findit.nil? | |
@movies << 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 << 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 | |
pb = ProgressBar.new("finding", @movies.length) | |
@movies.each do |mov| | |
if mov.torrent_link.nil? | |
mov.find_torrent | |
self.write_to_file | |
end | |
pb.inc | |
end | |
pb.finish | |
end | |
def grab_torrents | |
pb = ProgressBar.new("grabbing", @movies.length) | |
@movies.each do |mov| | |
if mov.downloaded.nil? | |
mov.download_torrent(@dir) | |
end | |
pb.inc | |
end | |
pb.finish | |
end | |
def write_to_file | |
File.open(@file, 'w') { |f| f.write(JSON.pretty_generate(@movies)) } | |
end | |
def open_from_file | |
JSON.parse(File.read(@file)).each do |mov| | |
self << Movie.new(mov["title"], mov["info"]["imdb_link"], mov["info"]["torrent_link"], mov["info"]["year"], mov["info"]["downloaded"]) | |
end | |
end | |
end | |
list = MovieList.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment