1
0
mirror of https://github.com/snobu/destreamer.git synced 2026-01-21 23:42:16 +00:00

Major code refactoring (#164)

* Added Chromium caching of identity provider cookies

* Moved token expiry check in standalone method

* Created refreshSession function

* Session is now refreshed if the token expires

* Linting fixes

* Removed debug console.log()

* Added CC support

* Created function to prompt user for download parameters (interactive mode)

* Fix data folder for puppeteer

* Fixed multiple session error

* Fix token expire time

* Moved session refreshing to a more sensible place

* Changed Metadata name to Video (to better reflect the data structure)

* Complete CLI refactoring

* Removed useless sleep function

* Added outDir check from CLI

* Complete input parsing refactoring (both inline and file)

* Fixed and improved tests to work with the new input parsing

* Moved and improved output path generation to videoUtils

* Main code refactoring, added outpath to video type

* Minor changes in spacing and type definition style

* Updated readme after code refactoring

* Fix if inputFile doesn't start with url on line 1

* Minor naming change

* Use module 'winston' for logging

* Created logge, changed all console.log and similar to use the logger

* Added verbose logging, changed posterUrl property name on Video type

* Moved GUID extraction to input parsing

* Added support for group links

* Fixed test after last input parsing update

* Removed debug proces.exit()

* Changed from desc to asc order for group videos

* Updated test to reflect GUIDs output after parsing

* Added couple of comments and restyled some imports

* More readable verbose GUIDs logging

* Removed unused errors

* Temporary fix for timeout not working in ApiClient

* Explicit class member accessibility

* Defined array naming schema to be Array<T>

* Defined type/interface schema to be type only

* A LOT of type definitions
This commit is contained in:
lukaarma
2020-07-18 21:49:36 +02:00
committed by GitHub
parent 89a942eb24
commit 7bfc565a05
19 changed files with 981 additions and 638 deletions

View File

@@ -1,6 +1,9 @@
import { logger } from './Logger';
import { Session } from './Types';
import axios, { AxiosRequestConfig, AxiosResponse, AxiosInstance, AxiosError } from 'axios';
import axiosRetry, { isNetworkOrIdempotentRequestError } from 'axios-retry';
import { Session } from './Types';
export class ApiClient {
private static instance: ApiClient;
@@ -11,26 +14,30 @@ export class ApiClient {
this.session = session;
this.axiosInstance = axios.create({
baseURL: session?.ApiGatewayUri,
timeout: 7000,
// timeout: 7000,
headers: { 'User-Agent': 'destreamer/2.0 (Hammer of Dawn)' }
});
axiosRetry(this.axiosInstance, {
// The following option is not working.
// We should open an issue on the relative GitHub
shouldResetTimeout: true,
retries: 6,
retryDelay: (retryCount) => {
retryDelay: (retryCount: number) => {
return retryCount * 2000;
},
retryCondition: (err: AxiosError) => {
const retryCodes = [429, 500, 502, 503];
const retryCodes: Array<number> = [429, 500, 502, 503];
if (isNetworkOrIdempotentRequestError(err)) {
console.warn(`${err}. Retrying request...`);
logger.warn(`${err}. Retrying request...`);
return true;
}
console.warn(`Got HTTP ${err?.response?.status}. Retrying request...`);
const condition = retryCodes.includes(err?.response?.status ?? 0);
}
logger.warn(`Got HTTP code ${err?.response?.status ?? undefined}. Retrying request...`);
return condition;
const shouldRetry: boolean = retryCodes.includes(err?.response?.status ?? 0);
return shouldRetry;
}
});
}
@@ -52,7 +59,7 @@ export class ApiClient {
method: AxiosRequestConfig['method'] = 'get',
payload?: any): Promise<AxiosResponse | undefined> {
const delimiter = path.split('?').length === 1 ? '?' : '&';
const delimiter: '?' | '&' = path.split('?').length === 1 ? '?' : '&';
const headers: object = {
'Authorization': 'Bearer ' + this.session?.AccessToken
@@ -74,7 +81,7 @@ export class ApiClient {
method: AxiosRequestConfig['method'] = 'get',
payload?: any,
responseType: AxiosRequestConfig['responseType'] = 'json'): Promise<AxiosResponse | undefined> {
const headers: object = {
'Authorization': 'Bearer ' + this.session?.AccessToken
};
@@ -87,5 +94,4 @@ export class ApiClient {
responseType: responseType
});
}
}
}