Spaces:
Sleeping
Sleeping
File size: 8,665 Bytes
2d2e00c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | const axios = require('axios');
class JioSaavn {
constructor() {
this.baseURL = 'https://fragrant-rain-244a.bob17040246.workers.dev/api.php';
this.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive'
};
}
/**
* Search for music on JioSaavn
* @param {string} title - Song title
* @param {string} artist - Artist name
* @param {boolean} debug - Enable debug mode
* @returns {Promise<Object>} Song information with download URL
*/
async search(title, artist, debug = false) {
try {
const searchQuery = `${title} ${artist}`;
const url = this._buildSearchURL(searchQuery);
console.log(`[JioSaavn] /jiosaavn/search title='${title}' artist='${artist}' url='${url}'`);
const startTime = Date.now();
const response = await axios.get(url, {
headers: this.headers,
timeout: 15000
});
const duration = Date.now() - startTime;
console.log(`[JioSaavn] status=${response.status} time_ms=${duration} len=${response.data.length} content_type='${response.headers['content-type']}'`);
if (!response.data || typeof response.data !== 'object') {
throw new Error('Invalid response format from JioSaavn API');
}
const results = response.data.results || [];
console.log(`[JioSaavn] results_count=${results.length}`);
if (results.length === 0) {
throw new Error('Music stream not found in JioSaavn results');
}
// Process results
const processedResults = results.map(rawSong => this._createSongPayload(rawSong));
console.log(`[JioSaavn] processed_count=${processedResults.length} sample_titles=${processedResults.slice(0, 3).map(r => r.name)}`);
// Find matching track
const matchingTrack = this._findMatchingTrack(processedResults, title, artist, debug);
if (!matchingTrack) {
throw new Error('Music stream not found in JioSaavn results');
}
// Create final response
const finalResponse = {
name: matchingTrack.name,
year: matchingTrack.year,
copyright: matchingTrack.copyright,
duration: matchingTrack.duration,
label: matchingTrack.label,
albumName: matchingTrack.album?.name || null,
artists: [
...(matchingTrack.artists?.primary || []),
...(matchingTrack.artists?.featured || []),
...(matchingTrack.artists?.all || [])
],
downloadUrl: matchingTrack.downloadUrl
};
if (debug) {
finalResponse._debug = {
queried_url: url,
time_ms: duration,
matched_artists: matchingTrack.artists
};
}
return finalResponse;
} catch (error) {
console.error(`[JioSaavn][ERR] ${error.message}`);
throw new Error(`JioSaavn search failed: ${error.message}`);
}
}
/**
* Search for all music results on JioSaavn
* @param {string} query - Search query
* @param {number} limit - Number of results to return
* @param {boolean} debug - Enable debug mode
* @returns {Promise<Object>} All matching songs
*/
async searchAll(query, limit = 10, debug = false) {
try {
const url = this._buildSearchURL(query, limit);
console.log(`[JioSaavn] /jiosaavn/search/all q='${query}' limit=${limit} url='${url}'`);
const startTime = Date.now();
const response = await axios.get(url, {
headers: this.headers,
timeout: 15000
});
const duration = Date.now() - startTime;
console.log(`[JioSaavn] status=${response.status} time_ms=${duration} len=${response.data.length} content_type='${response.headers['content-type']}'`);
if (!response.data || typeof response.data !== 'object') {
throw new Error('Invalid response format from JioSaavn API');
}
const results = response.data.results || [];
console.log(`[JioSaavn] results_count=${results.length}`);
if (results.length === 0) {
return { results: [] };
}
// Process all results
const processedResults = results.map(rawSong => this._createSongPayload(rawSong));
console.log(`[JioSaavn] processed_count=${processedResults.length} sample_titles=${processedResults.slice(0, 3).map(r => r.name)}`);
const response_data = {
query,
total: processedResults.length,
results: processedResults
};
if (debug) {
response_data._debug = {
queried_url: url,
time_ms: duration
};
}
return response_data;
} catch (error) {
console.error(`[JioSaavn][ERR] ${error.message}`);
throw new Error(`JioSaavn search all failed: ${error.message}`);
}
}
// Private helper methods
_buildSearchURL(query, limit = 10) {
const params = new URLSearchParams({
_format: 'json',
_marker: '0',
api_version: '4',
ctx: 'web6dot0',
__call: 'search.getResults',
q: query,
p: '0',
n: limit.toString()
});
return `${this.baseURL}?${params.toString()}`;
}
_createSongPayload(rawSong) {
return {
name: rawSong.title || rawSong.song || '',
year: rawSong.year || '',
copyright: rawSong.copyright || '',
duration: rawSong.duration || '',
label: rawSong.label || '',
album: {
name: rawSong.album || rawSong.album_name || '',
id: rawSong.album_id || ''
},
artists: {
primary: this._parseArtists(rawSong.primary_artists || rawSong.artists || []),
featured: this._parseArtists(rawSong.featured_artists || []),
all: this._parseArtists(rawSong.all_artists || [])
},
downloadUrl: rawSong.download_url || rawSong.media_url || rawSong.media_preview_url || '',
image: rawSong.image || rawSong.thumbnail || '',
language: rawSong.language || '',
genre: rawSong.genre || ''
};
}
_parseArtists(artists) {
if (!Array.isArray(artists)) {
return [];
}
return artists.map(artist => {
if (typeof artist === 'string') {
return { name: artist, id: '' };
}
return {
name: artist.name || artist.title || '',
id: artist.id || ''
};
});
}
_findMatchingTrack(processedResults, title, artist, debug) {
const normalizeString = (text) => {
return (text || '').toString().trim().toLowerCase().replace(/[^\w\s]/g, '');
};
const startsEither = (a, b) => {
return a.startsWith(b) || b.startsWith(a);
};
for (const track of processedResults) {
// Get all artist names
const primaryArtists = (track.artists?.primary || []).map(a => a.name?.trim()).filter(Boolean);
const singers = (track.artists?.all || []).filter(a => a.role === 'singer').map(a => a.name?.trim()).filter(Boolean);
const allArtists = [...primaryArtists, ...singers];
// Check if artist matches
const artistInput = normalizeString(artist);
let artistMatches = false;
if (allArtists.length > 0) {
for (const trackArtistName of allArtists) {
if (startsEither(artistInput, normalizeString(trackArtistName))) {
artistMatches = true;
break;
}
}
}
// Check if title matches
const titleMatches = startsEither(normalizeString(title), normalizeString(track.name));
if (debug) {
console.log(`[JioSaavn][match_check] track='${track.name}' artists=${allArtists} title_matches=${titleMatches} artist_matches=${artistMatches}`);
}
if (titleMatches && (artistMatches || allArtists.length === 0)) {
return track;
}
}
// Fallback: choose the first with title match only
const titleOnly = processedResults.find(t =>
startsEither(normalizeString(title), normalizeString(t.name))
);
if (titleOnly) {
console.log('[JioSaavn] Falling back to title-only match');
return titleOnly;
}
// Log a concise summary to help debugging
const sample = processedResults.slice(0, 5).map(tr => ({
name: tr.name,
artists: (tr.artists?.primary || []).map(a => a.name)
}));
console.log(`[JioSaavn] No exact match after processing. sample_candidates=${JSON.stringify(sample)}`);
return null;
}
}
module.exports = JioSaavn;
|