undo file identation changes

This commit is contained in:
Felipe Andrada 2020-11-30 01:54:14 -03:00
commit 85db8c5885

View file

@ -9,30 +9,30 @@ function loadVideo(num, cb) {
let masterUrl = list[num].url; let masterUrl = list[num].url;
if (!masterUrl.endsWith("?base64_init=1")) { if (!masterUrl.endsWith("?base64_init=1")) {
masterUrl += "?base64_init=1"; masterUrl += "?base64_init=1";
}
getJson(masterUrl, (err, json) => {
if (err) {
return cb(err);
} }
const videoData = json.video getJson(masterUrl, (err, json) => {
.sort((v1, v2) => v1.avg_bitrate - v2.avg_bitrate) if (err) {
.pop(); return cb(err);
const audioData = json.audio }
.sort((a1, a2) => a1.avg_bitrate - a2.avg_bitrate)
.pop();
const videoBaseUrl = url.resolve( const videoData = json.video
.sort((v1, v2) => v1.avg_bitrate - v2.avg_bitrate)
.pop();
const audioData = json.audio
.sort((a1, a2) => a1.avg_bitrate - a2.avg_bitrate)
.pop();
const videoBaseUrl = url.resolve(
url.resolve(masterUrl, json.base_url), url.resolve(masterUrl, json.base_url),
videoData.base_url videoData.base_url
); );
const audioBaseUrl = url.resolve( const audioBaseUrl = url.resolve(
url.resolve(masterUrl, json.base_url), url.resolve(masterUrl, json.base_url),
audioData.base_url audioData.base_url
); );
processFile( processFile(
"video", "video",
videoBaseUrl, videoBaseUrl,
videoData.init_segment, videoData.init_segment,
@ -41,9 +41,9 @@ getJson(masterUrl, (err, json) => {
err => { err => {
if (err) { if (err) {
return cb(err); return cb(err);
} }
processFile( processFile(
"audio", "audio",
audioBaseUrl, audioBaseUrl,
audioData.init_segment, audioData.init_segment,
@ -52,14 +52,14 @@ getJson(masterUrl, (err, json) => {
err => { err => {
if (err) { if (err) {
return cb(err); return cb(err);
} }
cb(null, num + 1); cb(null, num + 1);
}
);
} }
); );
} });
);
});
} }
function processFile(type, baseUrl, initData, segments, filename, cb) { function processFile(type, baseUrl, initData, segments, filename, cb) {
@ -68,30 +68,30 @@ function processFile(type, baseUrl, initData, segments, filename, cb) {
if(fs.existsSync(downloadingFlag)) { if(fs.existsSync(downloadingFlag)) {
log("⚠️", ` ${filename} - ${type} is incomplete, restarting the download`); log("⚠️", ` ${filename} - ${type} is incomplete, restarting the download`);
} else if (fs.existsSync(filePath)) { } else if (fs.existsSync(filePath)) {
log("⚠️", ` ${filename} - ${type} already exists`); log("⚠️", ` ${filename} - ${type} already exists`);
return cb(); return cb();
} else { } else {
fs.writeFileSync(downloadingFlag, ''); fs.writeFileSync(downloadingFlag, '');
}
const segmentsUrl = segments.map(seg => 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(); const segmentsUrl = segments.map(seg => baseUrl + seg.url);
cb();
}); 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) { function combineSegments(type, i, segmentsUrl, output, filename, downloadingFlag, cb) {
@ -99,43 +99,44 @@ function combineSegments(type, i, segmentsUrl, output, filename, downloadingFlag
fs.unlinkSync(downloadingFlag); fs.unlinkSync(downloadingFlag);
log("🏁", ` ${filename} - ${type} done`); log("🏁", ` ${filename} - ${type} done`);
return cb(); return cb();
} }
log( log(
"📦", "📦",
type === "video" ? "📹" : "🎧", type === "video" ? "📹" : "🎧",
`Downloading ${type} segment ${i}/${segmentsUrl.length} of ${filename}` `Downloading ${type} segment ${i}/${segmentsUrl.length} of ${filename}`
); );
let req = https https
.get(segmentsUrl[i], res => { .get(segmentsUrl[i], res => {
res.on("data", d => output.write(d)); res.on("data", d => output.write(d));
res.on("end", () => res.on("end", () =>
combineSegments(type, i + 1, segmentsUrl, output, filename, downloadingFlag, cb) combineSegments(type, i + 1, segmentsUrl, output, filename, downloadingFlag, cb)
); );
}) })
.on("error", e => { .on("error", e => {
cb(e); cb(e);
}); });
req.setTimeout(7000, function () {
console.log("Timeout. Retrying");
combineSegments(type, i, segmentsUrl, output, filename, downloadingFlag, cb)
})
} }
function getJson(url, cb) { function getJson(url, cb) {
let data = ""; let data = "";
https let req = https
.get(url, res => { .get(url, res => {
res.on("data", d => (data += d)); res.on("data", d => (data += d));
res.on("end", () => cb(null, JSON.parse(data))); res.on("end", () => cb(null, JSON.parse(data)));
}) })
.on("error", e => { .on("error", e => {
cb(e); cb(e);
}); });
req.setTimeout(7000, function () {
console.log("Timeout. Retrying");
combineSegments(type, i, segmentsUrl, output, filename, downloadingFlag, cb)
});
} }
function initJs(n = 0) { function initJs(n = 0) {
@ -145,12 +146,12 @@ function initJs(n = 0) {
if (err) { if (err) {
log("⚠️", ` ${err}`); log("⚠️", ` ${err}`);
return; return;
} }
if (list[num]) { if (list[num]) {
initJs(num); initJs(num);
} }
}); });
} }
initJs(); initJs();