diff --git a/package.json b/package.json index 76639f5..e22c084 100644 --- a/package.json +++ b/package.json @@ -1,50 +1,51 @@ { - "name": "destreamer", - "repository": { - "type": "git", - "url": "git://github.com/snobu/destreamer.git" - }, - "version": "2.1.0", - "description": "Save Microsoft Stream videos for offline enjoyment.", - "main": "build/src/destreamer.js", - "bin": "build/src/destreamer.js", - "scripts": { - "build": "echo Transpiling TypeScript to JavaScript... && node node_modules/typescript/bin/tsc && echo Destreamer was built successfully.", - "test": "mocha build/test", - "lint": "eslint src/*.ts" - }, - "keywords": [], - "author": "snobu", - "license": "MIT", - "devDependencies": { - "@types/mocha": "^8.0.4", - "@types/puppeteer": "^5.4.0", - "@types/readline-sync": "^1.4.3", - "@types/tmp": "^0.2.0", - "@types/yargs": "^15.0.11", - "@typescript-eslint/eslint-plugin": "^4.9.0", - "@typescript-eslint/parser": "^4.9.0", - "eslint": "^7.14.0", - "mocha": "^8.2.1", - "tmp": "^0.2.1" - }, - "dependencies": { - "@tedconf/fessonia": "^2.1.2", - "@types/cli-progress": "^3.8.0", - "@types/jwt-decode": "^2.2.1", - "axios": "^0.21.2", - "axios-retry": "^3.1.9", - "cli-progress": "^3.8.2", - "colors": "^1.4.0", - "is-elevated": "^3.0.0", - "iso8601-duration": "^1.3.0", - "jwt-decode": "^3.1.2", - "puppeteer": "5.5.0", - "readline-sync": "^1.4.10", - "sanitize-filename": "^1.6.3", - "terminal-image": "^1.2.1", - "typescript": "^4.1.2", - "winston": "^3.3.3", - "yargs": "^16.1.1" - } + "name": "destreamer", + "repository": { + "type": "git", + "url": "git://github.com/snobu/destreamer.git" + }, + "version": "2.1.0", + "description": "Save Microsoft Stream videos for offline enjoyment.", + "main": "build/src/destreamer.js", + "bin": "build/src/destreamer.js", + "scripts": { + "build": "echo Transpiling TypeScript to JavaScript... && tsc && echo Destreamer was built successfully.", + "watch": "tsc --watch", + "test": "mocha build/test", + "lint": "eslint src/*.ts" + }, + "keywords": [], + "author": "snobu", + "license": "MIT", + "devDependencies": { + "@types/mocha": "^8.0.4", + "@types/puppeteer": "^5.4.0", + "@types/readline-sync": "^1.4.3", + "@types/tmp": "^0.2.0", + "@types/yargs": "^15.0.11", + "@typescript-eslint/eslint-plugin": "^4.9.0", + "@typescript-eslint/parser": "^4.9.0", + "eslint": "^7.14.0", + "mocha": "^8.2.1", + "tmp": "^0.2.1" + }, + "dependencies": { + "@tedconf/fessonia": "^2.1.2", + "@types/cli-progress": "^3.8.0", + "@types/jwt-decode": "^2.2.1", + "axios": "^0.21.2", + "axios-retry": "^3.1.9", + "cli-progress": "^3.8.2", + "colors": "^1.4.0", + "is-elevated": "^3.0.0", + "iso8601-duration": "^1.3.0", + "jwt-decode": "^3.1.2", + "puppeteer": "5.5.0", + "readline-sync": "^1.4.10", + "sanitize-filename": "^1.6.3", + "terminal-image": "^1.2.1", + "typescript": "^4.1.2", + "winston": "^3.3.3", + "yargs": "^16.1.1" + } } diff --git a/src/ApiClient.ts b/src/ApiClient.ts index ace1704..fb99856 100644 --- a/src/ApiClient.ts +++ b/src/ApiClient.ts @@ -1,8 +1,10 @@ import { logger } from './Logger'; -import { StreamSession } from './Types'; +import { ShareSession, StreamSession, Video } from './Types'; +import { publishedDateToString, publishedTimeToString } from './VideoUtils'; import axios, { AxiosRequestConfig, AxiosResponse, AxiosInstance, AxiosError } from 'axios'; import axiosRetry, { isNetworkOrIdempotentRequestError } from 'axios-retry'; +// import fs from 'fs'; export class StreamApiClient { @@ -113,3 +115,118 @@ export class StreamApiClient { }); } } + +export class ShareApiClient { + private axiosInstance: AxiosInstance; + private site: string; + + public constructor(domain: string, site: string, session: ShareSession) { + this.axiosInstance = axios.create({ + baseURL: domain, + // timeout: 7000, + headers: { + 'User-Agent': 'destreamer/3.0 ALPHA', + 'Cookie': `rtFa=${session.rtFa}; FedAuth=${session.FedAuth}` + } + }); + this.site = site; + + + // FIXME: disabled because it was messing with the direct download check + // axiosRetry(this.axiosInstance, { + // // The following option is not working. + // // We should open an issue on the relative GitHub + // shouldResetTimeout: true, + // retries: 6, + // retryDelay: (retryCount: number) => { + // return retryCount * 2000; + // }, + // retryCondition: (err: AxiosError) => { + // const retryCodes: Array = [429, 500, 502, 503]; + // if (isNetworkOrIdempotentRequestError(err)) { + // logger.warn(`${err}. Retrying request...`); + + // return true; + // } + // logger.warn(`Got HTTP code ${err?.response?.status ?? undefined}.`); + // logger.warn('Here is the error message: '); + // console.dir(err.response?.data); + // logger.warn('We called this URL: ' + err.response?.config.baseURL + err.response?.config.url); + + // const shouldRetry: boolean = retryCodes.includes(err?.response?.status ?? 0); + + // return shouldRetry; + // } + // }); + } + + + public async getVideoInfo(filePath: string, outDir: string): Promise