mirror of
https://github.com/snobu/destreamer.git
synced 2026-03-26 21:39:07 +00:00
Still a way long to go
This commit is contained in:
100
destreamer.ts
100
destreamer.ts
@@ -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...');
|
||||
const browser = await puppeteer.launch({
|
||||
// Switch to false if you need to login interactively
|
||||
headless: false,
|
||||
args: ['--disable-dev-shm-usage']
|
||||
});
|
||||
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
|
||||
await page.goto(videoUrls[0], { waitUntil: "networkidle2" });
|
||||
await page.goto('https://web.microsoftstream.com', { waitUntil: "networkidle2" });
|
||||
await page.waitForSelector('input[type="email"]');
|
||||
|
||||
if (username) {
|
||||
@@ -126,41 +111,70 @@ async function rentVideoForLater(videoUrls: string[], outputDirectory: string, u
|
||||
// Who am i to deny a perfectly good nap?
|
||||
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...');
|
||||
|
||||
await sleep(4000);
|
||||
console.log("Calling Microsoft Stream API...");
|
||||
|
||||
let cookie = await exfiltrateCookie(page);
|
||||
|
||||
let sessionInfo: any;
|
||||
let session = await page.evaluate(
|
||||
() => {
|
||||
return {
|
||||
AccessToken: sessionInfo.AccessToken,
|
||||
ApiGatewayUri: sessionInfo.ApiGatewayUri,
|
||||
ApiGatewayVersion: sessionInfo.ApiGatewayVersion
|
||||
ApiGatewayVersion: sessionInfo.ApiGatewayVersion,
|
||||
Cookie: cookie
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
tokenCache.Write(session.AccessToken);
|
||||
console.log("Wrote access token to token cache.");
|
||||
|
||||
console.log(`ApiGatewayUri: ${session.ApiGatewayUri}`);
|
||||
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...");
|
||||
var [title, hlsUrl] = await getVideoInfo(videoID, session);
|
||||
var [title, hlsUrl] = await getVideoMetadata(videoGuids, session);
|
||||
|
||||
title = (sanitize(title) == "") ?
|
||||
`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 +
|
||||
` --output "${outputDirectory}/${title}.mp4" --add-header ` +
|
||||
`Cookie:"${cookie}" "${hlsUrl}"`;
|
||||
`Cookie:"${session.AccessToken}" "${hlsUrl}"`;
|
||||
|
||||
if (argv.simulate) {
|
||||
youtubedlCmd = youtubedlCmd + " -s";
|
||||
@@ -185,16 +199,14 @@ async function rentVideoForLater(videoUrls: string[], outputDirectory: string, u
|
||||
console.log(`\n\n[VERBOSE] Invoking youtube-dl:\n${youtubedlCmd}\n\n`);
|
||||
}
|
||||
execSync(youtubedlCmd, { stdio: 'inherit' });
|
||||
}
|
||||
|
||||
console.log("At this point Chrome's job is done, shutting it down...");
|
||||
await browser.close();
|
||||
}
|
||||
|
||||
|
||||
function sleep(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
|
||||
async function exfiltrateCookie(page: puppeteer.Page) {
|
||||
var jar = await page.cookies("https://.api.microsoftstream.com");
|
||||
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}`;
|
||||
}
|
||||
|
||||
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 hlsUrl: string;
|
||||
|
||||
let metadata = new Metadata();
|
||||
videoGuids.forEach(guid => {
|
||||
let content = axios.get(
|
||||
`${session.ApiGatewayUri}videos/${videoID}` +
|
||||
`?$expand=creator,tokens,status,liveEvent,extensions&api-version=${session.ApiGatewayVersion}`,
|
||||
`${session.ApiGatewayUri}videos/${guid}?api-version=${session.ApiGatewayVersion}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${session.AccessToken}`
|
||||
@@ -269,6 +286,7 @@ async function getVideoInfo(videoID: string, session: any) {
|
||||
});
|
||||
|
||||
return [title, hlsUrl];
|
||||
});
|
||||
}
|
||||
|
||||
// We should probably use Mocha or something
|
||||
|
||||
Reference in New Issue
Block a user