1- import { ParseError , VideoLinksError } from './errors' ;
1+ import { VideoLinksError } from './errors' ;
22import type { ObjectOrUnknown } from './types' ;
33
44export const KODIK_PLAYER_DOMAIN = 'kodik.info' ;
@@ -63,13 +63,22 @@ export const kodikPlayerLinkRegexp = /^(?<protocol>http[s]?:|)\/\/(?<host>[a-z0-
6363
6464export class VideoLinks {
6565 static async parseLink < Extended extends boolean > ( { extended, link } : VideoLinksParseParams < Extended > ) : Promise < KodikParsedLink < Extended > > {
66- if ( ! link ) throw new ParseError ( 'link is undefined' ) ;
66+ if ( ! link )
67+ throw new VideoLinksError ( {
68+ code : 'parse-link-invalid' ,
69+ description : 'link is not provided' ,
70+ data : { link }
71+ } ) ;
6772
6873 const kodikLink = this . normalizeKodikLink ( link ) ;
69- if ( ! kodikPlayerLinkRegexp . test ( link ) ) throw new ParseError ( 'kodikLink is not allowed' ) ;
74+ if ( ! kodikPlayerLinkRegexp . test ( link ) )
75+ throw new VideoLinksError ( {
76+ code : 'parse-link-invalid' ,
77+ description : 'link is not valid' ,
78+ data : { link }
79+ } ) ;
7080
71- const linkParams = kodikPlayerLinkRegexp . exec ( kodikLink ) ?. groups ;
72- if ( ! linkParams ) throw new ParseError ( 'cannot get \'groups\' from \'linkParams\'' ) ;
81+ const linkParams = kodikPlayerLinkRegexp . exec ( kodikLink ) ! . groups ! ;
7382
7483 const { host, hash, id, quality, type } = linkParams ;
7584 const parsedLink : KodikParsedLink = {
@@ -85,8 +94,16 @@ export class VideoLinks {
8594 const skipButtons = page . match ( / p a r s e S k i p B u t t o n s ? \( " (?< data > [ ^ " ] + ) " \s * , \s * " (?< type > [ ^ " ] + ) " \) / is) ?. groups ;
8695 const playerSingleUrl = page . match ( / s r c = " (?< link > \/ a s s e t s \/ j s \/ a p p \. p l a y e r _ s i n g l e \. [ a - z 0 - 9 ] + \. j s ) " / is) ?. groups ?. link ;
8796
88- if ( ! urlParams ) throw new ParseError ( 'cannot get urlParams' ) ;
89- if ( ! translation ) throw new ParseError ( 'cannot get translation' ) ;
97+ if ( ! urlParams ) throw new VideoLinksError ( {
98+ code : 'parse-link-ex-invalid' ,
99+ description : 'cannot get url params' ,
100+ data : { link, page }
101+ } ) ;
102+ if ( ! translation ) throw new VideoLinksError ( {
103+ code : 'parse-link-ex-invalid' ,
104+ description : 'cannot get translation' ,
105+ data : { link, page }
106+ } ) ;
90107
91108 const extendedParsedLink : KodikParsedLink < true > = {
92109 ...parsedLink ,
@@ -126,25 +143,38 @@ export class VideoLinks {
126143 ) ;
127144 const videoInfoResponse = await fetch ( url ) ;
128145 if ( videoInfoResponse . headers . get ( 'content-type' ) !== 'application/json' )
129- throw new VideoLinksError ( 'videoInfoResponse is not json' ) ;
146+ throw new VideoLinksError ( {
147+ code : 'get-links-invalid-response' ,
148+ description : 'videoInfoResponse is not json' ,
149+ data : { videoInfoResponse }
150+ } ) ;
130151
131152 const videoInfoJson = await videoInfoResponse . json ( ) ;
132153
133154 if ( typeof videoInfoJson !== 'object' || videoInfoJson === null )
134- throw new VideoLinksError ( 'videoInfoJson is not object' ) ;
155+ throw new VideoLinksError ( {
156+ code : 'get-links-invalid-response' ,
157+ description : 'videoInfoJson is not object' ,
158+ data : { videoInfoResponse, videoInfoJson }
159+ } ) ;
135160 if ( typeof videoInfoJson . links !== 'object' )
136- throw new VideoLinksError ( 'videoInfoJson.links is not object' ) ;
161+ throw new VideoLinksError ( {
162+ code : 'get-links-invalid-response' ,
163+ description : 'videoInfoJson.links is not object' ,
164+ data : { videoInfoResponse, videoInfoJson }
165+ } ) ;
137166
138167 const links = videoInfoJson . links as KodikVideoLinks ;
139168 const zCharCode = 'Z' . charCodeAt ( 0 ) ;
140169
141170 // decrypt source links
142171 for ( const [ , sources ] of Object . entries ( links ) ) {
143172 for ( const source of sources ) {
144- source . src = Buffer . from ( source . src . replace ( / [ a - z A - Z ] / g, e => {
173+ const decryptedBase64 = source . src . replace ( / [ a - z A - Z ] / g, e => {
145174 let eCharCode = e . charCodeAt ( 0 ) ;
146175 return String . fromCharCode ( ( eCharCode <= zCharCode ? 90 : 122 ) >= ( eCharCode = eCharCode + 13 ) ? eCharCode : eCharCode - 26 ) ;
147- } ) , 'base64' ) . toString ( 'utf8' ) ;
176+ } ) ;
177+ source . src = atob ( decryptedBase64 ) ;
148178 }
149179 }
150180
0 commit comments