1
0
mirror of https://github.com/snobu/destreamer.git synced 2026-02-11 17:19:42 +00:00

Still a way long to go

This commit is contained in:
snobu
2020-03-31 12:41:21 +03:00
parent 7c0a2b53ce
commit b20bbe0b5e

View File

@@ -86,33 +86,18 @@ function sanityChecks() {
} }
} }
async function rentVideoForLater(videoUrls: string[], outputDirectory: string, username?: string) {
let accessToken = null;
try {
accessToken = tokenCache.Read();
console.log(`Read returned: ${accessToken}`);
process.exit(200);
}
catch (e)
{
console.log("cache is empty or expired");
console.log(accessToken);
process.exit(404);
}
async function DoInteractiveLogin(username?: string) {
console.log('Launching headless Chrome to perform the OpenID Connect dance...'); console.log('Launching headless Chrome to perform the OpenID Connect dance...');
const browser = await puppeteer.launch({ const browser = await puppeteer.launch({
// Switch to false if you need to login interactively
headless: false, headless: false,
args: ['--disable-dev-shm-usage'] args: ['--disable-dev-shm-usage']
}); });
const page = (await browser.pages())[0]; const page = (await browser.pages())[0];
console.log('Navigating to STS login page...'); console.log('Navigating to microsoftonline.com login page...');
// This breaks on slow connections, needs more reliable logic // This breaks on slow connections, needs more reliable logic
await page.goto(videoUrls[0], { waitUntil: "networkidle2" }); await page.goto('https://web.microsoftstream.com', { waitUntil: "networkidle2" });
await page.waitForSelector('input[type="email"]'); await page.waitForSelector('input[type="email"]');
if (username) { if (username) {
@@ -126,41 +111,70 @@ async function rentVideoForLater(videoUrls: string[], outputDirectory: string, u
// Who am i to deny a perfectly good nap? // Who am i to deny a perfectly good nap?
await sleep(1500); await sleep(1500);
for (let videoUrl of videoUrls) {
let videoID = videoUrl.split('/').pop() ??
(console.error("Couldn't split the videoID, wrong url"), process.exit(25));
// changed waitUntil value to load (page completly loaded)
await page.goto(videoUrl, { waitUntil: 'load' });
await sleep(2000);
// try this instead of hardcoding sleep
// https://github.com/GoogleChrome/puppeteer/issues/3649
const cookie = await exfiltrateCookie(page);
console.log('Got cookie. Consuming cookie...'); console.log('Got cookie. Consuming cookie...');
await sleep(4000); await sleep(4000);
console.log("Calling Microsoft Stream API..."); console.log("Calling Microsoft Stream API...");
let cookie = await exfiltrateCookie(page);
let sessionInfo: any; let sessionInfo: any;
let session = await page.evaluate( let session = await page.evaluate(
() => { () => {
return { return {
AccessToken: sessionInfo.AccessToken, AccessToken: sessionInfo.AccessToken,
ApiGatewayUri: sessionInfo.ApiGatewayUri, ApiGatewayUri: sessionInfo.ApiGatewayUri,
ApiGatewayVersion: sessionInfo.ApiGatewayVersion ApiGatewayVersion: sessionInfo.ApiGatewayVersion,
Cookie: cookie
}; };
} }
); );
tokenCache.Write(session.AccessToken); tokenCache.Write(session.AccessToken);
console.log("Wrote access token to token cache.");
console.log(`ApiGatewayUri: ${session.ApiGatewayUri}`); console.log(`ApiGatewayUri: ${session.ApiGatewayUri}`);
console.log(`ApiGatewayVersion: ${session.ApiGatewayVersion}`); console.log(`ApiGatewayVersion: ${session.ApiGatewayVersion}`);
console.log("At this point Chromium's job is done, shutting it down...");
await browser.close();
return session;
}
function extractVideoGuid(videoUrls: string[]): string[] {
let videoGuids: string[] = [];
let guid: string = "";
for (let url of videoUrls) {
try {
let guid = url.split('/').pop();
}
catch (e)
{
console.error(`Could not split the video GUID from URL: ${e.message}`);
process.exit(25);
}
videoGuids.push(guid);
}
return videoGuids;
}
async function rentVideoForLater(videoUrls: string[], outputDirectory: string, session: object) {
const videoGuids = extractVideoGuid(videoUrls);
let accessToken = null;
try {
accessToken = tokenCache.Read();
}
catch (e)
{
console.log("Cache is empty or expired, performing interactive log on...");
}
console.log("Fetching title and HLS URL..."); console.log("Fetching title and HLS URL...");
var [title, hlsUrl] = await getVideoInfo(videoID, session); var [title, hlsUrl] = await getVideoMetadata(videoGuids, session);
title = (sanitize(title) == "") ? title = (sanitize(title) == "") ?
`Video${videoUrls.indexOf(videoUrl)}` : `Video${videoUrls.indexOf(videoUrl)}` :
@@ -175,7 +189,7 @@ async function rentVideoForLater(videoUrls: string[], outputDirectory: string, u
var youtubedlCmd = 'youtube-dl --no-call-home --no-warnings ' + format + var youtubedlCmd = 'youtube-dl --no-call-home --no-warnings ' + format +
` --output "${outputDirectory}/${title}.mp4" --add-header ` + ` --output "${outputDirectory}/${title}.mp4" --add-header ` +
`Cookie:"${cookie}" "${hlsUrl}"`; `Cookie:"${session.AccessToken}" "${hlsUrl}"`;
if (argv.simulate) { if (argv.simulate) {
youtubedlCmd = youtubedlCmd + " -s"; youtubedlCmd = youtubedlCmd + " -s";
@@ -187,14 +201,12 @@ async function rentVideoForLater(videoUrls: string[], outputDirectory: string, u
execSync(youtubedlCmd, { stdio: 'inherit' }); execSync(youtubedlCmd, { stdio: 'inherit' });
} }
console.log("At this point Chrome's job is done, shutting it down...");
await browser.close();
}
function sleep(ms: number) { function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
async function exfiltrateCookie(page: puppeteer.Page) { async function exfiltrateCookie(page: puppeteer.Page) {
var jar = await page.cookies("https://.api.microsoftstream.com"); var jar = await page.cookies("https://.api.microsoftstream.com");
var authzCookie = jar.filter(c => c.name === 'Authorization_Api')[0]; var authzCookie = jar.filter(c => c.name === 'Authorization_Api')[0];
@@ -215,14 +227,19 @@ async function exfiltrateCookie(page: puppeteer.Page) {
return `Authorization=${authzCookie.value}; Signature=${sigCookie.value}`; return `Authorization=${authzCookie.value}; Signature=${sigCookie.value}`;
} }
class Metadata {
title!: string;
hlsUrl!: string;
playbackUrl!: string;
}
async function getVideoInfo(videoID: string, session: any) { async function getVideoMetadata(videoGuids: string[], session: any) {
let title: string; let title: string;
let hlsUrl: string; let hlsUrl: string;
let metadata = new Metadata();
videoGuids.forEach(guid => {
let content = axios.get( let content = axios.get(
`${session.ApiGatewayUri}videos/${videoID}` + `${session.ApiGatewayUri}videos/${guid}?api-version=${session.ApiGatewayVersion}`,
`?$expand=creator,tokens,status,liveEvent,extensions&api-version=${session.ApiGatewayVersion}`,
{ {
headers: { headers: {
Authorization: `Bearer ${session.AccessToken}` Authorization: `Bearer ${session.AccessToken}`
@@ -269,6 +286,7 @@ async function getVideoInfo(videoID: string, session: any) {
}); });
return [title, hlsUrl]; return [title, hlsUrl];
});
} }
// We should probably use Mocha or something // We should probably use Mocha or something