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