From a6bfd54bfd06c57d89ce5456f5483fb14be7e3f2 Mon Sep 17 00:00:00 2001 From: snobu Date: Thu, 9 Apr 2020 12:48:49 +0300 Subject: [PATCH] Added eslint rule: prefer single quotes when possible, prefer double to escaping --- .eslintrc.json | 3 ++- Metadata.ts | 10 +++++----- TokenCache.ts | 6 +++--- destreamer.ts | 22 +++++++++++----------- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 2b15eed..56981f4 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -22,6 +22,7 @@ "rules": { "semi": [2, "always"], "no-unused-vars": "off", - "@typescript-eslint/no-unused-vars": "error" + "@typescript-eslint/no-unused-vars": "error", + "quotes": [2, "single", { "avoidEscape": true }] } } \ No newline at end of file diff --git a/Metadata.ts b/Metadata.ts index 3b8795f..58e7c0e 100644 --- a/Metadata.ts +++ b/Metadata.ts @@ -18,13 +18,13 @@ export async function getVideoMetadata(videoGuids: string[], session: Session): } }); - title = response.data["name"]; - playbackUrl = response.data["playbackUrls"] + title = response.data['name']; + playbackUrl = response.data['playbackUrls'] .filter((item: { [x: string]: string; }) => - item["mimeType"] == "application/vnd.apple.mpegurl") - .map((item: { [x: string]: string }) => { return item["playbackUrl"]; })[0]; + item['mimeType'] == 'application/vnd.apple.mpegurl') + .map((item: { [x: string]: string }) => { return item['playbackUrl']; })[0]; - posterImage = response.data["posterImage"]["medium"]["url"]; + posterImage = response.data['posterImage']['medium']['url']; metadata.push({ title: title, diff --git a/TokenCache.ts b/TokenCache.ts index 48acfc5..8ede85f 100644 --- a/TokenCache.ts +++ b/TokenCache.ts @@ -15,7 +15,7 @@ export class TokenCache { return null; } - let f = fs.readFileSync(tokenCacheFile, "utf8"); + let f = fs.readFileSync(tokenCacheFile, 'utf8'); j = JSON.parse(f); interface Jwt { @@ -25,7 +25,7 @@ export class TokenCache { const decodedJwt: Jwt = jwtDecode(j.AccessToken); let now = Math.floor(Date.now() / 1000); - let exp = decodedJwt["exp"]; + let exp = decodedJwt['exp']; let timeLeft = exp - now; let timeLeftInMinutes = Math.floor(timeLeft / 60); @@ -48,7 +48,7 @@ export class TokenCache { public Write(session: Session): void { let s = JSON.stringify(session, null, 4); - fs.writeFile(".token_cache", s, (err: any) => { + fs.writeFile('.token_cache', s, (err: any) => { if (err) { return console.error(err); } diff --git a/destreamer.ts b/destreamer.ts index 31ff1a7..9509653 100644 --- a/destreamer.ts +++ b/destreamer.ts @@ -26,17 +26,17 @@ const argv = yargs.options({ username: { type: 'string', demandOption: false }, outputDirectory: { type: 'string', alias: 'outputdirectory', default: 'videos' }, format: { - alias:"f", + alias: 'f', describe: `Expose youtube-dl --format option, for details see\n https://github.com/ytdl-org/youtube-dl/blob/master/README.md#format-selection`, - type:'string', + type: 'string', demandOption: false }, simulate: { - alias: "s", + alias: 's', describe: `If this is set to true no video will be downloaded and the script will log the video info (default: false)`, - type: "boolean", + type: 'boolean', default: false, demandOption: false }, @@ -91,7 +91,7 @@ async function DoInteractiveLogin(username?: string): Promise { console.log('Navigating to microsoftonline.com login page...'); // This breaks on slow connections, needs more reliable logic - await page.goto('https://web.microsoftstream.com', { waitUntil: "networkidle2" }); + await page.goto('https://web.microsoftstream.com', { waitUntil: 'networkidle2' }); await page.waitForSelector('input[type="email"]'); if (username) { @@ -108,7 +108,7 @@ async function DoInteractiveLogin(username?: string): Promise { console.info('Got cookie. Consuming cookie...'); await sleep(4000); - console.info("Calling Microsoft Stream API..."); + console.info('Calling Microsoft Stream API...'); let sessionInfo: any; let session = await page.evaluate( @@ -122,7 +122,7 @@ async function DoInteractiveLogin(username?: string): Promise { ); tokenCache.Write(session); - console.info("Wrote access token to token cache."); + console.info('Wrote access token to token cache.'); console.log(`ApiGatewayUri: ${session.ApiGatewayUri}`); console.log(`ApiGatewayVersion: ${session.ApiGatewayVersion}`); @@ -144,7 +144,7 @@ function extractVideoGuid(videoUrls: string[]): string[] { else urls = videoUrls as string[]; let videoGuids: string[] = []; - let guid: string | undefined = ""; + let guid: string | undefined = ''; for (let url of urls) { console.log(url); try { @@ -169,7 +169,7 @@ async function downloadVideo(videoUrls: string[], outputDirectory: string, sessi console.log(videoUrls); const videoGuids = extractVideoGuid(videoUrls); - console.log("Fetching title and HLS URL..."); + console.log('Fetching title and HLS URL...'); let metadata: Metadata[] = await getVideoMetadata(videoGuids, session); await Promise.all(metadata.map(async video => { video.title = sanitize(video.title); @@ -179,13 +179,13 @@ async function downloadVideo(videoUrls: string[], outputDirectory: string, sessi await drawThumbnail(video.posterImage, session.AccessToken); console.log('Spawning youtube-dl with cookie and HLS URL...'); - const format = argv.format ? `-f "${argv.format}"` : ""; + const format = argv.format ? `-f "${argv.format}"` : ''; var youtubedlCmd = 'youtube-dl --no-call-home --no-warnings ' + format + ` --output "${outputDirectory}/${video.title}.mp4" --add-header ` + `Authorization:"Bearer ${session.AccessToken}" "${video.playbackUrl}"`; if (argv.simulate) { - youtubedlCmd = youtubedlCmd + " -s"; + youtubedlCmd = youtubedlCmd + ' -s'; } execSync(youtubedlCmd, { stdio: 'inherit' });