-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·90 lines (78 loc) · 2.25 KB
/
main.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
#!/usr/bin/env node
const addic7edApi = require("addic7ed-api");
const flags = require("flags");
flags.defineString(
"lang",
"eng",
"Subtitles languege or code (case insensitive)"
);
flags.defineString("show", "", "Show name");
flags.defineNumber("s", -1, "Season");
flags.defineNumber("e", -1, "Episode");
flags.defineBoolean("hi", false, "Hearing impaired");
flags.parse();
if (flags.get("show") == "") {
console.log("Must provide show name");
console.log("");
flags.help();
process.exit(1);
}
if (flags.get("s") == -1) {
console.log("Must provide a season");
console.log("");
flags.help();
process.exit(1);
}
if (flags.get("e") == -1) {
console.log("Must provide an episode");
console.log("");
flags.help();
process.exit(1);
}
const show = flags.get("show");
const season = "" + flags.get("s");
const episode = "" + flags.get("e");
const hearingImpaired = flags.get("hi");
const lang = flags.get("lang").toLowerCase();
(async () => {
const subs = await addic7edApi.search(show, season, episode);
if (!subs || !subs.length) {
console.error("Found nothing for", show, season, episode);
process.exit(1);
}
subs.sort((a, b) => b.link > a.link); // Newest versions first
const alreadyDownloaded = new Set();
for (let subInfo of subs) {
if (
(subInfo.langId.toLowerCase() != lang &&
subInfo.lang.toLowerCase() != lang) ||
hearingImpaired != subInfo.hearingImpaired
) {
continue;
}
let fileName = `${show.replace(/ /g, ".")}.`;
fileName += `S${season.length == 1 ? `0${season}` : season}`;
fileName += `E${episode.length == 1 ? `0${episode}` : episode}.`;
fileName += `${subInfo.distribution}.`;
fileName += `${subInfo.version}.`;
fileName += `${subInfo.team}${subInfo.hearingImpaired ? ".HI" : ""}.srt`;
fileName = fileName.replace(/\//g, "-");
if (alreadyDownloaded.has(fileName)) {
continue;
}
console.log("Downloading", fileName);
let tries = 5;
while (tries > 0) {
tries--;
try {
await addic7edApi.download(subInfo, "./" + fileName);
} catch (e) {
if (tries == 0) {
console.log("Failed to download", fileName, "(5 retries)");
}
}
break;
}
alreadyDownloaded.add(fileName);
}
})();