diff --git a/.vscode/launch.json b/.vscode/launch.json index 625b166..d1d9d8f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,13 +4,15 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { "type": "node", "request": "launch", "name": "Launch Program", "program": "${workspaceFolder}/destreamer.js", "args": [ - "https://msit.microsoftstream.com/video/6f1a382b-e20c-44c0-98fc-5608286e48bc" + "--videoUrls", + "https://web.microsoftstream.com/video/6f1a382b-e20c-44c0-98fc-5608286e48bc" ] } ] diff --git a/Metadata.ts b/Metadata.ts index 3ab2a1a..3a681c8 100644 --- a/Metadata.ts +++ b/Metadata.ts @@ -5,60 +5,33 @@ import { Metadata, Session } from './Types'; export async function getVideoMetadata(videoGuids: string[], session: Session): Promise { 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}`; console.log(`Calling ${apiUrl}`); - let content = axios.get( - apiUrl, + let response = await axios.get(apiUrl, { headers: { 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 => { - 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); - } + term.brightMagenta(`\n title = ${title}\n playbackUrl = ${playbackUrl}\n`); - return playbackUrl; - }); - - console.log(`title = ${title} \n playbackUrl = ${playbackUrl}`) - - metadata.push({ - title: title, - playbackUrl: playbackUrl - }); - - + metadata.push({ + title: title, + playbackUrl: playbackUrl }); + })); - console.log(`metadata--------`) - console.dir(metadata); - return metadata; + return metadata; } \ No newline at end of file diff --git a/TokenCache.ts b/TokenCache.ts index 81fce58..5a2f713 100644 --- a/TokenCache.ts +++ b/TokenCache.ts @@ -20,7 +20,7 @@ export class TokenCache { [key: string]: any } - const decodedJwt: Jwt = jwtDecode(j.accessToken); + const decodedJwt: Jwt = jwtDecode(j.AccessToken); let now = Math.floor(Date.now() / 1000); let exp = decodedJwt["exp"]; @@ -31,10 +31,10 @@ export class TokenCache { } let session: Session = { - AccessToken: j.accessToken, - ApiGatewayUri: j.apiGatewayUri, - ApiGatewayVersion: j.apiGatewayVersion - } + AccessToken: j.AccessToken, + ApiGatewayUri: j.ApiGatewayUri, + ApiGatewayVersion: j.ApiGatewayVersion + }; return session; } diff --git a/destreamer.ts b/destreamer.ts index a0d67c6..7e16315 100644 --- a/destreamer.ts +++ b/destreamer.ts @@ -168,32 +168,18 @@ function extractVideoGuid(videoUrls: string[]): string[] { async function downloadVideo(videoUrls: string[], outputDirectory: string, session: Session) { console.log(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..."); let metadata: Metadata[] = await getVideoMetadata(videoGuids, session); - console.log('metadata:'); - console.log(metadata) metadata.forEach(video => { 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...'); const format = argv.format ? `-f "${argv.format}"` : ""; var youtubedlCmd = 'youtube-dl --no-call-home --no-warnings ' + format + - ` --output "${outputDirectory}/${video.title}.mp4" --add-header ` + - `Cookie:"${session.AccessToken}" "${video.playbackUrl}"`; - + ` --output "${outputDirectory}/${video.title}.mp4" --add-header ` + + `Authorization:"Bearer ${session.AccessToken}" "${video.playbackUrl}"`; + if (argv.simulate) { youtubedlCmd = youtubedlCmd + " -s"; } @@ -210,7 +196,12 @@ function sleep(ms: number) { async function main() { 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); }