Class: Top100Movies::Scraper

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.scrape_details(movie) ⇒ Object



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
61
62
# File 'lib/top_100_movies/scraper.rb', line 30

def self.scrape_details(movie)
  doc = Nokogiri::HTML(open("#{movie.url}"))

  movie.synopsis = doc.search("div#movieSynopsis").first.text.strip

  labels = doc.search(".meta-label").map{|i| i.text.strip}
  values = doc.search(".meta-value").map{|i| i.text.strip}

  labels.each_index do |i|
    case labels[i]
    when "Rating:"
      movie.rating = values[i]
    when "Genre:"
      genres = values[i].split(", \n")
      genres.map!{|genre| genre.gsub(/\s{2,}/,'')}
      movie.genres = genres
    when "Directed By:"
      movie.directors = values[i]
    when "Written By:"
      movie.writers = values[i]
    when "In Theaters:"
      movie.release_date = values[i].gsub("\n","")[0..11]
    when "On Disc/Streaming"
      movie.disc_release_date = values[i]
    when "Box Office:"
      movie.box_office = values[i]
    when "Runtime:"
      movie.runtime = values[i]
    when "Studio:"
      movie.studio = values[i]
    end
  end
end

.scrape_url(index) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/top_100_movies/scraper.rb', line 22

def self.scrape_url(index)
  url = "https://www.rottentomatoes.com"
  index.search("a").each do |link|
    url += link['href']
  end
  url
end

Instance Method Details

#generate_moviesObject



16
17
18
19
20
# File 'lib/top_100_movies/scraper.rb', line 16

def generate_movies
  scrape_movies.each do |cell|
    Top100Movies::Movie.new_from_index(cell)
  end
end

#get_pageObject



3
4
5
# File 'lib/top_100_movies/scraper.rb', line 3

def get_page
  Nokogiri::HTML(open("https://www.rottentomatoes.com/top/bestofrt/"))
end

#scrape_moviesObject



7
8
9
10
11
12
13
14
# File 'lib/top_100_movies/scraper.rb', line 7

def scrape_movies
  table = self.get_page.at('.table')
  cells = []
  table.search('tr').each do |tr|
    cells << tr.search('th, td')
  end
  cells
end