Playlist Downloads are now working
[DeezloaderRemix.git] / app / lib / deezer-api / obj / Track.js
1 const _md5 = require('../utils.js').md5
2 const _ecbCrypt = require('../utils.js').ecbCrypt
3
4 module.exports = class Track {
5   constructor(body){
6     switch(body.sourcePage){
7       case 'song.getData':
8         this.id = body.results.SNG_ID
9         this.title = body.results.SNG_TITLE
10         this.duration = body.results.DURATION
11         this.MD5 = body.results.MD5_ORIGIN
12         this.mediaVersion = body.results.MEDIA_VERSION
13         if (body.type == -1){
14           this.filesize = body.results.FILESIZE
15           this.album = {id: 0, title: body.results.ALB_NAME, picture: body.results.ALB_PICTURE}
16           this.artist = {id: 0, name: body.results.ART_NAME}
17           this.artists = [{id: 0, name: body.results.ART_NAME}]
18           this.recordType = -1
19         }else{
20           this.filesize = {
21             default: parseInt(body.results.FILESIZE),
22             mp3_128: parseInt(body.results.FILESIZE_MP3_128),
23             mp3_320: parseInt(body.results.FILESIZE_MP3_320),
24             flac: parseInt(body.results.FILESIZE_FLAC),
25           }
26           this.album = {id: body.results.ALB_ID, title: body.results.ALB_NAME, picture: body.results.ALB_PICTURE}
27           this.artist = {id: body.results.ART_ID, name: body.results.ART_NAME}
28           this.artists = []
29           body.results.ARTISTS.forEach(artist=>{
30             if (artist.__TYPE__ == "artist") this.artists.push({
31               id: artist.ART_ID,
32               name: artist.ART_NAME,
33               picture: artist.ART_PICTURE
34             })
35           })
36           this.gain = body.results.GAIN
37           this.discNumber = body.results.DISK_NUMBER
38           this.trackNumber = body.results.TRACK_NUMBER
39           this.explicit = body.results.EXPLICIT_LYRICS
40           this.ISRC = body.results.ISRC
41           this.contributor = body.results.SNG_CONTRIBUTORS
42           this.lyricsId = body.results.LYRICS_ID
43           this.date = {
44             day: body.results.PHYSICAL_RELEASE_DATE.slice(8,10),
45             month: body.results.PHYSICAL_RELEASE_DATE.slice(5,7),
46             year: body.results.PHYSICAL_RELEASE_DATE.slice(0, 4)
47           }
48         }
49       break
50       case 'deezer.pageTrack':
51         this.id = body.results.DATA.SNG_ID
52         this.title = `${body.results.DATA.SNG_TITLE}${body.results.DATA.VERSION ? ` ${body.results.DATA.VERSION}`: ""}`
53         this.duration = body.results.DATA.DURATION
54         this.filesize = {
55           default: parseInt(body.results.DATA.FILESIZE),
56           mp3_128: parseInt(body.results.DATA.FILESIZE_MP3_128),
57           mp3_320: parseInt(body.results.DATA.FILESIZE_MP3_320),
58           flac: parseInt(body.results.DATA.FILESIZE_FLAC),
59         }
60         this.MD5 = body.results.DATA.MD5_ORIGIN
61         this.mediaVersion = body.results.DATA.MEDIA_VERSION
62         this.fallbackId = (body.results.DATA.FALLBACK ? (body.results.DATA.FALLBACK.SNG_ID ? body.results.DATA.FALLBACK.SNG_ID : 0) : 0)
63         this.album = {id: body.results.DATA.ALB_ID, title: body.results.DATA.ALB_TITLE, picture: body.results.DATA.ALB_PICTURE}
64         this.artist = {id: body.results.DATA.ART_ID, name: body.results.DATA.ART_NAME, picture: body.results.DATA.ART_PICTURE}
65         this.artists = []
66         body.results.DATA.ARTISTS.forEach(artist=>{
67           if (artist.__TYPE__ == "artist") this.artists.push({
68             id: artist.ART_ID,
69             name: artist.ART_NAME,
70             picture: artist.ART_PICTURE
71           })
72         })
73         this.gain = body.results.DATA.GAIN
74         this.discNumber = body.results.DATA.DISK_NUMBER
75         this.trackNumber = body.results.DATA.TRACK_NUMBER
76         this.explicit = body.results.DATA.EXPLICIT_LYRICS
77         this.ISRC = body.results.DATA.ISRC
78         this.copyright = body.results.DATA.COPYRIGHT
79         this.recordType = body.results.DATA.TYPE
80         this.contributor = body.results.DATA.SNG_CONTRIBUTORS
81         if (body.results.LYRICS){
82           this.unsyncLyrics = {
83                         description: "",
84                         lyrics: body.results.LYRICS.LYRICS_TEXT
85                 }
86           this.syncLyrics = ""
87           for(let i=0; i < body.results.LYRICS.LYRICS_SYNC_JSON.length; i++){
88                                 if(body.results.LYRICS.LYRICS_SYNC_JSON[i].lrc_timestamp){
89                                         this.syncLyrics += body.results.LYRICS.LYRICS_SYNC_JSON[i].lrc_timestamp + body.results.LYRICS.LYRICS_SYNC_JSON[i].line+"\r\n";
90                                 }else if(i+1 < body.results.LYRICS.LYRICS_SYNC_JSON.length){
91                                         this.syncLyrics += body.results.LYRICS.LYRICS_SYNC_JSON[i+1].lrc_timestamp + body.results.LYRICS.LYRICS_SYNC_JSON[i].line+"\r\n";
92                                 }
93                         }
94         }
95         this.date = {
96           day: body.results.DATA.PHYSICAL_RELEASE_DATE.slice(8,10),
97           month: body.results.DATA.PHYSICAL_RELEASE_DATE.slice(5,7),
98           year: body.results.DATA.PHYSICAL_RELEASE_DATE.slice(0, 4)
99         }
100       break
101       case 'deezer.pageAlbum':
102         this.id = body.SNG_ID
103         this.title = `${body.SNG_TITLE}${body.VERSION ? ` ${body.VERSION}`: ""}`
104         this.duration = body.DURATION
105         this.filesize = {
106           default: parseInt(body.FILESIZE),
107           mp3_128: parseInt(body.FILESIZE_MP3_128),
108           mp3_320: parseInt(body.FILESIZE_MP3_320),
109           flac: parseInt(body.FILESIZE_FLAC),
110         }
111         this.MD5 = body.MD5_ORIGIN
112         this.mediaVersion = body.MEDIA_VERSION
113         this.fallbackId = (body.FALLBACK ? (body.FALLBACK.SNG_ID ? body.FALLBACK.SNG_ID : 0) : 0)
114         this.album = {id: body.ALB_ID, title: body.ALB_TITLE, picture: body.ALB_PICTURE}
115         this.artist = {id: body.ART_ID, name: body.ART_NAME, picture: body.ART_PICTURE}
116         this.artists = []
117         body.ARTISTS.forEach(artist=>{
118           if (artist.__TYPE__ == "artist") this.artists.push({
119             id: artist.ART_ID,
120             name: artist.ART_NAME,
121             picture: artist.ART_PICTURE
122           })
123         })
124         this.gain = body.GAIN
125         this.discNumber = body.DISK_NUMBER
126         this.trackNumber = body.TRACK_NUMBER
127         this.explicit = body.EXPLICIT_LYRICS
128         this.ISRC = body.ISRC
129         this.recordType = body.TYPE
130         this.contributor = body.SNG_CONTRIBUTORS
131         this.lyricsId = body.LYRICS_ID
132       break
133       case 'song.getListByAlbum':
134       case 'playlist.getSongs':
135         this.id = body.SNG_ID
136         this.title = `${body.SNG_TITLE}${body.VERSION ? ` ${body.VERSION}`: ""}`
137         this.duration = body.DURATION
138         this.MD5 = body.MD5_ORIGIN
139         this.mediaVersion = body.MEDIA_VERSION
140         this.fallbackId = (body.FALLBACK ? (body.FALLBACK.SNG_ID ? body.FALLBACK.SNG_ID : 0) : 0)
141         this.album = {id: body.ALB_ID, title: body.ALB_TITLE, picture: body.ALB_PICTURE}
142         this.artist = {id: body.ART_ID, name: body.ART_NAME, picture: body.ART_PICTURE}
143         this.artistsString = []
144         if (parseInt(this.id) > 0){
145           this.filesize = {
146             default: parseInt(body.FILESIZE),
147             mp3_128: parseInt(body.FILESIZE_MP3_128),
148             mp3_320: parseInt(body.FILESIZE_MP3_320),
149             flac: parseInt(body.FILESIZE_FLAC),
150           }
151           if (body.SNG_CONTRIBUTORS.main_artist){
152             this.artistsString = this.artistsString.concat(body.SNG_CONTRIBUTORS.main_artist)
153           }else if (body.SNG_CONTRIBUTORS.mainartist){
154             this.artistsString = this.artistsString.concat(body.SNG_CONTRIBUTORS.mainartist)
155           }
156           if (body.SNG_CONTRIBUTORS.associatedperformer) {
157             this.artistsString = this.artistsString.concat(body.SNG_CONTRIBUTORS.associatedperformer)
158           }
159         }else{
160           this.filesize = body.FILESIZE
161         }
162         if (!this.artistsString[0]) this.artistsString.push(this.artist.name)
163         this.gain = body.GAIN
164         this.discNumber = body.DISK_NUMBER
165         this.trackNumber = body.TRACK_NUMBER
166         this.explicit = body.EXPLICIT_LYRICS
167         this.ISRC = body.ISRC
168         this.recordType = body.TYPE
169         this.contributor = body.SNG_CONTRIBUTORS
170         this.lyricsId = body.LYRICS_ID
171       break
172     }
173   }
174
175   getDownloadUrl(format){
176     var urlPart = this.MD5+"¤"+format+"¤"+this.id+"¤"+this.mediaVersion
177     var md5val = _md5(urlPart)
178     urlPart = _ecbCrypt('jo6aey6haid2Teih', md5val+"¤"+urlPart+"¤")
179     return "https://e-cdns-proxy-" + this.MD5.substring(0, 1) + ".dzcdn.net/mobile/1/" + urlPart
180   }
181 }