KinopoiskAPI 0.8.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,58 @@
1
+ module KinopoiskAPI
2
+ class Agent
3
+
4
+ def status
5
+ @json['resultCode'].nil? ? true : false
6
+ end
7
+
8
+ def raw
9
+ @json
10
+ end
11
+
12
+ ####
13
+ ####
14
+ ####
15
+
16
+ private
17
+
18
+ def json
19
+ uri = URI(@url)
20
+ uuid = Digest::MD5.hexdigest("--#{rand(10000)}--#{Time.now}--")
21
+ query = URI.decode_www_form(String(uri.query)) << ["uuid", uuid]
22
+ uri.query = URI.encode_www_form(query)
23
+
24
+ path = uri.to_s.gsub("#{DOMAINS[:api]}/","") + DOMAINS[:salt]
25
+ key = Digest::MD5.hexdigest(path)
26
+
27
+ query = URI.decode_www_form(String(uri.query)) << ["key", key]
28
+ uri.query = URI.encode_www_form(query)
29
+
30
+ puts "[GET] -> " + uri.to_s
31
+
32
+ http = Net::HTTP.new(uri.host, uri.port)
33
+ http.use_ssl = true
34
+ response = http.get(uri.request_uri, DOMAINS[:headers])
35
+
36
+ if KinopoiskAPI::valid_json?(response.body)
37
+ json = JSON.parse(response.body)
38
+ if json['resultCode'] == 0
39
+ json['data']
40
+ else
41
+ json
42
+ end
43
+ else
44
+ {'resultCode'=> -1, "message"=> "Error method require", :data => { 'code'=> response.code, 'body'=> response.body} }
45
+ end
46
+ end
47
+
48
+
49
+ def s2a(str)
50
+ if str.nil?
51
+ []
52
+ else
53
+ str.split(',').map { |i| i.strip }
54
+ end
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,258 @@
1
+ module KinopoiskAPI
2
+ class Film < Agent
3
+ attr_accessor :id, :url
4
+
5
+ def initialize(id)
6
+ @id = id
7
+ @url = "#{DOMAINS[:api]}/#{METHODS[:get_film][:method]}?#{METHODS[:get_film][:id]}=#{id}"
8
+ @json = json
9
+ end
10
+
11
+ def all
12
+ {
13
+ id: @id,
14
+ url: url,
15
+ title: title,
16
+ slogan: slogan,
17
+ description: description,
18
+ poster: poster,
19
+ year: year,
20
+ kinopoisk: kinopoisk,
21
+ imdb: imdb,
22
+ number_of_reviews: number_of_reviews,
23
+ duration: duration,
24
+ countries: countries,
25
+ genres: genres,
26
+ video: video,
27
+ is_sequel_or_prequel: is_sequel_or_prequel,
28
+ is_similar_films: is_similar_films,
29
+ is_imax: is_imax,
30
+ is_3d: is_3d,
31
+ rating_mpaa: rating_mpaa,
32
+ minimal_age: minimal_age,
33
+ names: all_names
34
+ }
35
+ end
36
+
37
+ def url
38
+ @json['webURL'].present? ? @json['webURL'] : nil
39
+ end
40
+
41
+ def title
42
+ {
43
+ ru: @json['nameRU'].present? ? @json['nameRU'] : nil,
44
+ en: @json['nameEN'].present? ? @json['nameEN'] : nil
45
+ }
46
+ end
47
+
48
+ def slogan
49
+ @json['slogan'].present? ? @json['slogan'] : nil
50
+ end
51
+
52
+ def description
53
+ @json['description'].present? ? @json['description'] : nil
54
+ end
55
+
56
+ def poster
57
+ "#{DOMAINS[:kinopoisk][:poster][:film]}_#{@id}.jpg"
58
+ end
59
+
60
+ def year
61
+ @json['year'].present? ? @json['year'] : nil
62
+ end
63
+
64
+ def kinopoisk
65
+ if @json.present?
66
+ local_data = @json['ratingData']
67
+
68
+ if !local_data.nil?
69
+ rating = local_data['rating'].nil? ? nil : local_data['rating']
70
+ quantity = local_data['ratingVoteCount'].nil? ? nil : local_data['ratingVoteCount'].to_s.delete(' ').to_i
71
+
72
+ good_reviews_in_percentage = local_data['ratingGoodReview'].nil? ? nil : local_data['ratingGoodReview']
73
+ number_of_good_reviews = local_data['ratingGoodReviewVoteCount'].nil? ? nil : local_data['ratingGoodReviewVoteCount'].to_s.delete(' ').to_i
74
+
75
+ waiting_in_percentage = local_data['ratingAwait'].nil? ? nil : local_data['ratingAwait']
76
+ number_of_waiting = local_data['ratingAwaitCount'].nil? ? nil : local_data['ratingAwaitCount'].to_s.delete(' ').to_i
77
+
78
+ film_critics_in_percentage = local_data['ratingFilmCritics'].nil? ? nil : local_data['ratingFilmCritics']
79
+ film_critics = local_data['ratingFilmCriticsVoteCount'].nil? ? nil : local_data['ratingFilmCriticsVoteCount'].to_s.delete(' ').to_i
80
+
81
+ rf_critics_in_percentage = local_data['ratingRFCritics'].nil? ? nil : local_data['ratingRFCritics']
82
+ rf_critics = local_data['ratingRFCriticsVoteCount'].nil? ? nil : local_data['ratingRFCriticsVoteCount'].to_s.delete(' ').to_i
83
+ else
84
+ rating = 0.0
85
+ quantity = 0
86
+
87
+ good_reviews_in_percentage = '0%'
88
+ number_of_good_reviews = 0
89
+
90
+ waiting_in_percentage = '0%'
91
+ number_of_waiting = 0
92
+
93
+ film_critics_in_percentage = '0%'
94
+ film_critics = 0
95
+
96
+ rf_critics_in_percentage = '0%'
97
+ rf_critics = 0
98
+ end
99
+
100
+ {
101
+ rating: rating,
102
+ quantity: quantity,
103
+
104
+ good_reviews_in_percentage: good_reviews_in_percentage,
105
+ number_of_good_reviews: number_of_good_reviews,
106
+
107
+ waiting_in_percentage: waiting_in_percentage,
108
+ number_of_waiting: number_of_waiting,
109
+
110
+ film_critics_in_percentage: film_critics_in_percentage,
111
+ film_critics: film_critics,
112
+
113
+ rf_critics_in_percentage: rf_critics_in_percentage,
114
+ rf_critics: rf_critics
115
+ }
116
+ else
117
+ nil
118
+ end
119
+ end
120
+
121
+ def imdb
122
+ if @json.present?
123
+ local_data = @json['ratingData']
124
+
125
+ if !local_data.nil?
126
+ rating = local_data['ratingIMDb'].nil? ? 0.0 : local_data['ratingIMDb']
127
+ quantity = local_data['ratingIMDbVoteCount'].nil? ? 0 : local_data['ratingIMDbVoteCount'].to_s.delete(' ').to_i
128
+ else
129
+ rating = 0.0
130
+ quantity = 0
131
+ end
132
+
133
+ {
134
+ id: @json['imdbID'],
135
+ rating: rating,
136
+ quantity: quantity
137
+ }
138
+ else
139
+ nil
140
+ end
141
+ end
142
+
143
+ def number_of_reviews
144
+ @json['reviewsCount'].present? ? @json['year'] : nil
145
+ end
146
+
147
+ def duration
148
+ @json['filmLength'].present? ? @json['filmLength'] : 0
149
+ end
150
+
151
+ def countries
152
+ if @json.present?
153
+ @json['country'].present? ? @json['country'].split(',').map { |country| country.strip } : nil
154
+ else
155
+ nil
156
+ end
157
+ end
158
+
159
+ def genres
160
+ if @json.present?
161
+ @json['genre'].present? ? @json['genre'].split(',').map { |genre| genre.strip } : nil
162
+ else
163
+ nil
164
+ end
165
+ end
166
+
167
+ def video
168
+ @json['videoURL'].present? ? @json['videoURL'] : nil
169
+ end
170
+
171
+ def is_sequel_or_prequel
172
+ @json['isHasSequelsAndPrequelsFilms'].present? ? @json['isHasSequelsAndPrequelsFilms'] : false
173
+ end
174
+
175
+ def is_similar_films
176
+ @json['isHasSimilarFilms'].present? ? @json['isHasSimilarFilms'] : false
177
+ end
178
+
179
+ def is_imax
180
+ @json['isIMAX'].present? ? @json['isIMAX'] : false
181
+ end
182
+
183
+ def is_3d
184
+ @json['is3D'].present? ? @json['is3D'] : false
185
+ end
186
+
187
+ def rating_mpaa
188
+ @json['ratingMPAA'].present? ? @json['ratingMPAA'] : nil
189
+ end
190
+
191
+ def minimal_age
192
+ @json['ratingAgeLimits'].present? ? @json['ratingAgeLimits'] : 0
193
+ end
194
+
195
+ def all_names
196
+ correctly = {}
197
+ unless creators.nil?
198
+ creators.each do |items|
199
+ new_items = []
200
+ items.each do |item|
201
+ poster = item['posterURL'].present? ? "#{DOMAINS[:kinopoisk][:poster][:name]}_#{item['id']}.jpg" : nil
202
+ new_item = {
203
+ id: item['id'],
204
+ url: "#{DOMAINS[:kinopoisk][:main]}/name/#{item['id']}",
205
+ full_name: {
206
+ ru: item['nameRU'].present? ? item['nameRU'] : nil,
207
+ en: item['nameEN'].present? ? item['nameEN'] : nil
208
+ },
209
+ description: item['description'].present? ? item['description'] : nil,
210
+ poster: poster,
211
+ profession: item['professionText'].present? ? item['professionText'] : nil
212
+ }
213
+ new_items.push(new_item)
214
+ end
215
+ items.each do |item|
216
+ correctly[item['professionKey']] = new_items
217
+ end
218
+ end
219
+ end
220
+ correctly
221
+ end
222
+
223
+ def name_profession(name)
224
+ names[name].nil? ? nil : names[name]
225
+ end
226
+
227
+ def premiere
228
+ local_data = @json['rentData']
229
+
230
+ if !local_data.nil?
231
+ ru = local_data['premiereRU'].nil? ? nil : Date.parse(local_data['premiereRU'])
232
+ world = local_data['premiereWorld'].nil? ? nil : Date.parse(local_data['premiereWorld'])
233
+ world_country = local_data['premiereWorldCountry'].nil? ? nil : local_data['premiereWorldCountry']
234
+ else
235
+ ru = nil
236
+ world = nil
237
+ world_country = nil
238
+ end
239
+
240
+ {
241
+ ru: ru,
242
+ world: world,
243
+ world_country: world_country
244
+ }
245
+ end
246
+
247
+ private
248
+
249
+ def creators
250
+ if @json.present?
251
+ @json['creators'].present? ? @json['creators'] : nil
252
+ else
253
+ nil
254
+ end
255
+ end
256
+
257
+ end
258
+ end
@@ -0,0 +1,38 @@
1
+ module KinopoiskAPI
2
+ class Gallery < Agent
3
+ attr_accessor :id, :url
4
+
5
+ def initialize(id)
6
+ @id = id
7
+ @url = "#{DOMAINS[:api]}/#{METHODS[:get_gallery][:method]}?#{METHODS[:get_gallery][:id]}=#{id}"
8
+ @json = json
9
+ end
10
+
11
+ def all
12
+ correctly = {}
13
+ gallery.each do |items|
14
+ new_items = []
15
+ items.last.each do |item|
16
+ new_item = {
17
+ image: "#{DOMAINS[:kinopoisk][:poster][:gallery]}/#{item['image']}",
18
+ preview: "#{DOMAINS[:kinopoisk][:poster][:gallery]}/#{item['preview']}"
19
+ }
20
+ new_items.push(new_item)
21
+ end
22
+ correctly[items.first] = new_items
23
+ end
24
+ correctly
25
+ end
26
+
27
+ def section(name)
28
+ all[name]
29
+ end
30
+
31
+ private
32
+
33
+ def gallery
34
+ @json['gallery']
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+ module KinopoiskAPI
2
+ class Genres < Agent
3
+ attr_accessor :url
4
+
5
+ def initialize
6
+ @url = "#{DOMAINS[:api]}/#{METHODS[:get_genres][:method]}"
7
+ @json = json
8
+ end
9
+
10
+ def all
11
+ correctly = []
12
+ genres.each do |item|
13
+ new_item = {
14
+ id: item['genreID'],
15
+ name: item['genreName']
16
+ }
17
+ correctly.push(new_item)
18
+ end
19
+ correctly
20
+ end
21
+
22
+ private
23
+
24
+ def genres
25
+ @json['genreData']
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,105 @@
1
+ module KinopoiskAPI
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
+ end
10
+
11
+ def all
12
+ {
13
+ keyword: keyword,
14
+ quantity: {
15
+ films: number_of_films,
16
+ names: number_of_names
17
+ },
18
+ exactly: exactly,
19
+ maybe: maybe,
20
+ names: names
21
+ }
22
+ end
23
+
24
+ def keyword
25
+ @json['keyword']
26
+ end
27
+
28
+ def number_of_films
29
+ @json['searchFilmsCountResult']
30
+ end
31
+
32
+ def number_of_names
33
+ @json['searchPeoplesCountResult']
34
+ end
35
+
36
+ def exactly
37
+ {
38
+ id: json_exactly['id'],
39
+ title: {
40
+ ru: json_exactly['nameRU'],
41
+ en: json_exactly['nameEN']
42
+ },
43
+ info: json_exactly['description'],
44
+ duration: json_exactly['filmLength'],
45
+ year: json_exactly['year'],
46
+ countries: json_exactly['country'].split(',').map { |country| country.strip },
47
+ genres: json_exactly['genre'].split(',').map { |genre| genre.strip },
48
+ rating: json_exactly['rating'],
49
+ poster: "#{DOMAINS[:kinopoisk][:poster][:film]}_#{json_exactly['id']}.jpg"
50
+ }
51
+ end
52
+
53
+ def maybe
54
+ correctly = []
55
+ json_films.each do |film|
56
+ new_item = {
57
+ id: json_exactly['id'],
58
+ title: {
59
+ ru: json_exactly['nameRU'],
60
+ en: json_exactly['nameEN']
61
+ },
62
+ info: film['description'],
63
+ duration: film['filmLength'],
64
+ year: film['year'],
65
+ countries: film['country'].split(',').map { |country| country.strip },
66
+ genres: film['genre'].split(',').map { |genre| genre.strip },
67
+ poster: "#{DOMAINS[:kinopoisk][:poster][:film]}_#{film['id']}.jpg"
68
+ }
69
+ correctly.push(new_item)
70
+ end
71
+ correctly
72
+ end
73
+
74
+ def names
75
+ correctly = []
76
+ json_names.each do |name|
77
+ new_item = {
78
+ full_name: {
79
+ ru: name['nameRU'],
80
+ en: name['nameEN']
81
+ },
82
+ info: name['description'],
83
+ poster: "#{DOMAINS[:kinopoisk][:poster][:name]}_#{name['id']}.jpg"
84
+ }
85
+ correctly.push(new_item)
86
+ end
87
+ correctly
88
+ end
89
+
90
+ private
91
+
92
+ def json_exactly
93
+ @json['youmean']
94
+ end
95
+
96
+ def json_films
97
+ @json['searchFilms']
98
+ end
99
+
100
+ def json_names
101
+ @json['searchPeople']
102
+ end
103
+
104
+ end
105
+ end