class XbmcSql::Top250Updater
Constants
- ID_PARSER
Attributes
missing[RW]
rows[RW]
Public Class Methods
go!()
click to toggle source
# File lib/xbmc_sql/top_250_updater.rb, line 4 def self.go! self.new.tap do |updater| updater.scrape updater.update_current updater.remove_old updater.note_missing end true rescue => e puts e false end
new()
click to toggle source
# File lib/xbmc_sql/top_250_updater.rb, line 19 def initialize @rows = {} @missing = {} end
Public Instance Methods
note_missing()
click to toggle source
# File lib/xbmc_sql/top_250_updater.rb, line 62 def note_missing missing.each do |position, values| puts "YOU ARE MISSING ##{position}: #{values[:title]}" end end
remove_old()
click to toggle source
# File lib/xbmc_sql/top_250_updater.rb, line 51 def remove_old puts "----------------------- Removing old. -----------------------------" XbmcSql::Movie.in_top_250.each do |movie| if movie.imdb_250 != simple_list[movie.imdb_id] puts "Removing #{movie.title} - previously was ##{movie.imdb_250}, now is not in the list." movie.update_attributes! imdb_250: nil end end puts "------------------------------------------------------------------" end
scrape()
click to toggle source
# File lib/xbmc_sql/top_250_updater.rb, line 26 def scrape table.each do |row| position = row.at_xpath('td[2]/span[1]/text()').to_s.gsub('.', '') title = row.at_xpath('td[2]/a/text()').to_s tt = ID_PARSER.match(row.at_xpath('td[2]/a/@href').to_s)[1] rows[position] = {title: title, imdb_id: tt } end end
update_current()
click to toggle source
# File lib/xbmc_sql/top_250_updater.rb, line 36 def update_current puts "----------------------- updating current... -----------------------------" rows.each do |position, values| movies = XbmcSql::Movie.with_imdb_id values[:imdb_id] if movies.present? movies.each { |movie| update_movie! movie, position } else puts "##{position}: #{values[:title]} IS MISSING" missing[position] = values end end end
Private Instance Methods
page()
click to toggle source
# File lib/xbmc_sql/top_250_updater.rb, line 88 def page @page ||= begin response = HTTParty.get('http://www.imdb.com/chart/top') Nokogiri::HTML response.body end end
simple_list()
click to toggle source
# File lib/xbmc_sql/top_250_updater.rb, line 95 def simple_list rows.inject({}) do |result, (position, values)| result[values[:imdb_id]] = position result end end
table()
click to toggle source
# File lib/xbmc_sql/top_250_updater.rb, line 84 def table @table ||= page.xpath('//table/tbody[@class="lister-list"]/tr') end
update_movie!(movie, position)
click to toggle source
# File lib/xbmc_sql/top_250_updater.rb, line 70 def update_movie!(movie, position) if movie.imdb_250.blank? puts "##{position}: #{movie.title} entered the list" elsif movie.imdb_250 == position puts "##{position}: #{movie.title} stayed at ##{movie.imdb_250}" elsif movie.imdb_250.to_i < position.to_i puts "##{position}: #{movie.title} raised up from ##{movie.imdb_250}" movie.update_attributes! imdb_250: position else puts "##{position}: #{movie.title} fell from ##{movie.imdb_250}" movie.update_attributes! imdb_250: position end end