-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathktuvitManager.js
411 lines (373 loc) · 12.2 KB
/
ktuvitManager.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* @author Maor Magori
* @copyright Maor Magori 2020
* @license MIT
*/
// Importing notations
// The request manager.
const superagent = require("superagent");
// Custom encoding middleware for superagent
require("./detectAndEncodeResponse")(superagent);
// HTML parser.
const jsdom = require("jsdom");
// converts IMDb id to name.
const imdb2name = require("name-to-imdb");
/**
* Class that handles all the calls to Ktuvit.me
*/
class KtuvitManager {
CACHE_TTL = 12 * 60 * 60 * 1000;
// Ktuvit's current address
static BASE_URL = "https://www.ktuvit.me/";
// Enum of used Ktuvit links.
static KTUVIT = {
SEARCH_URL:
this.BASE_URL + "Services/ContentProvider.svc/SearchPage_search",
MOVIE_INFO_URL: this.BASE_URL + "MovieInfo.aspx?ID=",
EPISODE_INFO_URL: this.BASE_URL + "Services/GetModuleAjax.ashx?",
REQUEST_DOWNLOAD_IDENTIFIER_URL:
this.BASE_URL + "Services/ContentProvider.svc/RequestSubtitleDownload",
DOWNLOAD_SUB_URL:
this.BASE_URL + "Services/DownloadFile.ashx?DownloadIdentifier=",
LOGIN_URL: this.BASE_URL + "Services/MembershipService.svc/Login",
};
/**
* @constructor
* @param {string} loginCookie User's login cookie's content from Ktuvit.
* @param {boolean} useCache Should manager cache ktuvit ID results. disabling makes the manager return non-idempotent results
*/
constructor(loginCookie, useCache = true) {
this.loginCookie = loginCookie;
this.headers = {
accept: "application/json, text/javascript, */*; q=0.01",
cookie: `Login=${this.loginCookie}`,
};
this.useCache = useCache;
this.ktuvitIdCache = {};
this.ktuvitCacheLifetime = {};
}
/**
* Use in case you don't have the user's login cookie.
* @todo Add error for wrong credentials.
* @returns The given user's login cookie
* @param {string} email
* @param {string} hashedPass
*/
static async getLoginCookie(email, hashedPass) {
return superagent
.post(KtuvitManager.KTUVIT.LOGIN_URL)
.send({ request: { Email: email, Password: hashedPass } })
.then((res) => {
//Parsing the cookie as a string because a cookie parser would be
// an extra dependency for a single use.
return res.headers["set-cookie"][1].split(";")[0].replace("Login=", "");
});
}
/**
* Use this to make sure your manager's cookie works.
* @returns {true|false} true if cookie works, false otherwise.
*/
validateCookie() {
//TODO: add a validator.
}
/**
* Makes post calls with the user's cookie.
* @param {string} link url
* @param {string} data post data.
* @returns {Promise} request promise
*/
postWithLoginInfo(link, data) {
return new Promise((resolve, reject) => {
superagent
.post(link)
.withCredentials()
.set(this.headers)
.send({ request: data })
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
});
}
/**
* Makes get calls with the user's cookie.
* @param {string} link url
* @returns {Promise} request promise
*/
getWithLoginInfo(link) {
return new Promise((resolve, reject) => {
superagent
.get(link)
.withCredentials()
//.timeout({deadline: 6000})
.set(this.headers)
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
});
}
/**
* Searches given arguments on ktuvit and returns results array.
* As an advice you should avoid using anything besides name, type, year and withSubsOnly.
* @typedef {object} Item
* @property {string} name Title's name
* @property {Array.<string>} actors actors names
* @property {Array.<string>} directors directors names
* @property {Array.<string>} genres title's geners
* @property {Array.<string>} countries Title's origin countries
* @property {Array.<string>} languages title's languages
* @property {string} year relese year or series range
* @property {Array.<string>} rating imdb ratings to include.
* @property {movie|series} type Title's type
* @property {true|false} withSubsOnly weather to show subless titles.
* @property {string} imdbId Imdb id
* @param {Item} item search parameters.
* @returns {Promise<Array>} results array
*/
async searchKtuvit(item) {
// The search query
const query = {
FilmName: item.name || "",
Actors: item.actors || [],
Studios: null,
Directors: item.directors || [],
Genres: item.genres || [],
Countries: item.countries || [],
Languages: item.languages || [],
Year: item.year === undefined ? "" : `${item.year}`.split("–")[0].trim(),
Rating: item.rating || [],
Page: 1,
SearchType:
item.type === undefined ? "-1" : item.type == "movie" ? "0" : "1",
WithSubsOnly: item.withSubsOnly || false,
};
try {
let res = await this.postWithLoginInfo(
KtuvitManager.KTUVIT.SEARCH_URL,
query
);
const parsedData = JSON.parse(res.body.d);
if (parsedData.ErrorMessage == "" || parsedData.ErrorMessage == null)
return JSON.parse(res.body.d).Films;
else {
// The search query's arguments are wrong.
throw new Error("Incorrect search Values");
}
} catch (err) {
if (err.message == "Incorrect search Values") {
console.log("search query: \n", query);
throw err;
} else {
// Agent error
console.error(
"agent error. Please use validateCookie() to make sure your cookie works."
);
throw err;
}
}
}
/**
* Returns just the Ktuvit ID of a title's. Can only be used with imdbID.
* @param {Item} item search Item
* @returns {string|null} Ktuvit ID
*/
async getKtuvitID(item) {
if (this._isKtuvitIdCached(item.imdbId) && this.useCache) {
return this.ktuvitIdCache[item.imdbId];
}
if (!item.imdbId) {
throw new Error("imdbId not provided.");
}
try {
item.name =
item.name || (await this._getTitleNameFromImdbId(item.imdbId));
} catch (err) {
} finally {
if (!item.name) {
throw new Error("Could not fetch name from given IMDB ID");
}
}
const ktuvitResultsArray = await this.searchKtuvit(item);
const KtuvitId = await this.findIDInResults(
ktuvitResultsArray,
item.imdbId
);
this.ktuvitIdCache[item.imdbId] = KtuvitId;
this.ktuvitCacheLifetime[item.imdbId] = Date.now();
return KtuvitId;
}
_isKtuvitIdCached(imdbId) {
return (
this.ktuvitIdCache.hasOwnProperty(imdbId) &&
Date.now() - this.ktuvitCacheLifetime[imdbId] < this.CACHE_TTL
);
}
async _getTitleNameFromImdbId(imdbId) {
if (!this._isImdbId(imdbId)) {
return;
}
const getTitleName = new Promise((resolve, reject) => {
//For some reason id based search doesn't work in ktuvit so we fetch the name from Imdb.
{
try {
imdb2name(imdbId, (err, res, inf) => {
try {
if (err) {
reject(err);
}
resolve(inf?.meta?.name);
} catch (err) {
reject(err);
}
});
} catch (err) {
reject(err);
}
}
});
return getTitleName;
}
_isImdbId(imdbId) {
return /ev\d{7}\/\d{4}(-\d)?|(ch|co|ev|nm|tt)\d{7}/?.test(imdbId);
}
/**
* returns Ktuvit ID of a title based on it's imdbID from a results array.
* @param {Array} films
* @param {string} imdbId
*/
findIDInResults(films, imdbId) {
try {
const ktuvitId = films.find((title) => imdbId.includes(title.ImdbID)).ID;
return ktuvitId;
} catch (err) {
return null;
}
}
/**
* return an array of subtitle object for a given episode's ktuvit id.
* @param {string} ktuvitID Ktuvit ID of a title.
* @param {string|number} season
* @param {string|number} episode
* @returns {Array.<subtitle>}
*/
async getSubsIDsListEpisode(ktuvitID, season, episode) {
//bulding the query. A simple query builder string.
var query_string = `moduleName=SubtitlesList&SeriesID=${ktuvitID}&Season=${season}&Episode=${episode}`;
var res = await this.getWithLoginInfo(
KtuvitManager.KTUVIT.EPISODE_INFO_URL + query_string
);
var subtitles = this.extractSubsFromHtml(res.text);
return subtitles || [];
}
/**
* return an array of subtitle object for a given movie's ktuvit id.
* @param {string} ktuvitID Ktuvit ID of a title.
* @returns {Array.<subtitle>}
*/
async getSubsIDsListMovie(ktuvitID) {
var res = await this.getWithLoginInfo(
KtuvitManager.KTUVIT.MOVIE_INFO_URL + ktuvitID
);
var subtitles = this.extractSubsFromHtml(res.text);
return subtitles || [];
}
/**
* extracts subtitles info from html page.
* @typedef subtitle
* @property {string} subName The sub's file name.
* @property {string} id SubId of the sub.
* @property {number} downloads Amount of times the file's been downloaded.
* @property {date} uploadDate when the sub was uploaded.
* @property {string} size File size.
* @property {string} fileType File extension.
* @property {string} credit The credit's section from Ktuvit website.
* @param {string} html
* @returns {Array.<subtitle>}
*/
extractSubsFromHtml(html) {
//The episode html only contains the subtitle rows and since I built this function
//for the movie's html so I need to add the missing information.
html = html.includes("<!DOCTYPE html>")
? html
: `<!DOCTYPE html><table id="subtitlesList"><thead><tr/></thead>${html}</table>`;
var dummyDom = new jsdom.JSDOM(html).window;
var subtitlesListElement =
dummyDom.document.getElementById("subtitlesList");
subtitlesListElement = [...subtitlesListElement.rows];
subtitlesListElement.shift();
// I don't care it's this way, it works.
// I never learned jquery so don't judge me.
var subtitlesIDs = subtitlesListElement.map((sub) => {
//Getting sub's file name from html.
let subName = sub.cells[0]
.querySelector("div")
.innerHTML.split("<br>")[0];
//trimming sub's name
subName = subName.trim();
//Getting the sub's ktuvit id.
let id = sub.cells[5].firstElementChild.getAttribute("data-subtitle-id");
//Amount of times the sub has been downloaded.
let downloads = parseInt(sub.cells[4].innerHTML);
//The sub file's upload date.
let uploadDate = new Date(
sub.cells[3].innerHTML.split("/").reverse().join("-")
);
//The sub file size.
let size = sub.cells[2].innerHTML;
//Sub's file type.
let fileType = sub.cells[1].innerHTML;
//The sub creator
let credit = sub.cells[0].querySelector("div > small").innerHTML;
return {
subName: subName,
id: id,
downloads: downloads,
uploadDate: uploadDate,
size: size,
fileType: fileType,
credit: credit,
};
});
return subtitlesIDs;
}
/**
* DOwnloads the srt and calls the callback function with the srt content.
* @param {string} KtuvitId Title's ktuvit id
* @param {string} subId The sub's id.
* @param {function (buffer, err)} cb The callback function
* @param {object} extraOpts extra options
*/
async downloadSubtitle(KtuvitId, subId, cb, extraOpts) {
const downloadIdentifierRequest = {
FilmID: KtuvitId,
SubtitleID: subId,
FontSize: 0,
FontColor: "",
PredefinedLayout: -1,
};
// To prevent unknown download calls. each download has a one time use token called download identifier.
let downloadIdentifier = await this.postWithLoginInfo(
KtuvitManager.KTUVIT.REQUEST_DOWNLOAD_IDENTIFIER_URL,
downloadIdentifierRequest
);
downloadIdentifier = JSON.parse(
downloadIdentifier.body.d
).DownloadIdentifier;
await superagent
.get(KtuvitManager.KTUVIT.DOWNLOAD_SUB_URL + downloadIdentifier)
.charset("ISO-8859-8", extraOpts?.bytesAmountForDetection)
.withCredentials()
.set(this.headers)
.buffer(true)
.then((res) => {
cb(res.text);
})
.catch((err) => cb(null, err));
}
}
module.exports = KtuvitManager;