diff --git a/destreamer.ts b/destreamer.ts index 45804fb..a700d4d 100644 --- a/destreamer.ts +++ b/destreamer.ts @@ -77,7 +77,11 @@ function init() { console.info(colors.blue("There will be no video downloaded, it's only a simulation\n")); } -async function DoInteractiveLogin(username?: string): Promise { +async function DoInteractiveLogin(url: string, username?: string): Promise { + + let videoId = url.split("/").pop() ?? ( + console.log('Couldn\'t split the video Id from the first videoUrl'), process.exit(25) + ); console.log('Launching headless Chrome to perform the OpenID Connect dance...'); const browser = await puppeteer.launch({ @@ -85,10 +89,9 @@ async function DoInteractiveLogin(username?: string): Promise { args: ['--disable-dev-shm-usage'] }); const page = (await browser.pages())[0]; - console.log('Navigating to microsoftonline.com login page...'); + console.log('Navigating to login page...'); - // This breaks on slow connections, needs more reliable logic - await page.goto('https://web.microsoftstream.com', { waitUntil: 'networkidle2' }); + await page.goto(url, { waitUntil: 'load' }); await page.waitForSelector('input[type="email"]'); if (username) { @@ -96,16 +99,8 @@ async function DoInteractiveLogin(username?: string): Promise { await page.click('input[type="submit"]'); } - await browser.waitForTarget(target => target.url().includes('microsoftstream.com/'), { timeout: 90000 }); + await browser.waitForTarget(target => target.url().includes(videoId), { timeout: 150000 }); console.info('We are logged in.'); - // We may or may not need to sleep here. - // Who am i to deny a perfectly good nap? - await sleep(1500); - - console.info('Got cookie. Consuming cookie...'); - - await sleep(4000); - console.info('Calling Microsoft Stream API...'); let sessionInfo: any; let session = await page.evaluate( @@ -119,12 +114,9 @@ async function DoInteractiveLogin(username?: string): Promise { ); tokenCache.Write(session); - console.info('Wrote access token to token cache.'); + console.log('Wrote access token to token cache.'); + console.log("At this point Chromium's job is done, shutting it down...\n"); - console.log(`ApiGatewayUri: ${session.ApiGatewayUri}`); - console.log(`ApiGatewayVersion: ${session.ApiGatewayVersion}`); - - console.info("At this point Chromium's job is done, shutting it down..."); await browser.close(); return session; @@ -241,7 +233,7 @@ async function main() { let session = tokenCache.Read(); if (session == null) { - session = await DoInteractiveLogin(argv.username); + session = await DoInteractiveLogin(videoUrls[0], argv.username); }