Class: TopMoviesOf::Scraper

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

Instance Method Summary collapse

Constructor Details

#initializeScraper

Returns a new instance of Scraper.



7
8
# File 'lib/Top_Movies_Of/scraper.rb', line 7

def initialize
end

Instance Method Details

#add_attributes(input) ⇒ Object

adds score and summary for a single movie that is chosen



67
68
69
70
71
72
73
74
# File 'lib/Top_Movies_Of/scraper.rb', line 67

def add_attributes(input) #adds score and summary for a single movie that is chosen
  movie = TopMoviesOf::Movie.find_movie(input)
  summary = get_movie_summary(movie.name)
  score = get_movie_score(movie.name)
  movie.summary = summary
  movie.score = score
  return movie
end

#get_movie_score(name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/Top_Movies_Of/scraper.rb', line 31

def get_movie_score(name)
  if get_single_movie_page(name) != nil
    page = get_single_movie_page(name)
    @score = page.css("#all-critics-numbers .superPageFontColor").first.text
    return @score
  else
    puts "#{name} #{page}"
    puts "Sorry, I cannot pull up information about this movie"
    return nil
  end
end

#get_movie_summary(name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/Top_Movies_Of/scraper.rb', line 43

def get_movie_summary(name)
  if get_single_movie_page(name) != nil
    page = get_single_movie_page(name)
    @summary = page.css("#movieSynopsis").text.lstrip
    return @summary
  else
    puts "#{name} #{page}"
    puts "Sorry, I cannot pull up information about this movie"
    return nil
  end
end

#get_movie_titleObject

gets an array of top movie titles



14
15
16
17
18
19
20
21
# File 'lib/Top_Movies_Of/scraper.rb', line 14

def get_movie_title #gets an array of top movie titles
  get_title = get_page(@year).css("td .articleLink")
  array = []
  get_title.each do |title|
    array << title.text.lstrip
  end
  return array
end

#get_page(year) ⇒ Object



10
11
12
# File 'lib/Top_Movies_Of/scraper.rb', line 10

def get_page(year)
  @doc = Nokogiri::HTML(open("https://www.rottentomatoes.com/top/bestofrt/?year=#{year}"))
end

#get_single_movie_page(name) ⇒ Object

scrape specific movie page



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

def get_single_movie_page(name) #scrape specific movie page
  if url=get_page(@year).search('td').text_includes("#{name}").first
    attributes = url.search("a")
    url_name = attributes.first.values.first
    Nokogiri::HTML(open("https://www.rottentomatoes.com#{url_name}"))
  end
end

#make_movies(year) ⇒ Object

makes movie objects from array of movie titles



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/Top_Movies_Of/scraper.rb', line 55

def make_movies(year) #makes movie objects from array of movie titles
  @year = year
  x = 1
  while x <= get_movie_title.length
    get_movie_title[0..49].each do |movie|
      format_movie = movie.split(" (")
      new_mov = TopMoviesOf::Movie.new(ranking = x,name = format_movie[0])
      x+=1
    end
  end
end