Simplified Deezer API
authorRemixDev <deezloaderremix@gmail.com>
Mon, 4 Feb 2019 11:19:25 +0000 (12:19 +0100)
committerRemixDev <deezloaderremix@gmail.com>
Mon, 4 Feb 2019 11:19:25 +0000 (12:19 +0100)
app/lib/deezer-api/index.js
app/lib/deezer-api/obj/Album.js
app/lib/deezer-api/obj/Track.js

index ef3b9c009f189b7829e8828ebd8d7709f98d9545..a13fd1e27fcc2fa1326d42487ed00ccb7b9882d6 100644 (file)
@@ -150,11 +150,10 @@ module.exports = class Deezer {
     var body
     if (id<0){
       body = await this.apiCall(`song.getData`, {sng_id: id})
-      body.results.sourcePage = 'song.getData'
-      body.results.type = -1
     }else{
       body = await this.apiCall(`deezer.pageTrack`, {sng_id: id})
-      body.results.sourcePage = 'deezer.pageTrack'
+                       if (body.results.LYRICS) body.results.DATA.LYRICS = body.results.LYRICS
+      body.results = body.results.DATA
     }
     return new Track(body.results)
   }
@@ -163,7 +162,6 @@ module.exports = class Deezer {
     var tracksArray = []
     var body = await this.apiCall(`song.getListData`, {sng_ids: ids})
     body.results.data.forEach(track=>{
-      track.sourcePage = 'song.getData'
       tracksArray.push(new Track(track))
     })
     return tracksArray
@@ -171,11 +169,11 @@ module.exports = class Deezer {
 
   async getAlbum(id){
     var body = await this.apiCall(`album.getData`, {alb_id: id})
-    body.results.sourcePage = 'album.getData'
     /*
     Alternative query, currently not used
       var body = await this.apiCall(`deezer.pageAlbum`, {alb_id: id, lang: 'en'})
-      body.sourcePage = 'deezer.pageAlbum'
+                       if (body.results.SONGS) body.results.DATA.SONGS = body.results.SONGS
+                       body.results = body.results.DATA
     */
     return new Album(body.results)
   }
@@ -184,7 +182,6 @@ module.exports = class Deezer {
     var tracksArray = []
     var body = await this.apiCall(`song.getListByAlbum`, {alb_id: id, nb: -1})
     body.results.data.forEach(track=>{
-      track.sourcePage = 'song.getListByAlbum'
       tracksArray.push(new Track(track))
     })
     return tracksArray
@@ -204,7 +201,6 @@ module.exports = class Deezer {
     var tracksArray = []
     var body = await this.apiCall(`playlist.getSongs`, {playlist_id: id, nb: -1})
     body.results.data.forEach((track, index)=>{
-      track.sourcePage = 'playlist.getSongs'
       let _track = new Track(track)
       _track.position = index
       tracksArray.push(_track)
@@ -216,7 +212,6 @@ module.exports = class Deezer {
     var tracksArray = []
     var body = await this.apiCall(`artist.getTopTrack`, {art_id: id, nb: 100})
     body.results.data.forEach((track, index)=>{
-      track.sourcePage = 'artist.getTopTrack'
       let _track = new Track(track)
       _track.position = index
       tracksArray.push(_track)
index a79039156c3075c378f6e74d984ff8042c360b51..526656e406e76110dc0bf363ce0d59815faf868b 100644 (file)
@@ -2,55 +2,36 @@ const Track = require('./Track.js')
 
 module.exports = class Album {
   constructor(body){
-    switch (body.sourcePage){
-      case 'album.getData':
-        this.id = body.ALB_ID
-        this.title = body.ALB_TITLE
-        this.picture = body.ALB_PICTURE
-        this.artist = {
-          id: body.ART_ID,
-          name: body.ART_NAME
-        }
-        this.label = body.LABEL_NAME
-        this.discTotal = body.NUMBER_DISK
-        this.trackTotal = body.NUMBER_TRACK
-        this.explicit = body.EXPLICIT_ALBUM_CONTENT.EXPLICIT_LYRICS_STATUS > 0
-        this.date = {
-          day: body.PHYSICAL_RELEASE_DATE.slice(8,10),
-          month: body.PHYSICAL_RELEASE_DATE.slice(5,7),
-          year: body.PHYSICAL_RELEASE_DATE.slice(0, 4)
-        }
-      break
-      case 'deezer.pageAlbum':
-        this.id = body.DATA.ALB_ID
-        this.title = body.DATA.ALB_TITLE
-        this.picture = body.DATA.ALB_PICTURE
-        this.artist = {
-          id: body.DATA.ART_ID,
-          name: body.DATA.ART_NAME
-        }
-        body.DATA.ARTISTS.forEach(artist=>{
-          if (artist.__TYPE__ == "artist") this.artists.push({
-            id: artist.ART_ID,
-            name: artist.ART_NAME,
-            picture: artist.ART_PICTURE
-          })
-        })
-        this.label = body.DATA.LABEL_NAME
-        this.date = {
-          day: body.DATA.PHYSICAL_RELEASE_DATE.slice(8,10),
-          month: body.DATA.PHYSICAL_RELEASE_DATE.slice(5,7),
-          year: body.DATA.PHYSICAL_RELEASE_DATE.slice(0, 4)
-        }
-        this.explicit = body.DATA.EXPLICIT_ALBUM_CONTENT.EXPLICIT_LYRICS_STATUS > 0
-        this.barcode = body.DATA.UPC
-        this.trackTotal = body.SONGS.total
-        this.tracks = []
-        body.SONGS.data.forEach(track=>{
-          track.sourcePage = 'deezer.pageAlbum'
-          this.tracks.push(new Track(track))
-        })
-      break
-    }
+         this.id = body.ALB_ID
+         this.title = body.ALB_TITLE
+         this.picture = body.ALB_PICTURE
+         this.artist = {
+           id: body.ART_ID,
+           name: body.ART_NAME
+         }
+         this.label = body.LABEL_NAME
+         this.discTotal = body.NUMBER_DISK
+         this.trackTotal = body.NUMBER_TRACK
+         this.explicit = body.EXPLICIT_ALBUM_CONTENT.EXPLICIT_LYRICS_STATUS > 0
+               this.barcode = body.UPC
+         this.date = {
+           day: body.PHYSICAL_RELEASE_DATE.slice(8,10),
+           month: body.PHYSICAL_RELEASE_DATE.slice(5,7),
+           year: body.PHYSICAL_RELEASE_DATE.slice(0, 4)
+         }
+               if (body.ARTISTS){
+                       body.ARTISTS.forEach(artist=>{
+                        if (artist.__TYPE__ == "artist") this.artists.push({
+                                id: artist.ART_ID,
+                                name: artist.ART_NAME,
+                                picture: artist.ART_PICTURE
+                        })
+                })
+               }
+        if (body.SONGS){
+                body.SONGS.data.forEach(track=>{
+           this.tracks.push(new Track(track))
+         })
+        }
   }
 }
index 92bd9ab2d986b78846c032d136870ca2d3e0f5c0..4de5d7b90a99a9fb9493e9adf6b66f0493d2e0b8 100644 (file)
@@ -3,175 +3,77 @@ const _ecbCrypt = require('../utils.js').ecbCrypt
 
 module.exports = class Track {
   constructor(body){
-    switch(body.sourcePage){
-      case 'song.getData':
-        this.id = body.SNG_ID
-        this.title = `${body.SNG_TITLE}${body.VERSION ? ` ${body.VERSION}`: ""}`
-        this.duration = body.DURATION
-        this.MD5 = body.MD5_ORIGIN
-        this.mediaVersion = body.MEDIA_VERSION
-        if (body.type == -1){
-          this.filesize = body.FILESIZE
-          this.album = {id: 0, title: body.ALB_TITLE, picture: body.ALB_PICTURE}
-          this.artist = {id: 0, name: body.ART_NAME}
-          this.artists = [{id: 0, name: body.ART_NAME}]
-          this.recordType = -1
-        }else{
-          this.filesize = {
-            default: parseInt(body.FILESIZE),
-            mp3_128: parseInt(body.FILESIZE_MP3_128),
-            mp3_320: parseInt(body.FILESIZE_MP3_320),
-            flac: parseInt(body.FILESIZE_FLAC),
-          }
-          this.album = {id: body.ALB_ID, title: body.ALB_TITLE, picture: body.ALB_PICTURE}
-          this.artist = {id: body.ART_ID, name: body.ART_NAME}
-          this.artists = []
-          body.ARTISTS.forEach(artist=>{
-            if (artist.__TYPE__ == "artist") this.artists.push({
-              id: artist.ART_ID,
-              name: artist.ART_NAME,
-              picture: artist.ART_PICTURE
-            })
-          })
-          this.gain = body.GAIN
-          this.discNumber = body.DISK_NUMBER
-          this.trackNumber = body.TRACK_NUMBER
-          this.explicit = body.EXPLICIT_LYRICS
-          this.ISRC = body.ISRC
-          this.contributor = body.SNG_CONTRIBUTORS
-          this.lyricsId = body.LYRICS_ID
-          this.date = {
-            day: body.PHYSICAL_RELEASE_DATE.slice(8,10),
-            month: body.PHYSICAL_RELEASE_DATE.slice(5,7),
-            year: body.PHYSICAL_RELEASE_DATE.slice(0, 4)
-          }
-        }
-      break
-      case 'deezer.pageTrack':
-        this.id = body.DATA.SNG_ID
-        this.title = `${body.DATA.SNG_TITLE}${body.DATA.VERSION ? ` ${body.DATA.VERSION}`: ""}`
-        this.duration = body.DATA.DURATION
-        this.filesize = {
-          default: parseInt(body.DATA.FILESIZE),
-          mp3_128: parseInt(body.DATA.FILESIZE_MP3_128),
-          mp3_320: parseInt(body.DATA.FILESIZE_MP3_320),
-          flac: parseInt(body.DATA.FILESIZE_FLAC),
-        }
-        this.MD5 = body.DATA.MD5_ORIGIN
-        this.mediaVersion = body.DATA.MEDIA_VERSION
-        this.fallbackId = (body.DATA.FALLBACK ? (body.DATA.FALLBACK.SNG_ID ? body.DATA.FALLBACK.SNG_ID : 0) : 0)
-        this.album = {id: body.DATA.ALB_ID, title: body.DATA.ALB_TITLE, picture: body.DATA.ALB_PICTURE}
-        this.artist = {id: body.DATA.ART_ID, name: body.DATA.ART_NAME, picture: body.DATA.ART_PICTURE}
-        this.artists = []
-        body.DATA.ARTISTS.forEach(artist=>{
-          if (artist.__TYPE__ == "artist") this.artists.push({
-            id: artist.ART_ID,
-            name: artist.ART_NAME,
-            picture: artist.ART_PICTURE
-          })
-        })
-        this.gain = body.DATA.GAIN
-        this.discNumber = body.DATA.DISK_NUMBER
-        this.trackNumber = body.DATA.TRACK_NUMBER
-        this.explicit = body.DATA.EXPLICIT_LYRICS
-        this.ISRC = body.DATA.ISRC
-        this.copyright = body.DATA.COPYRIGHT
-        this.recordType = body.DATA.TYPE
-        this.contributor = body.DATA.SNG_CONTRIBUTORS
-        if (body.LYRICS){
-          this.unsyncLyrics = {
-                       description: "",
-                       lyrics: body.LYRICS.LYRICS_TEXT
-               }
-                                       if (body.LYRICS.LYRICS_SYNC_JSON){
-                                               this.syncLyrics = ""
-                 for(let i=0; i < body.LYRICS.LYRICS_SYNC_JSON.length; i++){
-                                       if(body.LYRICS.LYRICS_SYNC_JSON[i].lrc_timestamp){
-                                               this.syncLyrics += body.LYRICS.LYRICS_SYNC_JSON[i].lrc_timestamp + body.LYRICS.LYRICS_SYNC_JSON[i].line+"\r\n";
-                                       }else if(i+1 < body.LYRICS.LYRICS_SYNC_JSON.length){
-                                               this.syncLyrics += body.LYRICS.LYRICS_SYNC_JSON[i+1].lrc_timestamp + body.LYRICS.LYRICS_SYNC_JSON[i].line+"\r\n";
-                                       }
-                               }
-                                       }
-        }
-        this.date = {
-          day: body.DATA.PHYSICAL_RELEASE_DATE.slice(8,10),
-          month: body.DATA.PHYSICAL_RELEASE_DATE.slice(5,7),
-          year: body.DATA.PHYSICAL_RELEASE_DATE.slice(0, 4)
-        }
-      break
-      case 'deezer.pageAlbum':
-        this.id = body.SNG_ID
-        this.title = `${body.SNG_TITLE}${body.VERSION ? ` ${body.VERSION}`: ""}`
-        this.duration = body.DURATION
-        this.filesize = {
-          default: parseInt(body.FILESIZE),
-          mp3_128: parseInt(body.FILESIZE_MP3_128),
-          mp3_320: parseInt(body.FILESIZE_MP3_320),
-          flac: parseInt(body.FILESIZE_FLAC),
-        }
-        this.MD5 = body.MD5_ORIGIN
-        this.mediaVersion = body.MEDIA_VERSION
-        this.fallbackId = (body.FALLBACK ? (body.FALLBACK.SNG_ID ? body.FALLBACK.SNG_ID : 0) : 0)
-        this.album = {id: body.ALB_ID, title: body.ALB_TITLE, picture: body.ALB_PICTURE}
-        this.artist = {id: body.ART_ID, name: body.ART_NAME, picture: body.ART_PICTURE}
-        this.artists = []
-        body.ARTISTS.forEach(artist=>{
-          if (artist.__TYPE__ == "artist") this.artists.push({
-            id: artist.ART_ID,
-            name: artist.ART_NAME,
-            picture: artist.ART_PICTURE
-          })
-        })
-        this.gain = body.GAIN
-        this.discNumber = body.DISK_NUMBER
-        this.trackNumber = body.TRACK_NUMBER
-        this.explicit = body.EXPLICIT_LYRICS
-        this.ISRC = body.ISRC
-        this.recordType = body.TYPE
-        this.contributor = body.SNG_CONTRIBUTORS
-        this.lyricsId = body.LYRICS_ID
-      break
-      case 'song.getListByAlbum':
-      case 'playlist.getSongs':
-      case 'artist.getTopTrack':
-        this.id = body.SNG_ID
-        this.title = `${body.SNG_TITLE}${body.VERSION ? ` ${body.VERSION}`: ""}`
-        this.duration = body.DURATION
-        this.MD5 = body.MD5_ORIGIN
-        this.mediaVersion = body.MEDIA_VERSION
-        this.fallbackId = (body.FALLBACK ? (body.FALLBACK.SNG_ID ? body.FALLBACK.SNG_ID : 0) : 0)
-        this.album = {id: body.ALB_ID, title: body.ALB_TITLE, picture: body.ALB_PICTURE}
-        this.artist = {id: body.ART_ID, name: body.ART_NAME, picture: body.ART_PICTURE}
-        this.artistsString = []
-        if (parseInt(this.id) > 0){
-          this.filesize = {
-            default: parseInt(body.FILESIZE),
-            mp3_128: parseInt(body.FILESIZE_MP3_128),
-            mp3_320: parseInt(body.FILESIZE_MP3_320),
-            flac: parseInt(body.FILESIZE_FLAC),
-          }
-          if (body.SNG_CONTRIBUTORS.main_artist){
-            this.artistsString = this.artistsString.concat(body.SNG_CONTRIBUTORS.main_artist)
-          }else if (body.SNG_CONTRIBUTORS.mainartist){
-            this.artistsString = this.artistsString.concat(body.SNG_CONTRIBUTORS.mainartist)
-          }
-          if (body.SNG_CONTRIBUTORS.associatedperformer) {
-            this.artistsString = this.artistsString.concat(body.SNG_CONTRIBUTORS.associatedperformer)
-          }
-        }else{
-          this.filesize = body.FILESIZE
-        }
-        if (!this.artistsString[0]) this.artistsString.push(this.artist.name)
-        this.gain = body.GAIN
-        this.discNumber = body.DISK_NUMBER
-        this.trackNumber = body.TRACK_NUMBER
-        this.explicit = body.EXPLICIT_LYRICS
-        this.ISRC = body.ISRC
-        this.recordType = body.TYPE
-        this.contributor = body.SNG_CONTRIBUTORS
-        this.lyricsId = body.LYRICS_ID
-      break
+    this.id = body.SNG_ID
+    this.title = `${body.SNG_TITLE}${body.VERSION ? ` ${body.VERSION}`: ""}`
+    this.duration = body.DURATION
+    this.MD5 = body.MD5_ORIGIN
+    this.mediaVersion = body.MEDIA_VERSION
+    if (parseInt(this.id)<0){
+      this.filesize = body.FILESIZE
+      this.album = {id: 0, title: body.ALB_TITLE, picture: body.ALB_PICTURE}
+      this.artist = {id: 0, name: body.ART_NAME}
+      this.artists = [this.artist]
+      this.recordType = -1
+    }else{
+      this.filesize = {
+        default: parseInt(body.FILESIZE),
+        mp3_128: parseInt(body.FILESIZE_MP3_128),
+        mp3_320: parseInt(body.FILESIZE_MP3_320),
+        flac: parseInt(body.FILESIZE_FLAC),
+      }
+                       this.fallbackId = (body.FALLBACK ? (body.FALLBACK.SNG_ID ? body.FALLBACK.SNG_ID : 0) : 0)
+      this.album = {id: body.ALB_ID, title: body.ALB_TITLE, picture: body.ALB_PICTURE}
+      this.artist = {id: body.ART_ID, name: body.ART_NAME, picture: body.ART_PICTURE ? body.ART_PICTURE : null}
+                       this.date = {
+        day: body.PHYSICAL_RELEASE_DATE.slice(8,10),
+        month: body.PHYSICAL_RELEASE_DATE.slice(5,7),
+        year: body.PHYSICAL_RELEASE_DATE.slice(0, 4)
+      }
+                       if (body.ARTISTS){
+                               this.artists = []
+             body.ARTISTS.forEach(artist=>{
+               if (artist.__TYPE__ == "artist") this.artists.push({
+                 id: artist.ART_ID,
+                 name: artist.ART_NAME,
+                 picture: artist.ART_PICTURE
+               })
+             })
+                       }else{
+                               if (body.SNG_CONTRIBUTORS.main_artist){
+               this.artistsString = this.artistsString.concat(body.SNG_CONTRIBUTORS.main_artist)
+             }else if (body.SNG_CONTRIBUTORS.mainartist){
+               this.artistsString = this.artistsString.concat(body.SNG_CONTRIBUTORS.mainartist)
+             }
+             if (body.SNG_CONTRIBUTORS.associatedperformer) {
+               this.artistsString = this.artistsString.concat(body.SNG_CONTRIBUTORS.associatedperformer)
+             }
+                       }
+      this.gain = body.GAIN
+      this.discNumber = body.DISK_NUMBER
+      this.trackNumber = body.TRACK_NUMBER
+      this.explicit = body.EXPLICIT_LYRICS
+      this.ISRC = body.ISRC
+      this.contributor = body.SNG_CONTRIBUTORS
+      this.lyricsId = body.LYRICS_ID
+                       this.copyright = body.COPYRIGHT
+           this.recordType = body.TYPE
+           this.contributor = body.SNG_CONTRIBUTORS
+                       if (body.LYRICS){
+             this.unsyncLyrics = {
+                               description: "",
+                               lyrics: body.LYRICS.LYRICS_TEXT
+                       }
+                               if (body.LYRICS.LYRICS_SYNC_JSON){
+                                       this.syncLyrics = ""
+               for(let i=0; i < body.LYRICS.LYRICS_SYNC_JSON.length; i++){
+                                       if(body.LYRICS.LYRICS_SYNC_JSON[i].lrc_timestamp){
+                                               this.syncLyrics += body.LYRICS.LYRICS_SYNC_JSON[i].lrc_timestamp + body.LYRICS.LYRICS_SYNC_JSON[i].line+"\r\n";
+                                       }else if(i+1 < body.LYRICS.LYRICS_SYNC_JSON.length){
+                                               this.syncLyrics += body.LYRICS.LYRICS_SYNC_JSON[i+1].lrc_timestamp + body.LYRICS.LYRICS_SYNC_JSON[i].line+"\r\n";
+                                       }
+                               }
+                               }
+           }
     }
   }