Class: WorldTopMovies::Scraper

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

Constant Summary collapse

@@genres =
[
  "Action",
  "Adventure",
  "Animation",
  "Biography",
  "Comedy",
  "Crime",
  "Drama",
  "Family",
  "Fantasy",
  "History",
  "Horror",
  "Mystery",
  "Romance",
  "Sci-Fi",
  "Sport",
  "Thriller",
  "War",
]

Class Method Summary collapse

Class Method Details

.genresObject



22
23
24
# File 'lib/world_top_movies/scraper.rb', line 22

def self.genres
  @@genres
end

.get_movie_details_page(movie_url) ⇒ Object



47
48
49
50
# File 'lib/world_top_movies/scraper.rb', line 47

def self.get_movie_details_page(movie_url)
  response = HTTParty.get(movie_url)
  Nokogiri::HTML(response.body)
end

.get_movies(genre) ⇒ Object



33
34
35
36
# File 'lib/world_top_movies/scraper.rb', line 33

def self.get_movies(genre)
  # Returns an XML object of all the movies by given genre
  self.get_top_movies_page(genre).css("div.lister-item.mode-advanced")
end

.get_top_movies_page(genre) ⇒ Object



26
27
28
29
30
31
# File 'lib/world_top_movies/scraper.rb', line 26

def self.get_top_movies_page(genre)
  #Get response from get request (raw HTML)
  response = HTTParty.get("https://www.imdb.com/search/title/?title_type=feature&num_votes=200000,&genres=#{genre}&sort=user_rating,desc&view=advanced")
  #Parse response
  Nokogiri::HTML(response.body)
end

.make_movies(genre = nil) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/world_top_movies/scraper.rb', line 38

def self.make_movies(genre = nil)
  # Iterates over all movies from the XML object and creates new instances
  self.get_movies(genre).each do |m|
    movie = WorldTopMovies::Movie.new_from_page(m)
    # Include genre to the genre property if for some reason the website doesn't include it
    genre && !movie.genres.include?(genre) && movie.genres << genre
  end
end