-
Notifications
You must be signed in to change notification settings - Fork 0
/
imdb.rb
32 lines (27 loc) · 1.1 KB
/
imdb.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require 'open-uri'
require 'nokogiri'
IMDB_GENRES = 'action','adventure','animation','biography','comedy','crime','documentary','drama','family','fantasy','film_noir','history','horror','music','musical','mystery','romance','sci_fi','short','sport','thriller','war','western'
class Imdb
def self.random_film_title
case (1..100).to_a.sample
when 1..81
doc = Nokogiri::HTML(open(top_charts_page))
return doc.css('table.chart tbody td.titleColumn > a').to_a.map(&:text).sample
when 82..99
doc = Nokogiri::HTML(open(random_genre_page))
return doc.css('table.results:first tr.detailed td.title > a').to_a.map(&:text).sample
when 100
doc = Nokogiri::HTML(open(recent_charts_page))
return doc.css('#boxoffice table.chart tbody td.titleColumn > a').to_a.map(&:text).sample
end
end
def self.random_genre_page
"http://www.imdb.com/search/title?genres=#{ IMDB_GENRES.sample }&title_type=feature&sort=moviemeter,asc"
end
def self.top_charts_page
"http://www.imdb.com/chart/top"
end
def self.recent_charts_page
"http://www.imdb.com/chart/"
end
end