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 +4 -4
- data/kinopoisk_parser.gemspec +1 -1
- data/lib/kinopoisk/movie.rb +3 -13
- data/lib/kinopoisk_parser.rb +14 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7efdb357ef8aa519896bf12cbfc14a1d6fcfd4d8
|
4
|
+
data.tar.gz: 98f354d07b2872ff351200e0d84610c2344e0f59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: caf299d7f7acd611513debfeff6cd01c00014ee2daf5598f43c88f55c9010cf7476570eeceefbac4f6ac7e18b71c0c4587fab26bdd814c9aa44e8e6d3aa771f9
|
7
|
+
data.tar.gz: da0584635f218d625d386522532b0317eaf6b4fcf3bf8a303eb1c9a09c7711d608da4b2a2bfebf8c46c2e12cbc7fbc5c363b589c3de1d421261a2a8e872ca915
|
data/kinopoisk_parser.gemspec
CHANGED
data/lib/kinopoisk/movie.rb
CHANGED
@@ -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) ?
|
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
|
-
|
180
|
-
|
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)
|
data/lib/kinopoisk_parser.rb
CHANGED
@@ -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,
|
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
|
22
|
+
# Returns a nokogiri document or throws an error
|
19
23
|
def self.parse(url)
|
20
24
|
page = fetch url
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|