Module: Kinopoisk

Defined in:
lib/kinopoisk/movie.rb,
lib/kinopoisk/person.rb,
lib/kinopoisk/search.rb,
lib/kinopoisk_parser.rb

Defined Under Namespace

Classes: Movie, Person, Search

Constant Summary collapse

SEARCH_URL =
'https://www.kinopoisk.ru/index.php?kp_query='
NotFound =
Class.new StandardError
Denied =
Class.new StandardError do
  def initialize(msg = 'Request denied')
    super
  end
end

Class Method Summary collapse

Class Method Details

.fetch(url) ⇒ Object

Headers are needed to mimic proper request so kinopoisk won’t block it



18
19
20
# File 'lib/kinopoisk_parser.rb', line 18

def self.fetch(url)
  HTTPClient.new.get url, follow_redirect: true, header: { 'User-Agent'=>'a', 'Accept-Encoding'=>'a' }
end

.parse(url) ⇒ Object

Returns a nokogiri document or throws an error



23
24
25
26
27
28
29
30
31
32
# File 'lib/kinopoisk_parser.rb', line 23

def self.parse(url)
  page = fetch url

  raise NotFound, 'Page not found' if page.status != 200
  raise Denied if page.header.request_uri.to_s =~ /error\.kinopoisk\.ru|\/showcaptcha/

  Nokogiri::HTML page.body.encode('utf-8')
rescue HTTPClient::BadResponseError
  raise Denied
end