class Scraper

Public Class Methods

new() click to toggle source
# File lib/scraper.rb, line 8
def initialize
  @input = gets.downcase.chomp.strip

  if @input["https://www.imdb.com/list/"]
    @input = @input
  else
    puts "false input"
    print "Input: "
    initialize
  end

  @movie_check = []

end

Public Instance Methods

ask_for_pages() click to toggle source
# File lib/scraper.rb, line 23
def ask_for_pages
  @pages = gets.to_i
end
choose_random() click to toggle source
# File lib/scraper.rb, line 51
def choose_random
  @index = rand(@movies_hash[:year].length)

  if @movie_check.include?(@movies_hash[:title][@index]) == true
    choose_random
  else
    @movie_check << @movies_hash[:title][@index]
    puts @movies_hash[:title][@index].to_s + @movies_hash[:year][@index].to_s + " | " + @movies_hash[:runtime][@index].to_s + " runtime | " + @movies_hash[:genre][@index].to_s + " | Metascore: " + @movies_hash[:rating][@index].to_s
  end
end
get_description() click to toggle source
# File lib/scraper.rb, line 62
def get_description
  input = gets.downcase.chomp.strip


  
  if input["y"]
    puts ""
    @movie_descriptions[:description][@index].chars.each do |letter|
      print letter
      sleep(0.03)
    end
    puts ""
  end
end
get_page() click to toggle source
# File lib/scraper.rb, line 27
def get_page
  link = @input.to_s + "?sort=list_order,asc&st_dt=&mode=detail&page=" + rand(1..@pages).to_s
  page = Nokogiri::HTML(open(link.to_s))

  page
end
page_to_movies_hash() click to toggle source
# File lib/scraper.rb, line 35
def page_to_movies_hash
  use_page = get_page
  movie_title = use_page.css("h3").css("a").collect {|title| title.text}
  movie_year_raw = use_page.css("h3").css("span").collect {|year| year.text}
  movie_year = movie_year_raw.reject {|v| movie_year_raw.index(v).even?}
  movie_runtime = use_page.css("p").css("span").css(".runtime").collect {|runtime| runtime.text}
  movie_genres = use_page.css("p").css("span").css(".genre").collect {|genre| genre.text.strip}
  movie_rating = use_page.css("div").css("span").css(".metascore").css(".favorable").collect {|rating| rating.text.strip}
  movie_bio = use_page.css("div").css(".lister-item-content").css("p[class='']").collect {|bio| bio.text.strip}
  
  @movies_hash = {:title => movie_title, :year => movie_year, :runtime => movie_runtime, :genre => movie_genres, :rating => movie_rating}
  

  @movie_descriptions = {:description => movie_bio}
end