@@ -82,7 +82,7 @@ export class YouTubeMusic {
8282 'user-agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36' ,
8383 } ,
8484 } ) ,
85- apiKey : null ,
85+ apiConfig : null ,
8686 } ;
8787
8888 #request = async function request ( url , opts ) {
@@ -103,13 +103,15 @@ export class YouTubeMusic {
103103 return response . body ;
104104 } ;
105105
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 ;
108108 const body = await this . #request( 'https://music.youtube.com/' , { method : 'get' } ) ;
109109 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' ) ;
113115 } ;
114116
115117 #YTM_PATHS = {
@@ -130,16 +132,19 @@ export class YouTubeMusic {
130132 */
131133 if ( typeof queryObject !== 'object' ) throw new Error ( '<queryObject> must be an object' ) ;
132134 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+
133138 const response = await this . #request( 'https://music.youtube.com/youtubei/v1/search' , {
134139 timeout : { request : 10000 } ,
135140 method : 'post' ,
136- searchParams : { alt : 'json' , key : await this . #getApiKey ( ) , ...params } ,
141+ searchParams : { alt : 'json' , key : INNERTUBE_API_KEY , ...params } ,
137142 responseType : 'json' ,
138143 json : {
139144 context : {
140145 client : {
141- clientName : 'WEB_REMIX' ,
142- clientVersion : '0.1' ,
146+ clientName : INNERTUBE_CLIENT_NAME ,
147+ clientVersion : INNERTUBE_CLIENT_VERSION ,
143148 hl : 'en' ,
144149 gl : 'US' ,
145150 } ,
0 commit comments