kinopoisk_parser 2.2.2 → 2.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a512062e75b11a59968e53d182e4348e2065243e
4
- data.tar.gz: 9a6e745858ac1ebaa2898bd5eee752ccfd2f106f
3
+ metadata.gz: 7efdb357ef8aa519896bf12cbfc14a1d6fcfd4d8
4
+ data.tar.gz: 98f354d07b2872ff351200e0d84610c2344e0f59
5
5
  SHA512:
6
- metadata.gz: 6f85074b1cb0cd4273f06b1ce66748bf4c5c13d9ddacbfafbee37b1d250e7c085371192ccffe6e0ca65616b28b41e6a52c2cf8b4d817a3d83f449bdc369c1831
7
- data.tar.gz: 526a04d09ee9e39a1828ea6dcde5cad3afbb4bbdcf5ab5c8e6b635d75f9bb8c5e0505d17a9b858eec93588a21b28e6cfc446b2a05a2c46869e4a423438057729
6
+ metadata.gz: caf299d7f7acd611513debfeff6cd01c00014ee2daf5598f43c88f55c9010cf7476570eeceefbac4f6ac7e18b71c0c4587fab26bdd814c9aa44e8e6d3aa771f9
7
+ data.tar.gz: da0584635f218d625d386522532b0317eaf6b4fcf3bf8a303eb1c9a09c7711d608da4b2a2bfebf8c46c2e12cbc7fbc5c363b589c3de1d421261a2a8e872ca915
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'kinopoisk_parser'
3
- gem.version = '2.2.2'
3
+ gem.version = '2.2.3'
4
4
  gem.authors = ['RavWar']
5
5
  gem.email = ['rav_war@mail.ru']
6
6
  gem.homepage = 'https://github.com/RavWar/kinopoisk_parser'
@@ -14,7 +14,7 @@ module Kinopoisk
14
14
  # Movie page request is made once and on the first access to a remote data.
15
15
  #
16
16
  def initialize(input, title=nil)
17
- @id = input.is_a?(String) ? find_by_title(input) : input
17
+ @id = input.is_a?(String) ? find_id_by_title(input) : input
18
18
  @url = "https://www.kinopoisk.ru/film/#{id}/"
19
19
  @title = title
20
20
  end
@@ -176,18 +176,8 @@ module Kinopoisk
176
176
  @doc ||= Kinopoisk.parse url
177
177
  end
178
178
 
179
- # Kinopoisk has defined first=yes param to redirect to first result
180
- # Return its id from location header
181
- def find_by_title(title)
182
- url = SEARCH_URL + "#{URI.escape(title)}&first=yes"
183
- location = Kinopoisk.fetch(url).headers['Location'].to_s
184
-
185
- if location =~ /error\.kinopoisk\.ru|showcaptcha/
186
- raise Denied, 'Request denied'
187
- else
188
- id_match = location.match(/\/(\d*)\/$/)
189
- id_match[1] if id_match
190
- end
179
+ def find_id_by_title(title)
180
+ Search.new(title).movies.first&.id || raise(NotFound)
191
181
  end
192
182
 
193
183
  def search_by_itemprop(name)
@@ -8,20 +8,26 @@ module Kinopoisk
8
8
  SEARCH_URL = 'https://www.kinopoisk.ru/index.php?kp_query='
9
9
 
10
10
  NotFound = Class.new StandardError
11
- Denied = Class.new StandardError
11
+ Denied = Class.new StandardError do
12
+ def initialize(msg = 'Request denied')
13
+ super
14
+ end
15
+ end
12
16
 
13
17
  # Headers are needed to mimic proper request so kinopoisk won't block it
14
18
  def self.fetch(url)
15
- HTTPClient.new.get url, nil, { follow_redirect: true, 'User-Agent'=>'a', 'Accept-Encoding'=>'a' }
19
+ HTTPClient.new.get url, follow_redirect: true, header: { 'User-Agent'=>'a', 'Accept-Encoding'=>'a' }
16
20
  end
17
21
 
18
- # Returns a nokogiri document or an error if fetch response status is not 200
22
+ # Returns a nokogiri document or throws an error
19
23
  def self.parse(url)
20
24
  page = fetch url
21
- if page.status == 200
22
- Nokogiri::HTML(page.body.encode('utf-8'))
23
- else
24
- raise NotFound, 'Page not found'
25
- end
25
+
26
+ raise NotFound, 'Page not found' if page.status != 200
27
+ raise Denied if page.header.request_uri.to_s =~ /error\.kinopoisk\.ru|\/showcaptcha/
28
+
29
+ Nokogiri::HTML page.body.encode('utf-8')
30
+ rescue HTTPClient::BadResponseError
31
+ raise Denied
26
32
  end
27
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kinopoisk_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.2
4
+ version: 2.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - RavWar