-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
121 lines (115 loc) · 4.03 KB
/
index.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
/**
* Getting information about film by ID (IMDb, TMDb, Kinopoisk, Douban)
*
* @param {Object|String|Number} id
* @param {String|Number|Array} [season]
* @param {String} [key]
* @return {Object}
*/
module.exports = (id, season, key) => {
if (!id) return Promise.resolve(null);
let params = input(id, season, key);
if (!params) return Promise.resolve(null);
params.gs = true;
return Promise.resolve(params)
.then(require('./lib/1_missing'))
.then(require('./lib/2_urls'))
.then(require('./lib/3_raw'))
.then(require('./lib/4_processed'))
.then(require('./lib/5_output'))
.then(require('./lib/6_validation'))
};
module.exports.id = (id, season, key) => {
if (!id) return Promise.resolve(null);
let params = input(id, season, key);
if (!params) return Promise.resolve(null);
return Promise.resolve(params)
.then(require('./lib/1_missing'))
.then(require('./lib/2_urls'))
.then(require('./lib/3_raw'))
.then(require('./lib/4_processed'))
.then(require('./lib/5_output'))
.then(require('./lib/6_validation'))
};
const input = (id, season, key) => {
if (!id) return null;
let params = {};
if (typeof id === 'string' || typeof id === 'number') {
let domain = typeof id === 'string'
? id.replace(/[^a-z]/g, '')
? id.replace(/[^a-z]/g, '')
: 'imdb'
: 'tmdb';
id = typeof id === 'number'
? id.toString()
: id.replace(/[^0-9]/g, '');
switch (domain) {
case 'kp':
params.kp_id = id;
break;
case 'douban':
params.douban_id = id;
break;
case 'tmdb':
params.tmdb_id = id;
if (key) params.tmdb_key = key;
break;
case 'imdb':
case 'tt':
params.imdb_id = id;
if (key) params.imdb_key = key;
break;
}
if (typeof season !== 'undefined' && season !== null) {
params.season = typeof season === 'number'
? [season.toString()]
: typeof season === 'string'
? season
.split(',')
.map(s => s.replace(/[^0-9]/g, ''))
.filter(Boolean)
: typeof season === 'object'
? season
.map(s => s.toString().replace(/[^0-9]/g, ''))
.filter(Boolean)
: season;
}
} else if (typeof id === 'object') {
Object.keys(id).forEach(i => {
if (['imdb_id', 'tmdb_id', 'kp_id', 'douban_id'].indexOf(i) + 1) {
id[i] = typeof id[i] === 'number'
? id[i].toString()
: id[i].replace(/[^0-9]/g, '');
}
if (i === 'season') {
id[i] = typeof id[i] === 'number'
? [id[i].toString()]
: typeof id[i] === 'string'
? id[i]
.split(',')
.map(se => se.replace(/[^0-9]/g, ''))
.filter(Boolean)
: typeof id[i] === 'object'
? id[i]
.map(se => se.toString().replace(/[^0-9]/g, ''))
.filter(Boolean)
: id[i];
}
if (i === 'cookie' && typeof id[i] === 'object') {
Object.keys(id[i]).forEach(domain => {
let c = id[i][domain].map(cookie => {
return cookie.name + '=' + cookie.value;
});
id[i][domain] = c.join(';');
});
}
if (id[i]) {
params[i] = id[i];
}
});
}
else {
return null;
}
return params;
};