Mid-apocalypse working release with token cache
This commit is contained in:
parent
73aeb92e66
commit
c48e4c54ca
4 changed files with 34 additions and 68 deletions
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
|
@ -4,13 +4,15 @@
|
||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
|
|
||||||
{
|
{
|
||||||
"type": "node",
|
"type": "node",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "Launch Program",
|
"name": "Launch Program",
|
||||||
"program": "${workspaceFolder}/destreamer.js",
|
"program": "${workspaceFolder}/destreamer.js",
|
||||||
"args": [
|
"args": [
|
||||||
"https://msit.microsoftstream.com/video/6f1a382b-e20c-44c0-98fc-5608286e48bc"
|
"--videoUrls",
|
||||||
|
"https://web.microsoftstream.com/video/6f1a382b-e20c-44c0-98fc-5608286e48bc"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
59
Metadata.ts
59
Metadata.ts
|
@ -5,60 +5,33 @@ import { Metadata, Session } from './Types';
|
||||||
|
|
||||||
export async function getVideoMetadata(videoGuids: string[], session: Session): Promise<Metadata[]> {
|
export async function getVideoMetadata(videoGuids: string[], session: Session): Promise<Metadata[]> {
|
||||||
let metadata: Metadata[] = [];
|
let metadata: Metadata[] = [];
|
||||||
videoGuids.forEach(async guid => {
|
let title: string;
|
||||||
|
let playbackUrl: string;
|
||||||
|
|
||||||
|
await Promise.all(videoGuids.map(async guid => {
|
||||||
let apiUrl = `${session.ApiGatewayUri}videos/${guid}?api-version=${session.ApiGatewayVersion}`;
|
let apiUrl = `${session.ApiGatewayUri}videos/${guid}?api-version=${session.ApiGatewayVersion}`;
|
||||||
console.log(`Calling ${apiUrl}`);
|
console.log(`Calling ${apiUrl}`);
|
||||||
let content = axios.get(
|
let response = await axios.get(apiUrl,
|
||||||
apiUrl,
|
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${session.AccessToken}`
|
Authorization: `Bearer ${session.AccessToken}`
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.then(response => {
|
|
||||||
return response.data;
|
|
||||||
})
|
|
||||||
.catch((error: AxiosError) => {
|
|
||||||
term.red('Error when calling Microsoft Stream API: ' +
|
|
||||||
`${error.response?.status} ${error.response?.statusText}\n`);
|
|
||||||
console.dir(error.response?.data);
|
|
||||||
term.red("This is an unrecoverable error. Exiting...\n");
|
|
||||||
process.exit(29);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
title = await response.data["name"];
|
||||||
|
playbackUrl = await response.data["playbackUrls"]
|
||||||
|
.filter((item: { [x: string]: string; }) =>
|
||||||
|
item["mimeType"] == "application/vnd.apple.mpegurl")
|
||||||
|
.map((item: { [x: string]: string }) => { return item["playbackUrl"]; })[0];
|
||||||
|
|
||||||
let title: string = await content.then(data => {
|
|
||||||
return data["name"];
|
|
||||||
});
|
|
||||||
|
|
||||||
let playbackUrl: string = await content.then(data => {
|
term.brightMagenta(`\n title = ${title}\n playbackUrl = ${playbackUrl}\n`);
|
||||||
let playbackUrl = null;
|
|
||||||
try {
|
|
||||||
playbackUrl = data["playbackUrls"]
|
|
||||||
.filter((item: { [x: string]: string; }) =>
|
|
||||||
item["mimeType"] == "application/vnd.apple.mpegurl")
|
|
||||||
.map((item: { [x: string]: string }) =>
|
|
||||||
{ return item["playbackUrl"]; })[0];
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
console.error(`Error fetching HLS URL: ${e.message}.\n playbackUrl is ${playbackUrl}`);
|
|
||||||
process.exit(27);
|
|
||||||
}
|
|
||||||
|
|
||||||
return playbackUrl;
|
metadata.push({
|
||||||
});
|
title: title,
|
||||||
|
playbackUrl: playbackUrl
|
||||||
console.log(`title = ${title} \n playbackUrl = ${playbackUrl}`)
|
|
||||||
|
|
||||||
metadata.push({
|
|
||||||
title: title,
|
|
||||||
playbackUrl: playbackUrl
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
console.log(`metadata--------`)
|
return metadata;
|
||||||
console.dir(metadata);
|
|
||||||
return metadata;
|
|
||||||
}
|
}
|
|
@ -20,7 +20,7 @@ export class TokenCache {
|
||||||
[key: string]: any
|
[key: string]: any
|
||||||
}
|
}
|
||||||
|
|
||||||
const decodedJwt: Jwt = jwtDecode(j.accessToken);
|
const decodedJwt: Jwt = jwtDecode(j.AccessToken);
|
||||||
|
|
||||||
let now = Math.floor(Date.now() / 1000);
|
let now = Math.floor(Date.now() / 1000);
|
||||||
let exp = decodedJwt["exp"];
|
let exp = decodedJwt["exp"];
|
||||||
|
@ -31,10 +31,10 @@ export class TokenCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
let session: Session = {
|
let session: Session = {
|
||||||
AccessToken: j.accessToken,
|
AccessToken: j.AccessToken,
|
||||||
ApiGatewayUri: j.apiGatewayUri,
|
ApiGatewayUri: j.ApiGatewayUri,
|
||||||
ApiGatewayVersion: j.apiGatewayVersion
|
ApiGatewayVersion: j.ApiGatewayVersion
|
||||||
}
|
};
|
||||||
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
|
@ -168,32 +168,18 @@ function extractVideoGuid(videoUrls: string[]): string[] {
|
||||||
async function downloadVideo(videoUrls: string[], outputDirectory: string, session: Session) {
|
async function downloadVideo(videoUrls: string[], outputDirectory: string, session: Session) {
|
||||||
console.log(videoUrls);
|
console.log(videoUrls);
|
||||||
const videoGuids = extractVideoGuid(videoUrls);
|
const videoGuids = extractVideoGuid(videoUrls);
|
||||||
console.log('EXTRACTED videoGuids:');
|
|
||||||
console.log(videoGuids);
|
|
||||||
let accessToken = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
let tc = tokenCache.Read();
|
|
||||||
accessToken = tc?.AccessToken;
|
|
||||||
}
|
|
||||||
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...");
|
||||||
let metadata: Metadata[] = await getVideoMetadata(videoGuids, session);
|
let metadata: Metadata[] = await getVideoMetadata(videoGuids, session);
|
||||||
console.log('metadata:');
|
|
||||||
console.log(metadata)
|
|
||||||
metadata.forEach(video => {
|
metadata.forEach(video => {
|
||||||
video.title = sanitize(video.title);
|
video.title = sanitize(video.title);
|
||||||
term.blue(`Video Title: ${video.title}`);
|
term.blue(`\nDownloading Video: ${video.title}\n`);
|
||||||
console.log('Spawning youtube-dl with cookie and HLS URL...');
|
console.log('Spawning youtube-dl with cookie and HLS URL...');
|
||||||
const format = argv.format ? `-f "${argv.format}"` : "";
|
const format = argv.format ? `-f "${argv.format}"` : "";
|
||||||
var youtubedlCmd = 'youtube-dl --no-call-home --no-warnings ' + format +
|
var youtubedlCmd = 'youtube-dl --no-call-home --no-warnings ' + format +
|
||||||
` --output "${outputDirectory}/${video.title}.mp4" --add-header ` +
|
` --output "${outputDirectory}/${video.title}.mp4" --add-header ` +
|
||||||
`Cookie:"${session.AccessToken}" "${video.playbackUrl}"`;
|
`Authorization:"Bearer ${session.AccessToken}" "${video.playbackUrl}"`;
|
||||||
|
|
||||||
if (argv.simulate) {
|
if (argv.simulate) {
|
||||||
youtubedlCmd = youtubedlCmd + " -s";
|
youtubedlCmd = youtubedlCmd + " -s";
|
||||||
}
|
}
|
||||||
|
@ -210,7 +196,12 @@ function sleep(ms: number) {
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
sanityChecks();
|
sanityChecks();
|
||||||
let session = await DoInteractiveLogin(argv.username);
|
let session = tokenCache.Read();
|
||||||
|
if (session == null)
|
||||||
|
{
|
||||||
|
session = await DoInteractiveLogin(argv.username);
|
||||||
|
}
|
||||||
|
|
||||||
downloadVideo(argv.videoUrls as string[], argv.outputDirectory, session);
|
downloadVideo(argv.videoUrls as string[], argv.outputDirectory, session);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue