diff --git a/deemix/downloader.js b/deemix/downloader.js index 07a7bbd..adafd4c 100644 --- a/deemix/downloader.js +++ b/deemix/downloader.js @@ -23,6 +23,17 @@ const extensions = { [TrackFormats.MP4_RA1]: '.mp4' } +const formatsName = { + [TrackFormats.FLAC]: 'FLAC', + [TrackFormats.LOCAL]: 'MP3_MISC', + [TrackFormats.MP3_320]: 'MP3_320', + [TrackFormats.MP3_128]: 'MP3_128', + [TrackFormats.DEFAULT]: 'MP3_MISC', + [TrackFormats.MP4_RA3]: 'MP4_RA3', + [TrackFormats.MP4_RA2]: 'MP4_RA2', + [TrackFormats.MP4_RA1]: 'MP4_RA1' +} + const TEMPDIR = tmpdir()+`/deemix-imgs` fs.mkdirSync(TEMPDIR, { recursive: true }) @@ -89,8 +100,10 @@ async function getPreferredBitrate(track, bitrate, shouldFallback, uuid, listene async function testBitrate(track, formatNumber, formatName){ let request try { + if (!track.urls[formatName]) + track.urls[formatName] = generateCryptedStreamURL(track.id, track.MD5, track.mediaVersion, formatNumber) request = got.get( - generateCryptedStreamURL(track.id, track.MD5, track.mediaVersion, formatNumber), + track.urls[formatName], { headers: {'User-Agent': USER_AGENT_HEADER}, timeout: 30000 } ).on("response", (response)=>{ track.filesizes[`FILESIZE_${formatName}`] = response.headers["content-length"] @@ -409,7 +422,9 @@ class Downloader { // Download the track if (!trackAlreadyDownloaded || this.settings.overwriteFile == OverwriteOption.OVERWRITE){ - track.downloadURL = generateCryptedStreamURL(track.id, track.MD5, track.mediaVersion, track.bitrate) + if (!track.urls[formatsName[track.bitrate]]) + track.urls[formatsName[track.bitrate]] = generateCryptedStreamURL(track.id, track.MD5, track.mediaVersion, track.bitrate) + track.downloadURL = track.urls[formatsName[track.bitrate]] let stream = fs.createWriteStream(writepath) try { await streamTrack(stream, track, 0, this.downloadObject, this.listener) @@ -478,6 +493,7 @@ class Downloader { this.warn(itemData, e.errid, 'fallback') let newTrack = await this.dz.gw.get_track_with_fallback(track.fallbackID) track.parseEssentialData(newTrack) + await track.retriveTrackURLs(this.dz) await track.retriveFilesizes(this.dz) return await this.downloadWrapper(extraData, track) } @@ -487,6 +503,7 @@ class Downloader { if (searchedID != "0"){ let newTrack = await this.dz.gw.get_track_with_fallback(searchedID) track.parseEssentialData(newTrack) + await track.retriveTrackURLs(this.dz) await track.retriveFilesizes(this.dz) track.searched = true if (this.listener) this.listener.send('queueUpdate', { diff --git a/deemix/types/Track.js b/deemix/types/Track.js index 8a725df..692fdeb 100644 --- a/deemix/types/Track.js +++ b/deemix/types/Track.js @@ -22,6 +22,7 @@ class Track { this.title = "", this.MD5 = "" this.mediaVersion = "" + this.trackToken = "" this.duration = 0 this.fallbackID = "0" this.filesizes = {} @@ -48,18 +49,20 @@ class Track { this.artistsString = "" this.mainArtistsString = "" this.featArtistsString = "" + this.urls = {} } parseEssentialData(trackAPI_gw, trackAPI){ this.id = String(trackAPI_gw.SNG_ID) this.duration = trackAPI_gw.DURATION + this.trackToken = trackAPI_gw.TRACK_TOKEN this.MD5 = trackAPI_gw.MD5_ORIGIN if (!this.MD5){ if (trackAPI && trackAPI.md5_origin){ this.MD5 = trackAPI.md5_origin - }else{ + }/*else{ throw new MD5NotFound - } + }*/ } this.mediaVersion = trackAPI_gw.MEDIA_VERSION this.fallbackID = "0" @@ -113,6 +116,7 @@ class Track { } this.parseEssentialData(trackAPI_gw, trackAPI) + this.retriveTrackURLs(dz) if (this.localTrack){ this.parseLocalTrackData(trackAPI_gw) @@ -255,6 +259,14 @@ class Track { }); } + async retriveTrackURLs(dz){ + let urls = await dz.get_tracks_urls(this.trackToken) + this.urls = {} + urls[0].media.forEach(url => { + this.urls[url.format] = url.sources[0].url + }) + } + removeDuplicateArtists(){ [this.artist, this.artists] = removeDuplicateArtists(this.artist, this.artists) } @@ -393,5 +405,8 @@ class AlbumDoesntExists extends TrackError { module.exports = { Track, + TrackError, + MD5NotFound, + NoDataToParse, AlbumDoesntExists }