a04f13fc49468f997f7dbc884172d7947f532b43
[DeezloaderRemix.git] / app / lib / deezer-api / obj / Track.js
1
2 const crypto = require('crypto')
3
4 function _md5 (data) {
5   let md5sum = crypto.createHash('md5')
6   md5sum.update(new Buffer(data, 'binary'))
7   return md5sum.digest('hex')
8 }
9
10 function _ecbCrypt (key, data) {
11   let cipher = crypto.createCipheriv("aes-128-ecb", new Buffer(key), new Buffer(""));
12   return Buffer.concat([cipher.update(data, 'binary'), cipher.final()]).toString("hex").toLowerCase();
13 }
14
15 module.exports = class Track {
16   constructor(body){
17     if (body.type == -1){
18       this.id = body.results.SNG_ID
19       this.title = body.results.SNG_TITLE
20       this.duration = body.results.DURATION
21       this.filesize = body.results.FILESIZE
22       this.MD5 = body.results.MD5_ORIGIN
23       this.mediaVersion = body.results.MEDIA_VERSION
24       this.album = {id: 0, title: body.results.ALB_NAME, picture: body.results.ALB_PICTURE}
25       this.artist = {id: 0, name: body.results.ART_NAME}
26       this.recordType = -1
27     } else {
28       this.id = body.results.DATA.SNG_ID
29       this.title = `${body.results.DATA.SNG_TITLE}${body.results.DATA.VERSION ? ` ${body.results.DATA.VERSION}`: ""}`
30       this.duration = body.results.DATA.DURATION
31       this.filesize = {
32         default: parseInt(body.results.DATA.FILESIZE),
33         mp3_128: parseInt(body.results.DATA.FILESIZE_MP3_128),
34         mp3_320: parseInt(body.results.DATA.FILESIZE_MP3_320),
35         flac: parseInt(body.results.DATA.FILESIZE_FLAC),
36       }
37       this.MD5 = body.results.DATA.MD5_ORIGIN
38       this.mediaVersion = body.results.DATA.MEDIA_VERSION
39       this.fallbackId = (body.results.DATA.FALLBACK ? (body.results.DATA.FALLBACK.SNG_ID ? body.results.DATA.FALLBACK.SNG_ID : 0) : 0)
40       this.album = {id: body.results.DATA.ALB_ID, title: body.results.DATA.ALB_NAME, picture: body.results.DATA.ALB_PICTURE}
41       this.mainArtist = {id: body.results.DATA.ART_ID, name: body.results.DATA.ART_NAME, picture: body.results.DATA.ART_PICTURE}
42       this.artist = []
43       body.results.DATA.ARTISTS.forEach(artist=>{
44         if (artist.__TYPE__ == "artist") this.artist.push({
45           id: artist.ART_ID,
46           name: artist.ART_NAME,
47           picture: artist.ART_PICTURE
48         })
49       })
50       this.gain = body.results.DATA.GAIN
51       this.discNumber = body.results.DATA.DISK_NUMBER
52       this.trackNumber = body.results.DATA.TRACK_NUMBER
53       this.explicit = body.results.DATA.EXPLICIT_LYRICS
54       this.ISRC = body.results.DATA.ISRC
55       this.copyright = body.results.DATA.COPYRIGHT
56       this.recordType = body.results.DATA.TYPE
57       this.contributor = body.results.DATA.SNG_CONTRIBUTORS
58       this.unsyncLyrics = body.results.LYRICS.LYRICS_TEXT
59       this.syncLyrics = ""
60       for(let i=0; i < body.results.LYRICS.LYRICS_SYNC_JSON.length; i++){
61                                 if(body.results.LYRICS.LYRICS_SYNC_JSON[i].lrc_timestamp){
62                                         this.syncLyrics += body.results.LYRICS.LYRICS_SYNC_JSON[i].lrc_timestamp + body.results.LYRICS.LYRICS_SYNC_JSON[i].line+"\r\n";
63                                 }else if(i+1 < body.results.LYRICS.LYRICS_SYNC_JSON.length){
64                                         this.syncLyrics += body.results.LYRICS.LYRICS_SYNC_JSON[i+1].lrc_timestamp + body.results.LYRICS.LYRICS_SYNC_JSON[i].line+"\r\n";
65                                 }
66                         }
67       this.date = {
68         day: body.results.DATA.PHYSICAL_RELEASE_DATE.slice(8,10),
69         month: body.results.DATA.PHYSICAL_RELEASE_DATE.slice(5,7),
70         year: body.results.DATA.PHYSICAL_RELEASE_DATE.slice(0, 4)
71       }
72     }
73   }
74
75   getDownloadUrl(format){
76     var urlPart = this.MD5+"¤"+format+"¤"+this.id+"¤"+this.mediaVersion
77     var md5val = _md5(urlPart)
78     urlPart = _ecbCrypt('jo6aey6haid2Teih', md5val+"¤"+urlPart+"¤")
79     return "https://e-cdns-proxy-" + this.MD5.substring(0, 1) + ".dzcdn.net/mobile/1/" + urlPart
80   }
81 }