Skip to content

Commit e2dc48f

Browse files
committed
Minor change in audiomack, add youtube fallback to gaana and jiosaavn
1 parent 00b45c4 commit e2dc48f

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

src/sources/audiomack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { http1makeRequest, logger, encodeTrack } from '../utils.js'
1+
import { http1makeRequest, logger, encodeTrack , getBestMatch} from '../utils.js'
22
import crypto from 'node:crypto'
33
import { PassThrough } from 'node:stream'
44

src/sources/gaana.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Credits: https://github.com/southctrl; adapted for NodeLink
33
*/
44

5-
import { encodeTrack, http1makeRequest, logger } from '../utils.js'
5+
import { encodeTrack, http1makeRequest, logger, getBestMatch } from '../utils.js'
66
import HLSHandler from '../playback/hls/HLSHandler.js'
77

88
const USER_AGENT =
@@ -101,7 +101,7 @@ export default class GaanaSource {
101101
if (!/^\d+$/.test(String(trackId))) {
102102
const trackData = await this.getJson(`/api/songs/${encodeURIComponent(trackId)}`)
103103
if (!trackData?.track_id) {
104-
return { exception: { message: 'Track metadata not found for stream.', severity: 'common' } }
104+
throw new Error('Track metadata not found for stream.')
105105
}
106106
trackId = trackData.track_id
107107
}
@@ -111,13 +111,13 @@ export default class GaanaSource {
111111
)
112112

113113
if (!streamData) {
114-
return { exception: { message: 'Stream URL not found.', severity: 'common' } }
114+
throw new Error('Stream URL not found.')
115115
}
116116

117117
const hlsUrl = streamData.hlsUrl || streamData.hls_url || null
118118
const url = hlsUrl || streamData.url
119119
if (!url) {
120-
return { exception: { message: 'No playable stream URL.', severity: 'common' } }
120+
throw new Error('No playable stream URL.')
121121
}
122122

123123
const segments = Array.isArray(streamData.segments)
@@ -137,9 +137,28 @@ export default class GaanaSource {
137137
}
138138
}
139139
} catch (e) {
140-
logger('error', 'Gaana', `Stream resolve error: ${e.message}`)
141-
return { exception: { message: e.message, severity: 'fault' } }
140+
logger(
141+
'warn',
142+
'Gaana',
143+
`Direct stream failed for ${decodedTrack.title}: ${e.message}. Falling back to YouTube.`
144+
)
142145
}
146+
147+
const searchResult = await this.nodelink.sources.searchWithDefault(
148+
`${decodedTrack.title} ${decodedTrack.author}`
149+
)
150+
151+
const bestMatch = getBestMatch(searchResult.data, decodedTrack)
152+
if (!bestMatch)
153+
return {
154+
exception: {
155+
message: 'No suitable alternative found.',
156+
severity: 'fault'
157+
}
158+
}
159+
160+
const streamInfo = await this.nodelink.sources.getTrackUrl(bestMatch.info)
161+
return { newTrack: bestMatch, ...streamInfo }
143162
}
144163

145164
async loadStream(track, url, protocol, additionalData) {

src/sources/jiosaavn.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PassThrough } from 'node:stream'
2-
import { encodeTrack, http1makeRequest, logger } from '../utils.js'
2+
import { encodeTrack, http1makeRequest, logger, getBestMatch, } from '../utils.js'
33
import { desEcbDecryptBase64ToUtf8 } from '../decrypters/des-ecb.js'
44

55
const 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

Comments
 (0)