-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathrating.js
269 lines (255 loc) · 9.18 KB
/
rating.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
(function () {
'use strict';
function rating_kp_imdb(card) {
var network = new Lampa.Reguest();
var clean_title = kpCleanTitle(card.title);
var search_date = card.release_date || card.first_air_date || card.last_air_date || '0000';
var search_year = parseInt((search_date + '').slice(0, 4));
var orig = card.original_title || card.original_name;
var kp_prox = '';
var params = {
id: card.id,
url: kp_prox + 'https://kinopoiskapiunofficial.tech/',
rating_url: kp_prox + 'https://rating.kinopoisk.ru/',
headers: {
'X-API-KEY': '2a4a0808-81a3-40ae-b0d3-e11335ede616'
},
cache_time: 60 * 60 * 24 * 1000 //86400000 сек = 1день Время кэша в секундах
};
getRating();
function getRating() {
var movieRating = _getCache(params.id);
if (movieRating) {
return _showRating(movieRating[params.id]);
} else {
searchFilm();
}
}
function searchFilm() {
var url = params.url;
var url_by_title = Lampa.Utils.addUrlComponent(url + 'api/v2.1/films/search-by-keyword', 'keyword=' + encodeURIComponent(clean_title));
if (card.imdb_id) url = Lampa.Utils.addUrlComponent(url + 'api/v2.2/films', 'imdbId=' + encodeURIComponent(card.imdb_id));
else url = url_by_title;
network.clear();
network.timeout(15000);
network.silent(url, function (json) {
if (json.items && json.items.length) chooseFilm(json.items);
else if (json.films && json.films.length) chooseFilm(json.films);
else if (url !== url_by_title) {
network.clear();
network.timeout(15000);
network.silent(url_by_title, function (json) {
if (json.items && json.items.length) chooseFilm(json.items);
else if (json.films && json.films.length) chooseFilm(json.films);
else chooseFilm([]);
}, function (a, c) {
showError(network.errorDecode(a, c));
}, false, {
headers: params.headers
});
} else chooseFilm([]);
}, function (a, c) {
showError(network.errorDecode(a, c));
}, false, {
headers: params.headers
});
}
function chooseFilm(items) {
if (items && items.length) {
var is_sure = false;
var is_imdb = false;
items.forEach(function (c) {
var year = c.start_date || c.year || '0000';
c.tmp_year = parseInt((year + '').slice(0, 4));
});
if (card.imdb_id) {
var tmp = items.filter(function (elem) {
return (elem.imdb_id || elem.imdbId) == card.imdb_id;
});
if (tmp.length) {
items = tmp;
is_sure = true;
is_imdb = true;
}
}
var cards = items;
if (cards.length) {
if (orig) {
var _tmp = cards.filter(function (elem) {
return containsTitle(elem.orig_title || elem.nameOriginal, orig) || containsTitle(elem.en_title || elem.nameEn, orig) || containsTitle(elem.title || elem.ru_title || elem.nameRu, orig);
});
if (_tmp.length) {
cards = _tmp;
is_sure = true;
}
}
if (card.title) {
var _tmp2 = cards.filter(function (elem) {
return containsTitle(elem.title || elem.ru_title || elem.nameRu, card.title) || containsTitle(elem.en_title || elem.nameEn, card.title) || containsTitle(elem.orig_title || elem.nameOriginal, card.title);
});
if (_tmp2.length) {
cards = _tmp2;
is_sure = true;
}
}
if (cards.length > 1 && search_year) {
var _tmp3 = cards.filter(function (c) {
return c.tmp_year == search_year;
});
if (!_tmp3.length) _tmp3 = cards.filter(function (c) {
return c.tmp_year && c.tmp_year > search_year - 2 && c.tmp_year < search_year + 2;
});
if (_tmp3.length) cards = _tmp3;
}
}
if (cards.length == 1 && is_sure && !is_imdb) {
if (search_year && cards[0].tmp_year) {
is_sure = cards[0].tmp_year > search_year - 2 && cards[0].tmp_year < search_year + 2;
}
if (is_sure) {
is_sure = false;
if (orig) {
is_sure |= equalTitle(cards[0].orig_title || cards[0].nameOriginal, orig) || equalTitle(cards[0].en_title || cards[0].nameEn, orig) || equalTitle(cards[0].title || cards[0].ru_title || cards[0].nameRu, orig);
}
if (card.title) {
is_sure |= equalTitle(cards[0].title || cards[0].ru_title || cards[0].nameRu, card.title) || equalTitle(cards[0].en_title || cards[0].nameEn, card.title) || equalTitle(cards[0].orig_title || cards[0].nameOriginal, card.title);
}
}
}
if (cards.length == 1 && is_sure) {
var id = cards[0].kp_id || cards[0].kinopoisk_id || cards[0].kinopoiskId || cards[0].filmId;
var base_search = function base_search() {
network.clear();
network.timeout(15000);
network.silent(params.url + 'api/v2.2/films/' + id, function (data) {
var movieRating = _setCache(params.id, {
kp: data.ratingKinopoisk,
imdb: data.ratingImdb,
timestamp: new Date().getTime()
}); // Кешируем данные
return _showRating(movieRating);
}, function (a, c) {
showError(network.errorDecode(a, c));
}, false, {
headers: params.headers
});
};
network.clear();
network.timeout(5000);
network["native"](params.rating_url + id + '.xml', function (str) {
if (str.indexOf('<rating>') >= 0) {
try {
var ratingKinopoisk = 0;
var ratingImdb = 0;
var xml = $($.parseXML(str));
var kp_rating = xml.find('kp_rating');
if (kp_rating.length) {
ratingKinopoisk = parseFloat(kp_rating.text());
}
var imdb_rating = xml.find('imdb_rating');
if (imdb_rating.length) {
ratingImdb = parseFloat(imdb_rating.text());
}
var movieRating = _setCache(params.id, {
kp: ratingKinopoisk,
imdb: ratingImdb,
timestamp: new Date().getTime()
}); // Кешируем данные
return _showRating(movieRating);
} catch (ex) {
}
}
base_search();
}, function (a, c) {
base_search();
}, false, {
dataType: 'text'
});
} else {
var movieRating = _setCache(params.id, {
kp: 0,
imdb: 0,
timestamp: new Date().getTime()
}); // Кешируем данные
return _showRating(movieRating);
}
} else {
var _movieRating = _setCache(params.id, {
kp: 0,
imdb: 0,
timestamp: new Date().getTime()
}); // Кешируем данные
return _showRating(_movieRating);
}
}
function cleanTitle(str){
return str.replace(/[\s.,:;’'`!?]+/g, ' ').trim();
}
function kpCleanTitle(str){
return cleanTitle(str).replace(/^[ \/\\]+/, '').replace(/[ \/\\]+$/, '').replace(/\+( *[+\/\\])+/g, '+').replace(/([+\/\\] *)+\+/g, '+').replace(/( *[\/\\]+ *)+/g, '+');
}
function normalizeTitle(str){
return cleanTitle(str.toLowerCase().replace(/[\-\u2010-\u2015\u2E3A\u2E3B\uFE58\uFE63\uFF0D]+/g, '-').replace(/ё/g, 'е'));
}
function equalTitle(t1, t2){
return typeof t1 === 'string' && typeof t2 === 'string' && normalizeTitle(t1) === normalizeTitle(t2);
}
function containsTitle(str, title){
return typeof str === 'string' && typeof title === 'string' && normalizeTitle(str).indexOf(normalizeTitle(title)) !== -1;
}
function showError(error) {
Lampa.Noty.show('Рейтинг KP: ' + error);
}
function _getCache(movie) {
var timestamp = new Date().getTime();
var cache = Lampa.Storage.cache('kp_rating', 500, {}); //500 это лимит ключей
if (cache[movie]) {
if ((timestamp - cache[movie].timestamp) > params.cache_time) {
// Если кеш истёк, чистим его
delete cache[movie];
Lampa.Storage.set('kp_rating', cache);
return false;
}
} else return false;
return cache;
}
function _setCache(movie, data) {
var timestamp = new Date().getTime();
var cache = Lampa.Storage.cache('kp_rating', 500, {}); //500 это лимит ключей
if (!cache[movie]) {
cache[movie] = data;
Lampa.Storage.set('kp_rating', cache);
} else {
if ((timestamp - cache[movie].timestamp) > params.cache_time) {
data.timestamp = timestamp;
cache[movie] = data;
Lampa.Storage.set('kp_rating', cache);
} else data = cache[movie];
}
return data;
}
function _showRating(data) {
if (data) {
var kp_rating = !isNaN(data.kp) && data.kp !== null ? parseFloat(data.kp).toFixed(1) : '0.0';
var imdb_rating = !isNaN(data.imdb) && data.imdb !== null ? parseFloat(data.imdb).toFixed(1) : '0.0';
var render = Lampa.Activity.active().activity.render();
$('.wait_rating', render).remove();
$('.rate--imdb', render).removeClass('hide').find('> div').eq(0).text(imdb_rating);
$('.rate--kp', render).removeClass('hide').find('> div').eq(0).text(kp_rating);
}
}
}
function startPlugin() {
window.rating_plugin = true;
Lampa.Listener.follow('full', function (e) {
if (e.type == 'complite') {
var render = e.object.activity.render();
if ($('.rate--kp', render).hasClass('hide') && !$('.wait_rating', render).length) {
$('.info__rate', render).after('<div style="width:2em;margin-top:1em;margin-right:1em" class="wait_rating"><div class="broadcast__scan"><div></div></div><div>');
rating_kp_imdb(e.data.movie);
}
}
});
}
if (!window.rating_plugin) startPlugin();
})();