From 84d4ff10fcddbea2b4b90784a2fe41285e296a2d Mon Sep 17 00:00:00 2001 From: Luca Date: Wed, 25 Mar 2020 16:37:14 +0100 Subject: [PATCH 1/2] 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 --- destreamer.ts | 27 ++++++++++++++++++--------- package-lock.json | 21 +++++++++++++++++++++ package.json | 1 + 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/destreamer.ts b/destreamer.ts index 1b858ca..cbc24ac 100644 --- a/destreamer.ts +++ b/destreamer.ts @@ -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(); } diff --git a/package-lock.json b/package-lock.json index 0426e9b..d8cf839 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index e2c112a..a6a1e08 100644 --- a/package.json +++ b/package.json @@ -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" From 5888e705cce1be184ffadafa3e20c229ec6654f3 Mon Sep 17 00:00:00 2001 From: Luca Date: Wed, 25 Mar 2020 16:41:00 +0100 Subject: [PATCH 2/2] excluded .txt files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f666fa1..135af8f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.txt *.log *.js node_modules