KinopoiskAPI 0.8.2 → 0.9.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.
@@ -1,15 +1,23 @@
1
1
  require 'net/http'
2
+ require 'KinopoiskAPI/api_error'
2
3
  require 'KinopoiskAPI/agent'
3
4
  require 'KinopoiskAPI/film'
4
- require 'KinopoiskAPI/staff'
5
- require 'KinopoiskAPI/gallery'
6
- require 'KinopoiskAPI/similar'
7
- require 'KinopoiskAPI/genres'
8
- require 'KinopoiskAPI/reviews'
5
+ require 'KinopoiskAPI/people'
6
+ require 'KinopoiskAPI/category'
7
+ require 'KinopoiskAPI/today'
8
+ require 'KinopoiskAPI/top'
9
9
  require 'KinopoiskAPI/global_search'
10
- require 'KinopoiskAPI/today_films'
10
+ require 'KinopoiskAPI/film_search'
11
+ require 'KinopoiskAPI/people_search'
12
+
13
+
14
+ #require 'KinopoiskAPI/reviews'
15
+ #require 'KinopoiskAPI/gallery'
16
+ #require 'KinopoiskAPI/similar'
17
+
11
18
  require 'KinopoiskAPI/version'
12
19
 
20
+
13
21
  module KinopoiskAPI
14
22
  DOMAINS = {
15
23
  api: 'https://ext.kinopoisk.ru/ios/3.11.0',
@@ -31,8 +39,12 @@ module KinopoiskAPI
31
39
 
32
40
  METHODS = {
33
41
  get_film: {
34
- method: 'getKPFilmDetailView',
35
- id: 'filmID'
42
+ method: 'getKPFilmDetailView',
43
+ id: 'filmID'
44
+ },
45
+ get_staff: {
46
+ method: 'getStaffList',
47
+ id: 'filmID'
36
48
  },
37
49
  get_gallery: {
38
50
  method: 'getGallery',
@@ -43,16 +55,39 @@ module KinopoiskAPI
43
55
  type: 'kp_similar_films',
44
56
  id: 'filmID'
45
57
  },
46
- get_name: {
47
- method: 'getStaffList',
48
- id: 'filmID'
58
+
59
+ navigator_filters:{
60
+ method: 'navigatorFilters'
61
+ },
62
+
63
+
64
+
65
+ get_top_100_popular: {
66
+ method: 'getKPTop',
67
+ type: 'kp_item_top_popular_films'
68
+ },
69
+
70
+ get_top_100_await: {
71
+ method: 'getKPTop',
72
+ type: 'kp_item_top_await'
49
73
  },
50
- get_genres: {
51
- method: 'getKPGenresView'
74
+
75
+ get_top_100_people: {
76
+ method: 'getKPTop',
77
+ type: 'kp_item_top_popular_people'
78
+ },
79
+
80
+ get_top_250_best:{
81
+ method: 'getKPTop',
82
+ type: 'kp_item_top_best_film'
52
83
  },
53
- get_top_genre: {
54
- method: 'getTopGenre'
84
+
85
+ get_top_100_gross:{
86
+ method: 'getKPTop',
87
+ type: 'kp_item_most_box_office',
55
88
  },
89
+
90
+
56
91
  get_reviews: {
57
92
  method: 'getKPReviews',
58
93
  id: 'filmID'
@@ -61,8 +96,11 @@ module KinopoiskAPI
61
96
  method: 'getReviewDetail',
62
97
  id: 'reviewID'
63
98
  },
64
- get_people_detail: {
65
- method: 'getPeopleDetail'
99
+
100
+ get_people: {
101
+ method: 'getKPPeopleDetailView',
102
+ id: 'peopleID'
103
+
66
104
  },
67
105
  get_today_films: {
68
106
  method: 'getKPTodayFilms'
@@ -94,24 +132,26 @@ module KinopoiskAPI
94
132
  get_dates_for_soon_dvd: {
95
133
  method: 'getDatesForSoonDVD'
96
134
  },
97
- get_top: {
98
- method: 'getTop'
99
- },
135
+
100
136
  get_best_films: {
101
137
  method: 'getBestFilms'
102
138
  },
139
+
103
140
  search_global: {
104
141
  method: 'getKPGlobalSearch',
105
142
  keyword: 'keyword'
106
143
  },
107
- search_films: {
144
+ search_film: {
108
145
  method: 'getKPSearchInFilms',
109
- keyword: 'keyword'
146
+ keyword: 'keyword',
147
+ page: 'page'
110
148
  },
111
149
  search_people: {
112
- method: 'searchPeople',
113
- keyword: 'keyword'
150
+ method: 'getKPSearchInPeople',
151
+ keyword: 'keyword',
152
+ page: 'page'
114
153
  },
154
+
115
155
  search_cinemas: {
116
156
  method: 'searchCinemas',
117
157
  keyword: 'keyword'
@@ -137,9 +177,9 @@ module KinopoiskAPI
137
177
  raise ToDo
138
178
  end
139
179
 
140
- def self.valid_json?(json)
180
+ def self.valid_json?(j)
141
181
  begin
142
- JSON.parse(json)
182
+ JSON.parse(j)
143
183
  return true
144
184
  rescue JSON::ParserError => e
145
185
  return false
@@ -2,21 +2,34 @@ module KinopoiskAPI
2
2
  class Agent
3
3
 
4
4
  def status
5
- @json['resultCode'].nil? ? true : false
5
+ @json[:resultCode].nil? ? true : false
6
6
  end
7
7
 
8
- def raw
8
+ def status2
9
+ @json[:resultCode].nil? ? true : false
10
+ end
11
+
12
+ def data
9
13
  @json
10
14
  end
11
15
 
16
+ def data2
17
+ @json2
18
+ end
19
+
12
20
  ####
13
21
  ####
14
22
  ####
15
23
 
16
24
  private
17
25
 
18
- def json
19
- uri = URI(@url)
26
+ def json(url=nil)
27
+ if url.nil?
28
+ uri = URI(@url)
29
+ else
30
+ uri = URI(url)
31
+ end
32
+
20
33
  uuid = Digest::MD5.hexdigest("--#{rand(10000)}--#{Time.now}--")
21
34
  query = URI.decode_www_form(String(uri.query)) << ["uuid", uuid]
22
35
  uri.query = URI.encode_www_form(query)
@@ -27,21 +40,31 @@ module KinopoiskAPI
27
40
  query = URI.decode_www_form(String(uri.query)) << ["key", key]
28
41
  uri.query = URI.encode_www_form(query)
29
42
 
30
- puts "[GET] -> " + uri.to_s
43
+ print "[GET] -> " + uri.to_s
44
+ get_time = Time.now
45
+
46
+ http = Net::HTTP.new(uri.host, uri.port)
47
+ http.read_timeout = 10
48
+ http.use_ssl = true
49
+ response = http.get(uri.request_uri, DOMAINS[:headers])
31
50
 
32
- http = Net::HTTP.new(uri.host, uri.port)
33
- http.use_ssl = true
34
- response = http.get(uri.request_uri, DOMAINS[:headers])
51
+ print " <- [#{(Time.now-get_time).round 3}s]\n"
35
52
 
36
53
  if KinopoiskAPI::valid_json?(response.body)
37
- json = JSON.parse(response.body)
38
- if json['resultCode'] == 0
39
- json['data']
54
+ j = JSON.parse(response.body)
55
+ if j['resultCode'] == 0
56
+ j['data']
40
57
  else
41
- json
58
+ j
42
59
  end
43
60
  else
44
- {'resultCode'=> -1, "message"=> "Error method require", :data => { 'code'=> response.code, 'body'=> response.body} }
61
+ {:resultCode => -1, :message=> "Error method require", :data => { :code => response.code, :body => response.body} }
62
+ end
63
+ end
64
+
65
+ def json2
66
+ if @json2.nil?
67
+ @json2 = json(@url2)
45
68
  end
46
69
  end
47
70
 
@@ -54,5 +77,176 @@ module KinopoiskAPI
54
77
  end
55
78
  end
56
79
 
80
+ def int_data(data, name, none=0, type=Integer)
81
+ s = dn(data, name)
82
+
83
+ if s.nil?
84
+ none
85
+ else
86
+ r = s
87
+ if r.class == String
88
+ r = s.gsub(/[\ \%\$]/i, '')
89
+ end
90
+
91
+ if type == Integer
92
+ r.to_i == 0 ? none : r.to_i
93
+ elsif type == Float
94
+ r.to_f == 0 ? none : r.to_f
95
+ else
96
+ r
97
+ end
98
+ end
99
+ end
100
+
101
+ def year_data(data, name, point=:start)
102
+ s = dn(data, name)
103
+
104
+ if s.nil?
105
+ nil
106
+ else
107
+ if s.size == 4 && point == :start
108
+ s.to_i
109
+ else s.size == 9
110
+ arr = s.scan(/(\d{4})/).flatten
111
+ if point == :start
112
+ arr[0].to_i
113
+ elsif point == :end
114
+ arr[1].to_i
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ def time_data(data, name)
121
+ s = dn(data, name)
122
+ if !s.nil?
123
+
124
+ if s.size == 10
125
+ d=Date.parse(s) rescue nil
126
+ if d.nil?
127
+ s=s.gsub('00.', '01.')
128
+ d=Date.parse(s) rescue nil
129
+ end
130
+ d
131
+ else
132
+ year = s.scan(/\d{4}/)[0]
133
+ if !year.nil?
134
+ Date.parse("01.01.#{year}") #only year???
135
+ end
136
+ end
137
+
138
+ end
139
+ end
140
+
141
+ def arr_data(data, name)
142
+ s = dn(data, name)
143
+ if s.nil?
144
+ []
145
+ else
146
+ s.split(',').map { |genre| genre.strip }
147
+ end
148
+ end
149
+
150
+ def str_data(data, name)
151
+ s = dn(data, name)
152
+
153
+ if s.class == String
154
+ s
155
+ elsif s.class == NilClass
156
+ nil
157
+ else
158
+ s.to_s
159
+ end
160
+ end
161
+
162
+ def bool_data(data, name)
163
+ s = dn(data, name)
164
+
165
+ if s.class == String
166
+ s = s.to_i
167
+ end
168
+
169
+ if s.class == TrueClass || s.class == FalseClass
170
+ s
171
+ else
172
+ s.present? && s == 1 ? true : false
173
+ end
174
+ end
175
+
176
+ def min_data(data, name)
177
+ s = dn(data, name)
178
+ s.present? ? Time.parse(s).seconds_since_midnight.to_i / 60 : 0
179
+ end
180
+
181
+ def url_data(data, name, id, poster_name)
182
+ s = dn(data, name)
183
+ s.nil? ? nil : "#{DOMAINS[:kinopoisk][:poster][poster_name]}_#{id}.jpg"
184
+ end
185
+
186
+
187
+ def film_hash(h, id='filmID')
188
+ {
189
+ id: int_data(String, h[id ]),
190
+ kp_type: str_data(String, h['type' ]),
191
+ name_ru: str_data(String, h['nameRU' ]),
192
+ name_en: str_data(String, h['nameEN' ]),
193
+ slogan: str_data(String, h['slogan' ]),
194
+ description: str_data(String, h['description' ]),
195
+ poster_url: url_data(String, h['posterURL' ], h[id], :film),
196
+ year: year_data(String, h['year' ], :start),
197
+ year_end: year_data(String, h['year' ], :end),
198
+ reviews_count: int_data(String, h['reviewsCount']),
199
+ duration: min_data(String, h['filmLength' ]),
200
+ countries: arr_data(String, h['country' ]),
201
+ genres: arr_data(String, h['genre' ]),
202
+ video: h['videoURL'],
203
+ is_sequel_or_prequel: bool_data(String, h['hasSequelsAndPrequelsFilms']),
204
+ is_similar_films: bool_data(String, h['hasRelatedFilms' ]),
205
+ is_imax: bool_data(String, h['isIMAX' ]),
206
+ is_3d: bool_data(String, h['is3D' ]),
207
+ rating_mpaa: str_data(String, h['ratingMPAA' ]),
208
+ minimal_age: int_data(String, h['ratingAgeLimits' ])
209
+ }
210
+ end
211
+
212
+ def people_hash(h)
213
+ {
214
+ :id => int_data(String, h['id' ]),
215
+ :kp_type => str_data(String, h['type' ]),
216
+ :name_ru => str_data(String, h['nameRU' ]),
217
+ :name_en => str_data(String, h['nameEN' ]),
218
+ :poster_url => url_data(String, h['posterURL' ], @id, :name),
219
+ :sex => str_data(String, h['sex' ]),
220
+ :growth => int_data(String, h['growth' ]),
221
+ :birthday => time_data(String, h['birthday' ]),
222
+ :birthplace => str_data(String, h['sex' ]),
223
+ :has_awards => bool_data(String, h['has_awards']),
224
+ :profession => s2a(str_data(String, h['profession']))
225
+ }
226
+ end
227
+
228
+
229
+
230
+
231
+
232
+
233
+ def dn(data, name)
234
+ if data.nil?
235
+ r = @json[name]
236
+ elsif data == String
237
+ r = name
238
+ else
239
+ if @json[data].nil?
240
+ r = nil
241
+ else
242
+ r = @json[data][name]
243
+ end
244
+ end
245
+ r
246
+ end
247
+
248
+
249
+
250
+
57
251
  end
58
252
  end
@@ -0,0 +1,21 @@
1
+ module KinopoiskAPI
2
+ class APIerror < StandardError
3
+
4
+ def initialize(message, data)
5
+ super(message)
6
+ @data = data
7
+ end
8
+ attr_reader :data
9
+
10
+
11
+ def code
12
+ @data[:code]
13
+ end
14
+
15
+ def body
16
+ @data[:body]
17
+ end
18
+
19
+
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ module KinopoiskAPI
2
+ class Category < Agent
3
+ attr_accessor :url
4
+
5
+ def initialize
6
+ @url = "#{DOMAINS[:api]}/#{METHODS[:navigator_filters][:method]}"
7
+ @json = json
8
+ end
9
+
10
+ def genres
11
+ h(@json['genre'])
12
+ end
13
+
14
+ def countries
15
+ h(@json['country'])
16
+ end
17
+
18
+ private
19
+ def h(j)
20
+ j.map do |item|
21
+ {
22
+ id: int_data(String, item["id" ]),
23
+ name: str_data(String, item["name" ]),
24
+ popular: bool_data(String, item["popular" ])
25
+ }
26
+ end
27
+ end
28
+
29
+ #private end
30
+ end
31
+ end