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