kp_api 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,283 @@
1
+ module KpApi
2
+ class Agent
3
+
4
+ def status
5
+ @json[:resultCode].nil? ? true : false
6
+ end
7
+
8
+ def status2
9
+ @json[:resultCode].nil? ? true : false
10
+ end
11
+
12
+ def data
13
+ @json
14
+ end
15
+
16
+ def data2
17
+ @json2
18
+ end
19
+
20
+ ####
21
+ ####
22
+ ####
23
+
24
+ #For paginate
25
+ def next_page
26
+ if @page < @page_count
27
+ @page += 1
28
+ gen_url
29
+ @json = json
30
+ true
31
+ else
32
+ false
33
+ end
34
+ end
35
+
36
+ def page_count
37
+ @page_count
38
+ end
39
+
40
+ def current_page
41
+ @page
42
+ end
43
+
44
+ #private
45
+
46
+ def json(url=nil, bu=true)
47
+ if url.nil?
48
+ uri = URI(@url)
49
+ else
50
+ uri = URI(url)
51
+ end
52
+
53
+ path = uri.to_s.gsub(DOMAINS[:api],'')
54
+ time_stamp = Time.now.to_i.to_s
55
+ key = Digest::MD5.hexdigest(path + time_stamp + DOMAINS[:salt])
56
+ begin
57
+ print "[GET] -> " + uri.to_s
58
+ get_time = Time.now
59
+
60
+ http = Net::HTTP.new(uri.host, uri.port)
61
+ http.read_timeout = 10
62
+ if uri.scheme == "https"
63
+ http.use_ssl = true
64
+ end
65
+
66
+ headers = {
67
+ "Android-Api-Version" => "19",
68
+ "X-SIGNATURE" => key,
69
+ "device" => "android",
70
+ "X-TIMESTAMP" => time_stamp,
71
+ "User-Agent" => "Android client (4.4 / api19), ru.kinopoisk/4.2.0 (55)",
72
+ }
73
+
74
+ response = http.get(uri.request_uri, headers)
75
+
76
+ print " <- [#{(Time.now-get_time).round 3}s] [#{response.code}]\n"
77
+
78
+ if KpApi::valid_json?(response.body)
79
+ j = JSON.parse(response.body)
80
+ if j['resultCode'] == 0
81
+ j['data']
82
+ else
83
+ j
84
+ end
85
+ else
86
+
87
+ {:resultCode => -1, :message=> "Error method require", :data => { :code => response.code, :body => response.body} }
88
+ end
89
+ rescue StandardError => e
90
+ print "\n[Err] -> " + uri.to_s
91
+ raise KpApi::ApiError.new(0, e)
92
+ end
93
+
94
+ end
95
+
96
+ def json2
97
+ if @json2.nil?
98
+ @json2 = json(@url2)
99
+ end
100
+ end
101
+
102
+
103
+ def s2a(str)
104
+ if str.nil?
105
+ []
106
+ else
107
+ str.split(',').map { |i| i.strip }
108
+ end
109
+ end
110
+
111
+ def int_data(data, name, none=0, type=Integer)
112
+ s = dn(data, name)
113
+
114
+ if s.nil?
115
+ none
116
+ else
117
+ r = s
118
+ if r.class == String
119
+ r = s.gsub(/[\ \%\$]/i, '')
120
+ end
121
+
122
+ if type == Integer
123
+ r.to_i == 0 ? none : r.to_i
124
+ elsif type == Float
125
+ r.to_f == 0 ? none : r.to_f
126
+ else
127
+ r
128
+ end
129
+ end
130
+ end
131
+
132
+ def year_data(data, name, point=:start)
133
+ s = dn(data, name)
134
+
135
+ if s.nil?
136
+ nil
137
+ else
138
+ if s.size == 4 && point == :start
139
+ s.to_i
140
+ else s.size == 9
141
+ arr = s.scan(/(\d{4})/).flatten
142
+ if point == :start
143
+ arr[0].to_i
144
+ elsif point == :end
145
+ arr[1].to_i
146
+ end
147
+ end
148
+ end
149
+ end
150
+
151
+ def time_data(data, name)
152
+ s = dn(data, name)
153
+ if !s.nil?
154
+
155
+ if s.size == 10
156
+ d=Date.parse(s) rescue nil
157
+ if d.nil?
158
+ s=s.gsub('00.', '01.')
159
+ d=Date.parse(s) rescue nil
160
+ end
161
+ d
162
+ else
163
+ year = s.scan(/\d{4}/)[0]
164
+ if !year.nil?
165
+ Date.parse("01.01.#{year}") #only year???
166
+ end
167
+ end
168
+
169
+ end
170
+ end
171
+
172
+ def arr_data(data, name)
173
+ s = dn(data, name)
174
+ if s.nil?
175
+ []
176
+ else
177
+ s.split(',').map { |genre| genre.strip }
178
+ end
179
+ end
180
+
181
+ def str_data(data, name)
182
+ s = dn(data, name)
183
+
184
+ if s.class == String
185
+ s
186
+ elsif s.class == NilClass
187
+ nil
188
+ else
189
+ s.to_s
190
+ end
191
+ end
192
+
193
+ def bool_data(data, name)
194
+ s = dn(data, name)
195
+
196
+ if s.class == String
197
+ s = s.to_i
198
+ end
199
+
200
+ if s.class == TrueClass || s.class == FalseClass
201
+ s
202
+ else
203
+ !s.nil? && s == 1 ? true : false
204
+ end
205
+ end
206
+
207
+ def min_data(data, name)
208
+ s = dn(data, name)
209
+ begin
210
+ if s.nil?
211
+ 0
212
+ else
213
+ time = Time.parse(s)
214
+ (time.hour * 60) + time.min
215
+ end
216
+ rescue
217
+ end
218
+ end
219
+
220
+ def url_data(data, name, id, poster_name)
221
+ s = dn(data, name)
222
+ s.nil? ? nil : "#{DOMAINS[:kinopoisk][:poster][poster_name]}_#{id}.jpg"
223
+ end
224
+
225
+
226
+ def film_hash(h, id='filmID')
227
+ {
228
+ id: int_data(String, h[id ]),
229
+ kp_type: str_data(String, h['type' ]),
230
+ name_ru: str_data(String, h['nameRU' ]),
231
+ name_en: str_data(String, h['nameEN' ]),
232
+ slogan: str_data(String, h['slogan' ]),
233
+ description: str_data(String, h['description' ]),
234
+ poster_url: url_data(String, h['posterURL' ], h[id], :film),
235
+ year: year_data(String, h['year' ], :start),
236
+ year_end: year_data(String, h['year' ], :end),
237
+ reviews_count: int_data(String, h['reviewsCount']),
238
+ duration: min_data(String, h['filmLength' ]),
239
+ countries: arr_data(String, h['country' ]),
240
+ genres: arr_data(String, h['genre' ]),
241
+ video: h['videoURL'],
242
+ is_sequel_or_prequel: bool_data(String, h['hasSequelsAndPrequelsFilms']),
243
+ is_similar_films: bool_data(String, h['hasRelatedFilms' ]),
244
+ is_imax: bool_data(String, h['isIMAX' ]),
245
+ is_3d: bool_data(String, h['is3D' ]),
246
+ rating_mpaa: str_data(String, h['ratingMPAA' ]),
247
+ minimal_age: int_data(String, h['ratingAgeLimits' ])
248
+ }
249
+ end
250
+
251
+ def people_hash(h)
252
+ {
253
+ :id => int_data(String, h['id' ]),
254
+ :kp_type => str_data(String, h['type' ]),
255
+ :name_ru => str_data(String, h['nameRU' ]),
256
+ :name_en => str_data(String, h['nameEN' ]),
257
+ :poster_url => url_data(String, h['posterURL' ], @id, :name),
258
+ :sex => str_data(String, h['sex' ]),
259
+ :growth => int_data(String, h['growth' ]),
260
+ :birthday => time_data(String, h['birthday' ]),
261
+ :birthplace => str_data(String, h['sex' ]),
262
+ :has_awards => bool_data(String, h['has_awards']),
263
+ :profession => s2a(str_data(String, h['profession']))
264
+ }
265
+ end
266
+
267
+ def dn(data, name)
268
+ if data.nil?
269
+ r = @json[name]
270
+ elsif data == String
271
+ r = name
272
+ else
273
+ if @json[data].nil?
274
+ r = nil
275
+ else
276
+ r = @json[data][name]
277
+ end
278
+ end
279
+ r
280
+ end
281
+
282
+ end
283
+ end
@@ -0,0 +1,20 @@
1
+ module KpApi
2
+ class ApiError < StandardError
3
+
4
+ def initialize(message, data)
5
+ super(message)
6
+ @data = data
7
+ end
8
+ attr_reader :data
9
+
10
+ def code
11
+ @data[:code]
12
+ end
13
+
14
+ def body
15
+ @data[:body]
16
+ end
17
+
18
+
19
+ end
20
+ end
@@ -0,0 +1,52 @@
1
+ module KpApi
2
+ class Category < Agent
3
+ attr_accessor :url
4
+
5
+ def initialize(country_id=1)
6
+ @country_id = country_id
7
+ @url = "#{DOMAINS[:api]}#{METHODS[:navigator_filters ][:method]}?countryID=#{country_id}"
8
+ @url2 = "#{DOMAINS[:api]}#{METHODS[:get_all_cities_view][:method]}?countryID=#{country_id}"
9
+ @json = json
10
+
11
+ unless status
12
+ raise ApiError.new(@json[:message], @json[:data])
13
+ end
14
+ end
15
+
16
+ def genres
17
+ h(@json['genre'])
18
+ end
19
+
20
+ def countries
21
+ h(@json['country'])
22
+ end
23
+
24
+ def cities
25
+ json2
26
+
27
+ if @json2['countryName'].nil?
28
+ raise ApiError.new(@json2[:message], @json2[:data])
29
+ else
30
+ @json2['cityData'].map{|city|
31
+ {
32
+ id: int_data(String, city["cityID" ]),
33
+ name: str_data(String, city["cityName" ])
34
+ }
35
+ }
36
+ end
37
+ end
38
+
39
+ private
40
+ def h(j)
41
+ j.map do |item|
42
+ {
43
+ id: int_data(String, item["id" ]),
44
+ name: str_data(String, item["name" ]),
45
+ popular: bool_data(String, item["popular" ])
46
+ }
47
+ end
48
+ end
49
+
50
+ #private end
51
+ end
52
+ end
@@ -0,0 +1,103 @@
1
+ module KpApi
2
+ class Film < Agent
3
+ attr_accessor :id, :url
4
+
5
+ def initialize(id)
6
+ @id = id.to_i
7
+ @url = "#{DOMAINS[:api]}#{METHODS[:get_film][:method]}?#{METHODS[:get_film][:id]}=#{id}"
8
+ @url2 = "#{DOMAINS[:api]}#{METHODS[:get_staff][:method]}?#{METHODS[:get_staff][:id]}=#{id}"
9
+ @json = json
10
+
11
+ unless status
12
+ raise ApiError.new(@json[:message], @json[:data])
13
+ end
14
+
15
+ end
16
+
17
+ def view
18
+ film_hash(@json).merge(rating).merge(rent).merge(budget)
19
+ end
20
+
21
+ def rating
22
+ {
23
+ :rating_good_review => int_data('ratingData', 'ratingGoodReview', nil),
24
+ :rating_good_review_vote_count => int_data('ratingData', 'ratingGoodReviewVoteCount'),
25
+ :rating => int_data('ratingData', 'rating', nil, Float),
26
+ :rating_vote_count => int_data('ratingData', 'ratingVoteCount'),
27
+ :rating_await => int_data('ratingData', 'ratingAwait', nil),
28
+ :rating_await_count => int_data('ratingData', 'ratingAwaitCount'),
29
+ :rating_imdb => int_data('ratingData', 'ratingIMDb', nil, Float),
30
+ :rating_imdb_vote_count => int_data('ratingData', 'ratingIMDbVoteCount'),
31
+ :rating_film_critics => int_data('ratingData', 'ratingFilmCritics', nil),
32
+ :rating_film_critics_vote_count => int_data('ratingData', 'ratingFilmCriticsVoteCount'),
33
+ :rating_rf_critics => int_data('ratingData', 'ratingRFCritics', nil),
34
+ :rating_fr_critics_vote_count => int_data('ratingData', 'ratingRFCriticsVoteCount')
35
+ }
36
+ end
37
+
38
+ def rent
39
+ {
40
+ :distributors => str_data('rentData', 'Distributors'),
41
+ :premiere_world_country => str_data('rentData', 'premiereWorldCountry'),
42
+ :distributor_release => str_data('rentData', 'distributorRelease'),
43
+ :premiere_ru => time_data('rentData', 'premiereRU'),
44
+ :premiere_world => time_data('rentData', 'premiereWorld'),
45
+ :premiere_dvd => time_data('rentData', 'premiereDVD'),
46
+ :premiere_blu_ray => time_data('rentData', 'premiereBluRay')
47
+ }
48
+ end
49
+
50
+ def budget
51
+ {
52
+ :gross_ru => int_data('budgetData', 'grossRU', nil),
53
+ :gross_usa => int_data('budgetData', 'grossUSA', nil),
54
+ :gross_world => int_data('budgetData', 'grossWorld', nil),
55
+ :budget => int_data('budgetData', 'budget', nil)
56
+ }
57
+ end
58
+
59
+ def peoples
60
+ unless @json['creators'].nil?
61
+ @json['creators'].map { |items|
62
+ items.map do |item|
63
+ people_hash_old(item)
64
+ end
65
+ }.flatten
66
+ end
67
+ end
68
+
69
+ def peoples_full
70
+ json2
71
+ unless @json2['creators'].nil?
72
+ @json2['creators'].map { |items|
73
+ items.map do |item|
74
+ people_hash_old(item)
75
+ end
76
+ }.flatten
77
+ end
78
+ end
79
+
80
+ ##########################
81
+ ##########################
82
+ ##########################
83
+
84
+
85
+ private
86
+
87
+ def people_hash_old(item)
88
+ Hash[
89
+ [
90
+ [:id, item['id'].to_i ],
91
+ [:kp_type, item['type'] ],
92
+ [:poster_url, !item['posterURL'].nil? ? "#{DOMAINS[:kinopoisk][:poster][:name]}_#{item['id']}.jpg" : nil],
93
+ [:name_ru, item['nameRU'] ],
94
+ [:name_en, item['nameEN'] ],
95
+ [:description, item['description'] ],
96
+ [:profession_text, item['professionText']],
97
+ [:profession_key, item['professionKey'] ]
98
+ ]
99
+ ]
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,38 @@
1
+ module KpApi
2
+ class FilmSearch < Agent
3
+ attr_accessor :keyword, :url
4
+
5
+ def initialize(keyword)
6
+ @keyword = URI.encode(keyword)
7
+ @page = 1
8
+ gen_url
9
+ @json = json
10
+ @page_count = @json['pagesCount']
11
+ end
12
+
13
+ def found?
14
+ @page_count != 0
15
+ end
16
+
17
+ def films_count
18
+ @json['searchFilmsCountResult']
19
+ end
20
+
21
+ def view
22
+ @json['searchFilms'].map do |film|
23
+ film_hash(film, 'id')
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def gen_url
30
+ @url = [
31
+ "#{DOMAINS[:api]}#{METHODS[:search_film][:method]}",
32
+ "?#{METHODS[:search_film][:keyword]}=#{@keyword}",
33
+ "&#{METHODS[:search_film][:page]}=#{@page}"
34
+ ].join('')
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,57 @@
1
+ module KpApi
2
+ class GlobalSearch < Agent
3
+ attr_accessor :keyword, :url
4
+
5
+ def initialize(keyword)
6
+ @keyword = URI.encode(keyword)
7
+ @url = "#{DOMAINS[:api]}#{METHODS[:search_global][:method]}?#{METHODS[:search_global][:keyword]}=#{@keyword}"
8
+ @json = json
9
+
10
+ unless status
11
+ raise ApiError.new(@json[:message], @json[:data])
12
+ end
13
+ end
14
+
15
+ def found?
16
+ films_count != 0 || peoples_count != 0
17
+ end
18
+
19
+ def films_count
20
+ if @json['searchFilmsCountResult'].nil?
21
+ 0
22
+ else
23
+ @json['searchFilmsCountResult']
24
+ end
25
+ end
26
+
27
+ def peoples_count
28
+ if @json['searchPeoplesCountResult'].nil?
29
+ 0
30
+ else
31
+ @json['searchPeoplesCountResult']
32
+ end
33
+ end
34
+
35
+ def youmean
36
+ film_hash(@json['youmean'], 'id')
37
+ end
38
+
39
+ def films
40
+ unless @json['searchFilms'].nil?
41
+ @json['searchFilms'].map do |film|
42
+ film_hash(film, 'id')
43
+ end
44
+ end
45
+ end
46
+
47
+ def peoples
48
+ unless @json['searchPeople'].nil?
49
+ @json['searchPeople'].map do |name|
50
+ people_hash(name)
51
+ end
52
+ end
53
+ end
54
+
55
+
56
+ end
57
+ end
@@ -0,0 +1,64 @@
1
+ module KpApi
2
+ class People < Agent
3
+ attr_accessor :id, :url
4
+
5
+ def initialize(id)
6
+ @id = id.to_i
7
+ @url = "#{DOMAINS[:api]}#{METHODS[:get_people][:method]}?#{METHODS[:get_people][:id]}=#{id}"
8
+ @json = json
9
+
10
+ if !status || (@json['nameRU'] == "" && @json["nameEN"] == "" && @json["sex"] == nil)
11
+ raise ApiError.new(@json[:mesage], @json[:data])
12
+ end
13
+ end
14
+
15
+ def view
16
+ {
17
+ :id => @id,
18
+ :kp_type => str_data(nil, 'class'),
19
+ :name_ru => str_data(nil, 'nameRU'),
20
+ :name_en => str_data(nil, 'nameEN'),
21
+ :poster_url => url_data(nil, 'posterURL', @id, :name),
22
+ :sex => str_data(nil, 'sex'),
23
+ :growth => int_data(nil, 'growth'),
24
+ :birthday => time_data(nil, 'birthday'),
25
+ :birthplace => str_data(nil, 'birthplace'),
26
+ :has_awards => bool_data(nil, 'has_awards'),
27
+ :profession => s2a(str_data(nil, 'profession'))
28
+ }
29
+ end
30
+
31
+ def films
32
+ if filmography.nil?
33
+ []
34
+ else
35
+ filmography.map do |film|
36
+ {
37
+ :id => int_data(String, film['filmID' ]),
38
+ :rating => int_data(String, film['rating' ], nil, Float),
39
+ :rating_vote_count => int_data(String, film['ratingVoteCount']),
40
+ :description => str_data(String, film['description' ]),
41
+ :profession_text => str_data(String, film['professionText ']),
42
+ :profession_key => str_data(String, film['professionKey' ]),
43
+ :name_ru => str_data(String, film['nameRU' ]),
44
+ :name_en => str_data(String, film['nameEN' ]),
45
+ :year => int_data(String, film['year' ])
46
+ }
47
+ end
48
+ end
49
+ end
50
+
51
+ def film_ids
52
+ filmography.map {|film| int_data(String, film['filmID']) }
53
+ end
54
+
55
+ private
56
+
57
+ def filmography
58
+ if !@json['filmography'].nil?
59
+ @json['filmography'].flatten
60
+ end
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,38 @@
1
+ module KpApi
2
+ class PeopleSearch < Agent
3
+ attr_accessor :keyword, :url
4
+
5
+ def initialize(keyword)
6
+ @keyword = URI.encode(keyword)
7
+ @page = 1
8
+ gen_url
9
+ @json = json
10
+ @page_count = @json['pagesCount']
11
+ end
12
+
13
+ def found?
14
+ @page_count != 0
15
+ end
16
+
17
+ def peoples_count
18
+ @json['searchPeoplesCountResult']
19
+ end
20
+
21
+ def view
22
+ @json['searchPeople'].map do |film|
23
+ people_hash(film)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def gen_url
30
+ @url = [
31
+ "#{DOMAINS[:api]}#{METHODS[:search_people][:method]}",
32
+ "?#{METHODS[:search_people][:keyword]}=#{@keyword}",
33
+ "&#{METHODS[:search_people][:page]}=#{@page}"
34
+ ].join('')
35
+ end
36
+
37
+ end
38
+ end