-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathindex.js
178 lines (150 loc) · 4.21 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
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
const fs = require("fs");
const url = require("url");
const https = require("https");
const log = (...args) => console.log("→", ...args);
const list = require("./videojson.js");
function loadVideo(num, cb) {
let rawMasterUrl = new URL(list[num].url);
let masterUrl = rawMasterUrl.toString();
getJson(masterUrl, num, (err, json) => {
if (err) {
return cb(err);
}
const videoData = json.video
.sort((v1, v2) => v1.avg_bitrate - v2.avg_bitrate)
.pop();
let audioData = {}
if (json.audio !== null) {
audioData = json.audio
.sort((a1, a2) => a1.avg_bitrate - a2.avg_bitrate)
.pop();
}
const videoBaseUrl = url.resolve(
url.resolve(masterUrl, json.base_url),
videoData.base_url
);
let audioBaseUrl = "";
if (json.audio !== null) {
audioBaseUrl = url.resolve(
url.resolve(masterUrl, json.base_url),
audioData.base_url
);
}
processFile(
"video",
videoBaseUrl,
videoData.init_segment,
videoData.segments,
list[num].name + ".m4v",
err => {
if (err) {
cb(err);
}
if (json.audio !== null) {
processFile(
"audio",
audioBaseUrl,
audioData.init_segment,
audioData.segments,
list[num].name + ".m4a",
err => {
if (err) {
cb(err);
}
cb(null, num + 1);
}
);
}
}
);
});
}
function processFile(type, baseUrl, initData, segments, filename, cb) {
const file = filename.replace(/[^\w.]/gi, '-');
const filePath = `./parts/${file}`;
const downloadingFlag = `./parts/.${file}~`;
if (fs.existsSync(downloadingFlag)) {
log("⚠️", ` ${file} - ${type} is incomplete, restarting the download`);
} else if (fs.existsSync(filePath)) {
log("⚠️", ` ${file} - ${type} already exists`);
cb();
} else {
fs.writeFileSync(downloadingFlag, '');
}
const segmentsUrl = segments.map(seg => {
if (!seg.url) {
throw new Error(`found a segment with an empty url: ${JSON.stringify(seg)}`);
}
return baseUrl + seg.url;
});
const initBuffer = Buffer.from(initData, "base64");
fs.writeFileSync(filePath, initBuffer);
const output = fs.createWriteStream(filePath, {
flags: "a"
});
combineSegments(type, 0, segmentsUrl, output, filePath, downloadingFlag, err => {
if (err) {
log("⚠️", ` ${err}`);
}
output.end();
cb();
});
}
function combineSegments(type, i, segmentsUrl, output, filename, downloadingFlag, cb) {
if (i >= segmentsUrl.length) {
if (fs.existsSync(downloadingFlag)) {
fs.unlinkSync(downloadingFlag);
}
log("🏁", ` ${filename} - ${type} done`);
return cb();
}
log(
"📦",
type === "video" ? "📹" : "🎧",
`Downloading ${type} segment ${i}/${segmentsUrl.length} of ${filename}`
);
let req = https
.get(segmentsUrl[i], res => {
if (res.statusCode != 200) {
cb(new Error(`Downloading segment with url '${segmentsUrl[i]}' failed with status: ${res.statusCode} ${res.statusMessage}`))
}
res.on("data", d => output.write(d));
res.on("end", () =>
combineSegments(type, i + 1, segmentsUrl, output, filename, downloadingFlag, cb)
);
})
.on("error", e => {
cb(e);
});
req.setTimeout(7000, function () {
log("⚠️", 'Timeout. Retrying');
combineSegments(type, i, segmentsUrl, output, filename, downloadingFlag, cb);
});
}
function getJson(url, n, cb) {
let data = "";
https
.get(url, res => {
if (res.statusMessage.toLowerCase() !== 'gone') {
res.on("data", d => (data += d));
res.on("end", () => cb(null, JSON.parse(data)));
} else {
return cb(`The master.json file is expired or crushed. Please update or remove it from the sequence (broken on ` + n + ` position)`);
}
})
.on("error", e => {
return cb(e);
});
}
function initJs(n = 0) {
if (!list[n] || (!list[n].name && !list[n].url)) return;
loadVideo(n, (err, num) => {
if (err) {
log("⚠️", ` ${err}`);
}
if (list[num]) {
initJs(num);
}
});
}
initJs();