Chenged url fetching method to a class based one

Implemented control on video title in the eventuality it is null or only special characters

Implemented suggestion of issue #11 about sanitizing the title
This commit is contained in:
Luca 2020-03-25 16:37:14 +01:00
parent 005b8e3e31
commit 84d4ff10fc
3 changed files with 40 additions and 9 deletions

View file

@ -5,6 +5,7 @@ import fs from 'fs';
import path from 'path';
import { BrowserTests } from './BrowserTests';
import yargs = require('yargs');
import sanitize = require('sanitize-filename')
// Type in your username here (the one you use to
// login to Microsoft Stream).
@ -91,19 +92,27 @@ async function rentVideoForLater(videoUrls: string[], username: string, outputDi
await sleep(4000);
console.log('Looking up AMS stream locator...');
let amp: any;
// let amp: any;
let document: any;
const amsUrl = await page.evaluate(
() => { return amp.Player.players["vjs_video_3"].cache_.src }
// maybe there should be some check in case the url fetch fails
() => { return document?.querySelector(".azuremediaplayer")?.player?.cache_?.src; }
);
// console.log(`Video url is: ${amsUrl}`);
console.log('Fetching title');
let title = await page.evaluate(
// Clear abuse of null assertion operator,
// someone fix this please
() => { return document!.querySelector(".title")!.textContent!.trim() }
// Using optional chaining to return handle null case, generating default name
() => { return document?.querySelector(".title")?.textContent?.trim() ??
`Video${videoUrls.indexOf(videoUrl)}`; }
);
// Sanitize title
title = title.replace(/"/g, '');
// Implemented sanitize-filename as suggested in issue #11
title = sanitize(title)
if (title == "")
title = `Video${videoUrls.indexOf(videoUrl)}`
console.log(`Video title is: ${title}`);
@ -118,11 +127,11 @@ async function rentVideoForLater(videoUrls: string[], username: string, outputDi
const youtubedlCmd = 'youtube-dl --no-call-home --no-warnings ' + format +
` --output "${outputDirectory}/${title}.mp4" --add-header Cookie:"${cookie}" "${hlsUrl}"`
// console.log(`\n\n[DEBUG] Invoking youtube-dl: ${youtubedlCmd}\n\n`);
var result = execSync(youtubedlCmd, { stdio: 'inherit' });
}
console.log("At this point Chrome's job is done, shutting it down...");
await browser.close();
}

21
package-lock.json generated
View file

@ -546,6 +546,14 @@
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"sanitize-filename": {
"version": "1.6.3",
"resolved": "https://registry.npmjs.org/sanitize-filename/-/sanitize-filename-1.6.3.tgz",
"integrity": "sha512-y/52Mcy7aw3gRm7IrcGDFx/bCk4AhRh2eI9luHOQM86nZsqwiRkkq2GekHXBBD+SmPidc8i2PqtYZl+pWJ8Oeg==",
"requires": {
"truncate-utf8-bytes": "^1.0.0"
}
},
"set-blocking": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
@ -610,6 +618,14 @@
"resolved": "https://registry.npmjs.org/tree-kit/-/tree-kit-0.6.2.tgz",
"integrity": "sha512-95UzJA0EMbFfu5sGUUOoXixQMUGkwu82nGM4lmqLyQl+R4H3FK+lS0nT8TZJ5x7JhSHy+saVn7/AOqh6d+tmOg=="
},
"truncate-utf8-bytes": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz",
"integrity": "sha1-QFkjkJWS1W94pYGENLC3hInKXys=",
"requires": {
"utf8-byte-length": "^1.0.1"
}
},
"typedarray": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz",
@ -625,6 +641,11 @@
"resolved": "https://registry.npmjs.org/uniq/-/uniq-1.0.1.tgz",
"integrity": "sha1-sxxa6CVIRKOoKBVBzisEuGWnNP8="
},
"utf8-byte-length": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/utf8-byte-length/-/utf8-byte-length-1.0.4.tgz",
"integrity": "sha1-9F8VDExm7uloGGUFq5P8u4rWv2E="
},
"util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",

View file

@ -23,6 +23,7 @@
},
"dependencies": {
"puppeteer": "^2.1.1",
"sanitize-filename": "^1.6.3",
"terminal-kit": "^1.35.2",
"typescript": "^3.8.3",
"yargs": "^15.0.3"