Class: TopMoviesOf::Scraper
- Inherits:
-
Object
- Object
- TopMoviesOf::Scraper
- Defined in:
- lib/Top_Movies_Of/scraper.rb
Instance Method Summary collapse
-
#add_attributes(input) ⇒ Object
adds score and summary for a single movie that is chosen.
- #get_movie_score(name) ⇒ Object
- #get_movie_summary(name) ⇒ Object
-
#get_movie_title ⇒ Object
gets an array of top movie titles.
- #get_page(year) ⇒ Object
-
#get_single_movie_page(name) ⇒ Object
scrape specific movie page.
-
#initialize ⇒ Scraper
constructor
A new instance of Scraper.
-
#make_movies(year) ⇒ Object
makes movie objects from array of movie titles.
Constructor Details
#initialize ⇒ Scraper
Returns a new instance of Scraper.
4 5 |
# File 'lib/Top_Movies_Of/scraper.rb', line 4 def initialize end |
Instance Method Details
#add_attributes(input) ⇒ Object
adds score and summary for a single movie that is chosen
64 65 66 67 68 69 70 71 |
# File 'lib/Top_Movies_Of/scraper.rb', line 64 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
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/Top_Movies_Of/scraper.rb', line 28 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
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/Top_Movies_Of/scraper.rb', line 40 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_title ⇒ Object
gets an array of top movie titles
11 12 13 14 15 16 17 18 |
# File 'lib/Top_Movies_Of/scraper.rb', line 11 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
7 8 9 |
# File 'lib/Top_Movies_Of/scraper.rb', line 7 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
20 21 22 23 24 25 26 |
# File 'lib/Top_Movies_Of/scraper.rb', line 20 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
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/Top_Movies_Of/scraper.rb', line 52 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.each do |movie| format_movie = movie.split(" (") new_mov = TopMoviesOf::Movie.new(ranking = x,name = format_movie[0]) x+=1 end end end |