11import { PassThrough } from 'node:stream'
2- import { encodeTrack , http1makeRequest , logger } from '../utils.js'
2+ import { encodeTrack , http1makeRequest , logger , getBestMatch , } from '../utils.js'
33import { desEcbDecryptBase64ToUtf8 } from '../decrypters/des-ecb.js'
44
55const API_BASE = 'https://www.jiosaavn.com/api.php'
@@ -170,14 +170,7 @@ export default class JioSaavnSource {
170170 }
171171 }
172172
173- async getTrackUrl ( decodedTrack , itag , forceRefresh = false ) {
174- if ( ! forceRefresh ) {
175- const cached = this . nodelink . trackCacheManager . get ( 'jiosaavn' , decodedTrack . identifier )
176- if ( cached ) return cached
177- }
178-
179- const trackId = decodedTrack . identifier
180-
173+ async getTrackUrl ( decodedTrack ) {
181174 try {
182175 logger (
183176 'debug' ,
@@ -188,18 +181,11 @@ export default class JioSaavnSource {
188181 const trackData = await this . _fetchSongMetadata ( decodedTrack . identifier )
189182
190183 if ( ! trackData ) {
191- return {
192- exception : { message : 'Track metadata not found' , severity : 'common' }
193- }
184+ throw new Error ( 'Track metadata not found' )
194185 }
195186
196187 if ( ! trackData . encrypted_media_url ) {
197- return {
198- exception : {
199- message : 'No encrypted_media_url found' ,
200- severity : 'fault'
201- }
202- }
188+ throw new Error ( 'No encrypted_media_url found' )
203189 }
204190
205191 let playbackUrl = this . _decryptUrl ( trackData . encrypted_media_url )
@@ -215,9 +201,28 @@ export default class JioSaavnSource {
215201 additionalData : { }
216202 }
217203 } catch ( e ) {
218- logger ( 'error' , 'JioSaavn' , `Stream load error: ${ e . message } ` )
219- return { exception : { message : e . message , severity : 'fault' } }
204+ logger (
205+ 'warn' ,
206+ 'JioSaavn' ,
207+ `Direct stream failed for ${ decodedTrack . title } : ${ e . message } . Falling back to YouTube.`
208+ )
220209 }
210+
211+ const searchResult = await this . nodelink . sources . searchWithDefault (
212+ `${ decodedTrack . title } ${ decodedTrack . author } `
213+ )
214+
215+ const bestMatch = getBestMatch ( searchResult . data , decodedTrack )
216+ if ( ! bestMatch )
217+ return {
218+ exception : {
219+ message : 'No suitable alternative found.' ,
220+ severity : 'fault'
221+ }
222+ }
223+
224+ const streamInfo = await this . nodelink . sources . getTrackUrl ( bestMatch . info )
225+ return { newTrack : bestMatch , ...streamInfo }
221226 }
222227
223228 async loadStream ( _track , url , _protocol , _additionalData ) {
0 commit comments