Simplified Deezer API
[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     this.id = body.SNG_ID
7     this.title = `${body.SNG_TITLE}${body.VERSION ? ` ${body.VERSION}`: ""}`
8     this.duration = body.DURATION
9     this.MD5 = body.MD5_ORIGIN
10     this.mediaVersion = body.MEDIA_VERSION
11     if (parseInt(this.id)<0){
12       this.filesize = body.FILESIZE
13       this.album = {id: 0, title: body.ALB_TITLE, picture: body.ALB_PICTURE}
14       this.artist = {id: 0, name: body.ART_NAME}
15       this.artists = [this.artist]
16       this.recordType = -1
17     }else{
18       this.filesize = {
19         default: parseInt(body.FILESIZE),
20         mp3_128: parseInt(body.FILESIZE_MP3_128),
21         mp3_320: parseInt(body.FILESIZE_MP3_320),
22         flac: parseInt(body.FILESIZE_FLAC),
23       }
24                         this.fallbackId = (body.FALLBACK ? (body.FALLBACK.SNG_ID ? body.FALLBACK.SNG_ID : 0) : 0)
25       this.album = {id: body.ALB_ID, title: body.ALB_TITLE, picture: body.ALB_PICTURE}
26       this.artist = {id: body.ART_ID, name: body.ART_NAME, picture: body.ART_PICTURE ? body.ART_PICTURE : null}
27                         this.date = {
28         day: body.PHYSICAL_RELEASE_DATE.slice(8,10),
29         month: body.PHYSICAL_RELEASE_DATE.slice(5,7),
30         year: body.PHYSICAL_RELEASE_DATE.slice(0, 4)
31       }
32                         if (body.ARTISTS){
33                                 this.artists = []
34               body.ARTISTS.forEach(artist=>{
35                 if (artist.__TYPE__ == "artist") this.artists.push({
36                   id: artist.ART_ID,
37                   name: artist.ART_NAME,
38                   picture: artist.ART_PICTURE
39                 })
40               })
41                         }else{
42                                 if (body.SNG_CONTRIBUTORS.main_artist){
43                 this.artistsString = this.artistsString.concat(body.SNG_CONTRIBUTORS.main_artist)
44               }else if (body.SNG_CONTRIBUTORS.mainartist){
45                 this.artistsString = this.artistsString.concat(body.SNG_CONTRIBUTORS.mainartist)
46               }
47               if (body.SNG_CONTRIBUTORS.associatedperformer) {
48                 this.artistsString = this.artistsString.concat(body.SNG_CONTRIBUTORS.associatedperformer)
49               }
50                         }
51       this.gain = body.GAIN
52       this.discNumber = body.DISK_NUMBER
53       this.trackNumber = body.TRACK_NUMBER
54       this.explicit = body.EXPLICIT_LYRICS
55       this.ISRC = body.ISRC
56       this.contributor = body.SNG_CONTRIBUTORS
57       this.lyricsId = body.LYRICS_ID
58                         this.copyright = body.COPYRIGHT
59             this.recordType = body.TYPE
60             this.contributor = body.SNG_CONTRIBUTORS
61                         if (body.LYRICS){
62               this.unsyncLyrics = {
63                                 description: "",
64                                 lyrics: body.LYRICS.LYRICS_TEXT
65                         }
66                                 if (body.LYRICS.LYRICS_SYNC_JSON){
67                                         this.syncLyrics = ""
68                 for(let i=0; i < body.LYRICS.LYRICS_SYNC_JSON.length; i++){
69                                         if(body.LYRICS.LYRICS_SYNC_JSON[i].lrc_timestamp){
70                                                 this.syncLyrics += body.LYRICS.LYRICS_SYNC_JSON[i].lrc_timestamp + body.LYRICS.LYRICS_SYNC_JSON[i].line+"\r\n";
71                                         }else if(i+1 < body.LYRICS.LYRICS_SYNC_JSON.length){
72                                                 this.syncLyrics += body.LYRICS.LYRICS_SYNC_JSON[i+1].lrc_timestamp + body.LYRICS.LYRICS_SYNC_JSON[i].line+"\r\n";
73                                         }
74                                 }
75                                 }
76             }
77     }
78   }
79
80   getDownloadUrl(format){
81     var urlPart = this.MD5+"¤"+format+"¤"+this.id+"¤"+this.mediaVersion
82     var md5val = _md5(urlPart)
83     urlPart = _ecbCrypt('jo6aey6haid2Teih', md5val+"¤"+urlPart+"¤")
84     return "https://e-cdns-proxy-" + this.MD5.substring(0, 1) + ".dzcdn.net/mobile/1/" + urlPart
85   }
86 }