/**
 * Konichiwa~
 *
 * This module contains all the api calls related to download_all.
 * This module also takes care of the 9anime encryption scheme.
 */ import * as utils from "../utils" ; const DD = "gIXCaNh" ; function s ( t ) { let e ; let i = 0 ; for ( e = 0 ; e < t . length ; e ++ ) { i += t . charCodeAt ( e ) * e + e ; } return i ; } function a ( t , e ) { let i ; let n = 0 ; for ( i = 0 ; i < Math . max ( t . length , e . length ) ; i ++ ) { n += i < e . length ? e . charCodeAt ( i ) : 0 ; n += i < t . length ? t . charCodeAt ( i ) : 0 ; } return Number ( n ) . toString ( 16 ) ; } export function generateToken ( data , initialState = 0 ) { let keys = Object . keys ( data ) ; let _ = s ( DD ) + initialState ; for ( let key of keys ) { let trans = a ( DD + key , data [ key ] . toString ( ) ) ; _ += s ( trans ) ; } return _ - 33 ; } let baseUrl = "https://9anime.to" ; interface ISetupOptions { baseUrl : string ; } export function setup ( options ) { baseUrl = options . baseUrl ; } interface IGrabberParams { ts : string ; id : string ; update : number ; [ key ] : string | number ; } export interface IGrabber { grabber : string ; name : string ; params : { id : string ; options : string ; token : string ; } ; subtitle : string ; target : string ; type : string ; } param params returns export function grabber ( params ) { params . _ = generateToken ( params ) ; return new Promise ( ( resolve , reject ) => { let endpoint = `${ baseUrl }/ajax/episode/info?${ utils . obj2query ( params ) }` ; fetch ( endpoint ) . then ( response => { if ( response . ok ) { return response . json ( ) ; } throw new Error ( response . status . toString ( ) ) ; } ) . then ( resp => { if ( resp . error && resp . error === "token" ) { throw new Error ( "Token is missing" ) ; } resolve ( resp ) ; } ) . catch ( err => reject ( err ) ) ; } ) ; } interface IlinksParams { ts : string ; id : string ; options : string ; token : string ; mobile : number ; [ key ] : string | number ; } /**
 * Represents a episode file. Has these properties:
 * file - is the url of the file
 * label - is the quality of the file; ex: 360p, 480p
 * type - is the file type; ex: mp4
 */ export interface IFile { file : string ; label : string ; type : string ; } /**
 * Response structure of links9a method. "data" holds a list
 * of objects. Each object, lets call them file, represents
 * a quality for the episode.
 */ interface IFileList { data : IFile [ ] ; error : number ; token : string ; } param uri param data returns export function links9a ( uri , data ) { let decomposed = utils . decomposeURL ( uri ) ; let initState = s ( a ( DD + decomposed [ 0 ] , "" ) ) ; let merged = utils . mergeObject ( data , decomposed [ 1 ] ) ; merged . _ = generateToken ( merged , initState ) ; return new Promise ( ( resolve , reject ) => { let endpoint = `${ decomposed [ 0 ] }?${ utils . obj2query ( merged ) }` ; fetch ( endpoint ) . then ( response => { if ( response . ok ) { return response . json ( ) ; } throw new Error ( response . status . toString ( ) ) ; } ) . then ( resp => resolve ( resp ) ) . catch ( err => reject ( err ) ) ; } ) ; }