-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
168 lines (160 loc) · 5.6 KB
/
index.js
File metadata and controls
168 lines (160 loc) · 5.6 KB
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
const pt = require('parse-torrent');
const fe = require('file-exists');
const mt = require('mime-types');
const rc = require('read-chunk');
const wt = require('webtorrent');
const ft = require('file-type');
const cr = require('crypto');
const pa = require('path');
const fs = require('fs');
const info_about_video = input => {
return new Promise(resolve => {
if (fe.sync(input)) {
let m = ft(rc.sync(input, 0, ft.minimumBytes));
if (m && m.mime && /video/i.test(m.mime)) {
let item = {};
let {size} = fs.statSync(input);
item.path = input;
item.size = size;
item.file = pa.basename(input);
item.sha1 = cr
.createHash('sha1')
.update(fs.readFileSync(input))
.digest('hex');
item = {...item, ...get_episode_by_name(input)};
return resolve([item]);
} else {
pt.remote(input, err => {
if (err) {
//console.error('This file is not a video/torrent');
return resolve([]);
}
return get_episode_by_torrent(input)
.then(resolve);
});
}
} else if (/(^http|^magnet|[a-z0-9]{40})/i.test(input)) {
pt.remote(input, err => {
if (err) {
//console.error('This string is not a video/torrent');
return resolve([]);
}
return get_episode_by_torrent(input)
.then(resolve);
});
} else {
//console.error('This is not a video/torrent');
return resolve([]);
}
});
};
const get_episode_by_name = name => {
if (!is_video_by_name(name)) return {};
let res = {};
let seasons = name.match(/[^A-Z0-9]S([0-9]{1,2})[^A-DF-Z0-9]/i);
let episodes = name.match(/S([0-9]{1,2})[^A-DF-Z0-9]([0-9]{1,3})[^A-Z0-9]/i);
if (!seasons && !episodes) {
seasons = name.match(/SEASON[^A-Z0-9]([0-9]{1,2}|one|two|three|four|five|six|seven|eight|nine|ten)[^A-Z0-9]/i);
episodes = name.match(/SEASON[^A-Z0-9][A-Z0-9]{1,5}[^A-Z0-9]([0-9]{1,3})of[0-9]{1,3}/i);
if (seasons && seasons.length && seasons[1].replace(/[^0-9]/ig, '') === '') {
'one|two|three|four|five|six|seven|eight|nine|ten'
.split('|')
.forEach((s, i) => {
if (seasons[1] === s) {
seasons[1] = (i + 1).toString();
}
});
}
if (!seasons && !episodes) {
seasons = name.match(/SERIES[^A-Z0-9]([0-9]{1,3})[^A-Z0-9][0-9]{1,3}of[0-9]{1,3}/i);
episodes = name.match(/SERIES[^A-Z0-9][0-9]{1,3}[^A-Z0-9]([0-9]{1,3})of[0-9]{1,3}/i);
}
}
if (seasons && seasons[1]) {
res.season = parseInt(seasons[1]);
}
if (episodes && episodes[2]) {
if (episodes[1] && !res.season) {
res.season = parseInt(episodes[1]);
}
res.episode = parseInt(episodes[2]);
}
return (typeof res.season !== 'undefined' || typeof res.episode !== 'undefined') ? res : {};
};
const get_episode_by_torrent = torrent => {
return new Promise(resolve => {
let list = [];
const cl = new wt();
let t = cl.add(torrent);
let i = 1;
let meta = setInterval(() => {
i++;
if (i >= 10) {
clearInterval(meta);
try {
cl.destroy(err => err ? console.error(err) : '');
} catch (e) {
}
return resolve(list);
}
}, 2000);
t.on('metadata', () => {
clearInterval(meta);
t.files.forEach(info => {
let item = {};
item.size = info.length || '';
item.file = info.name || '';
item.hash = info._torrent.infoHash || '';
if (!is_video_by_name(item.file)) return {};
item = {...item, ...get_episode_by_name(item.file)};
list.push(item);
});
try {
cl.destroy(err => err ? console.error(err) : '');
} catch (e) {
}
return resolve(list);
});
});
};
const is_video_by_name = name => {
return /video/i.test(mt.lookup(name) || '') &&
!(/trailer|teaser|preview|sample/i.test(name));
};
const is_video = input => {
return info_about_video(input)
.then(list => {
if (list && list.length && list[0].sha1) {
return Promise.resolve(true);
}
return Promise.resolve(false);
});
};
const is_torrent = input => {
return info_about_video(input)
.then(list => {
if (list && list.length && list[0].hash) {
return Promise.resolve(true);
}
return Promise.resolve(false);
});
};
const is_video_torrent = input => {
return Promise.all([
is_video(input),
is_torrent(input)
]).then(res => {
let [video, torrent] = res;
if (video || torrent) {
return Promise.resolve(true);
}
return Promise.resolve(false);
});
};
module.exports.gebt = get_episode_by_torrent;
module.exports.gebn = get_episode_by_name;
module.exports.ivbn = is_video_by_name;
module.exports.isvt = is_video_torrent;
module.exports.isvi = is_video;
module.exports.isto = is_torrent;
module.exports.info = info_about_video;