@@ -82,7 +82,7 @@ export class YouTubeMusic {
82
82
'user-agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36' ,
83
83
} ,
84
84
} ) ,
85
- apiKey : null ,
85
+ apiConfig : null ,
86
86
} ;
87
87
88
88
#request = async function request ( url , opts ) {
@@ -103,13 +103,15 @@ export class YouTubeMusic {
103
103
return response . body ;
104
104
} ;
105
105
106
- #getApiKey = async function getApiKey ( force = false ) {
107
- if ( this . #store. apiKey && ! force ) return this . #store. apiKey ;
106
+ #deriveConfig = async function deriveConfig ( force = false ) {
107
+ if ( this . #store. apiConfig && ! force ) return this . #store. apiConfig ;
108
108
const body = await this . #request( 'https://music.youtube.com/' , { method : 'get' } ) ;
109
109
let match ;
110
- if ( ( match = ( body || '' ) . match ( / (? = " I N N E R T U B E _ A P I _ K E Y " : " ( .+ ?) " ) / ) ) )
111
- return ( [ , this . #store. apiKey ] = match ) , this . #store. apiKey ;
112
- throw new YouTubeSearchError ( 'Failed to extract `INNERTUBE_API_KEY`' ) ;
110
+ if ( ( match = ( body || '' ) . match ( / y t c f g \. s e t \s * \( \s * ( { .+ } ) \s * \) \s * ; / ) ) ) {
111
+ this . #store. apiConfig = JSON . parse ( match [ 1 ] ) ;
112
+ return this . #store. apiConfig ;
113
+ }
114
+ throw new YouTubeSearchError ( 'Failed to extract YouTube Music Configuration' ) ;
113
115
} ;
114
116
115
117
#YTM_PATHS = {
@@ -130,16 +132,19 @@ export class YouTubeMusic {
130
132
*/
131
133
if ( typeof queryObject !== 'object' ) throw new Error ( '<queryObject> must be an object' ) ;
132
134
if ( params && typeof params !== 'object' ) throw new Error ( '<params>, if defined must be an object' ) ;
135
+
136
+ let { INNERTUBE_API_KEY , INNERTUBE_CLIENT_NAME , INNERTUBE_CLIENT_VERSION } = await this . #deriveConfig( ) ;
137
+
133
138
const response = await this . #request( 'https://music.youtube.com/youtubei/v1/search' , {
134
139
timeout : { request : 10000 } ,
135
140
method : 'post' ,
136
- searchParams : { alt : 'json' , key : await this . #getApiKey ( ) , ...params } ,
141
+ searchParams : { alt : 'json' , key : INNERTUBE_API_KEY , ...params } ,
137
142
responseType : 'json' ,
138
143
json : {
139
144
context : {
140
145
client : {
141
- clientName : 'WEB_REMIX' ,
142
- clientVersion : '0.1' ,
146
+ clientName : INNERTUBE_CLIENT_NAME ,
147
+ clientVersion : INNERTUBE_CLIENT_VERSION ,
143
148
hl : 'en' ,
144
149
gl : 'US' ,
145
150
} ,
0 commit comments