Class: WhatsOnNetflix::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/whats_on_netflix/scraper.rb

Class Method Summary collapse

Class Method Details

.get_html(url) ⇒ Object



8
9
10
# File 'lib/whats_on_netflix/scraper.rb', line 8

def self.get_html(url)
  Nokogiri::HTML(open(url))
end

.scrape_imdb_info(name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/whats_on_netflix/scraper.rb', line 26

def self.scrape_imdb_info(name)

  search_page = Nokogiri::HTML(open("http://www.imdb.com/find?s=tt&q=" + URI.escape(name)))

  movie_page = Nokogiri::HTML(open("http://www.imdb.com" + "#{search_page.css("td a").attribute("href").value}"))
  
  info = {}
  
  info[:plot] = movie_page.css("div.summary_text").text.strip
  info[:genre] = ""
  info[:stars] = ""
  info[:year] = ""
  
  # getting info[:genre] into a readable format
  
  movie_page.css('span[itemprop="genre"]').each do |genre|
      info[:genre].concat("| #{genre.text} |")
  end
  
  # getting info[:stars] into a readable format
  
  movie_page.css('span[itemprop="actors"]').each do |actor|
    info[:stars].concat("#{actor.text.strip} ")
  end
  
  # getting info[:year] - TV show and movie pages are formatted a little differently
  
  if movie_page.css('a[title="See more release dates"]').text.include?("TV Series")
    info[:year] = movie_page.css('a[title="See more release dates"]').text
  else
    info[:year] = movie_page.css('span#titleYear').text.strip.gsub("(", "").gsub(")", "")
  end
  
  info
end

.scrape_title_list(url) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/whats_on_netflix/scraper.rb', line 12

def self.scrape_title_list(url)
  html = self.get_html(url)
  
  titles = []
  
  html.css("h4 + ul li").each do |title|
      titles << title.text
  end
  
  titles
end