Fix login for private tenants, improved refresh reliability (#181)

This commit is contained in:
lukaarma 2020-07-19 09:50:23 +02:00 committed by GitHub
parent 0be6ad8b60
commit 78e5818cc3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 8 deletions

View file

@ -53,8 +53,8 @@ export class TokenCache {
} }
export async function refreshSession(): Promise<Session> { export async function refreshSession(url: string): Promise<Session> {
const url = 'https://web.microsoftstream.com'; const videoId: string = url.split('/').pop() ?? process.exit(ERROR_CODE.INVALID_VIDEO_GUID);
const browser: puppeteer.Browser = await puppeteer.launch({ const browser: puppeteer.Browser = await puppeteer.launch({
executablePath: getPuppeteerChromiumPath(), executablePath: getPuppeteerChromiumPath(),
@ -70,7 +70,7 @@ export async function refreshSession(): Promise<Session> {
const page: puppeteer.Page = (await browser.pages())[0]; const page: puppeteer.Page = (await browser.pages())[0];
await page.goto(url, { waitUntil: 'load' }); 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 session: Session | null = null;
let tries = 1; let tries = 1;

View file

@ -44,7 +44,6 @@ async function init(): Promise<void> {
async function DoInteractiveLogin(url: string, username?: string): Promise<Session> { async function DoInteractiveLogin(url: string, username?: string): Promise<Session> {
const videoId: string = url.split('/').pop() ?? process.exit(ERROR_CODE.INVALID_VIDEO_GUID);
logger.info('Launching headless Chrome to perform the OpenID Connect dance...'); logger.info('Launching headless Chrome to perform the OpenID Connect dance...');
@ -81,7 +80,7 @@ async function DoInteractiveLogin(url: string, username?: string): Promise<Sessi
remember the credentials or it could still prompt the user for a password */ remember the credentials or it could still prompt the user for a password */
} }
await browser.waitForTarget((target: puppeteer.Target) => 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.'); logger.info('We are logged in.');
let session: Session | null = null; let session: Session | null = null;
@ -142,16 +141,16 @@ async function downloadVideo(videoGUIDs: Array<string>, outputDirectories: Array
return; return;
} }
for (const video of videos) { for (const [index, video] of videos.entries()) {
if (argv.skip && fs.existsSync(video.outPath)) { if (argv.skip && fs.existsSync(video.outPath)) {
logger.info(`File already exists, skipping: ${video.outPath} \n`); logger.info(`File already exists, skipping: ${video.outPath} \n`);
continue; continue;
} }
if (argv.keepLoginCookies) { if (argv.keepLoginCookies && index !== 0) {
logger.info('Trying to refresh token...'); 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({ const pbar: cliProgress.SingleBar = new cliProgress.SingleBar({