From 78e5818cc3d6bca1dd4b3de761b56c47fefa15f2 Mon Sep 17 00:00:00 2001 From: lukaarma Date: Sun, 19 Jul 2020 09:50:23 +0200 Subject: [PATCH] Fix login for private tenants, improved refresh reliability (#181) --- src/TokenCache.ts | 6 +++--- src/destreamer.ts | 9 ++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/TokenCache.ts b/src/TokenCache.ts index 020ffab..d46360e 100644 --- a/src/TokenCache.ts +++ b/src/TokenCache.ts @@ -53,8 +53,8 @@ export class TokenCache { } -export async function refreshSession(): Promise { - const url = 'https://web.microsoftstream.com'; +export async function refreshSession(url: string): Promise { + const videoId: string = url.split('/').pop() ?? process.exit(ERROR_CODE.INVALID_VIDEO_GUID); const browser: puppeteer.Browser = await puppeteer.launch({ executablePath: getPuppeteerChromiumPath(), @@ -70,7 +70,7 @@ export async function refreshSession(): Promise { const page: puppeteer.Page = (await browser.pages())[0]; await page.goto(url, { waitUntil: 'load' }); - await browser.waitForTarget((target: puppeteer.Target) => target.url().includes(url), { timeout: 30000 }); + await browser.waitForTarget((target: puppeteer.Target) => target.url().includes(videoId), { timeout: 30000 }); let session: Session | null = null; let tries = 1; diff --git a/src/destreamer.ts b/src/destreamer.ts index 12f3a26..4e4beef 100644 --- a/src/destreamer.ts +++ b/src/destreamer.ts @@ -44,7 +44,6 @@ async function init(): Promise { async function DoInteractiveLogin(url: string, username?: string): Promise { - const videoId: string = url.split('/').pop() ?? process.exit(ERROR_CODE.INVALID_VIDEO_GUID); logger.info('Launching headless Chrome to perform the OpenID Connect dance...'); @@ -81,7 +80,7 @@ async function DoInteractiveLogin(url: string, username?: string): Promise target.url().includes(videoId), { timeout: 150000 }); + await browser.waitForTarget((target: puppeteer.Target) => target.url().includes('microsoftstream.com'), { timeout: 150000 }); logger.info('We are logged in.'); let session: Session | null = null; @@ -142,16 +141,16 @@ async function downloadVideo(videoGUIDs: Array, outputDirectories: Array return; } - for (const video of videos) { + for (const [index, video] of videos.entries()) { if (argv.skip && fs.existsSync(video.outPath)) { logger.info(`File already exists, skipping: ${video.outPath} \n`); continue; } - if (argv.keepLoginCookies) { + if (argv.keepLoginCookies && index !== 0) { logger.info('Trying to refresh token...'); - session = await refreshSession(); + session = await refreshSession('https://web.microsoftstream.com/video/' + videoGUIDs[index]); } const pbar: cliProgress.SingleBar = new cliProgress.SingleBar({