Added support for Spotify Tracks
authorRemixDev <deezloaderremix@gmail.com>
Sun, 14 Apr 2019 16:00:50 +0000 (18:00 +0200)
committerRemixDev <deezloaderremix@gmail.com>
Sun, 14 Apr 2019 16:00:50 +0000 (18:00 +0200)
app/app.js
app/public/js/frontend.js

index a7f43d9dc61d5bf70f09a1b74acb39dc4691ffdf..8366d129bd92c2b4365ca6433fd4ab88301328fa 100644 (file)
@@ -645,6 +645,27 @@ io.sockets.on('connection', function (s) {
        }
        s.on("downloadspotifyplaylist", data=>{downloadSpotifyPlaylist(data)})
 
+       // Gets data from the frontend and creates data for the deezer track object
+       async function downloadSpotifyTrack(data){
+               logger.info(`Added to Queue ${data.id}`)
+               if (spotifySupport){
+                       try{
+                               let creds = await Spotify.clientCredentialsGrant()
+                               Spotify.setAccessToken(creds.body['access_token'])
+                               var resp = await Spotify.getTrack(data.id, {fields: "external_ids"})
+                               deezerId = await convertSpotify2Deezer(resp.body)
+                               data.id = deezerId
+                               downloadTrack(data)
+                       }catch(err){
+                               logger.error(`downloadSpotifyPlaylist failed: ${err.stack ? err.stack : err}`)
+                               return
+                       }
+               }else{
+                       s.emit("message", {title: "Spotify Support is not enabled", msg: "You should add authCredentials.js in your config files to use this feature<br>You can see how to do that in <a href=\"https://notabug.org/RemixDevs/DeezloaderRemix/wiki/Spotify+Features\">this guide</a>"})
+               }
+       }
+       s.on("downloadspotifytrack", data=>{downloadSpotifyTrack(data)})
+
        // Converts the spotify track to a deezer one
        // It tries first with the isrc (best way of conversion)
        // Fallbacks to the old way, using search
index 7759448668321cd1ff47076617041f598883737d..997d752dfddbd6f129c1c9ab31a82f71919bdb21 100644 (file)
@@ -1042,12 +1042,6 @@ $('#tab_url_form_url').submit(function (ev) {
                if (url.indexOf('?') > -1) {
                        url = url.substring(0, url.indexOf("?"))
                }
-               if (url.indexOf('open.spotify.com/') >= 0 ||  url.indexOf('spotify:') >= 0){
-                       if (url.indexOf('playlist') < 0){
-                               message('Playlist not found', 'Deezloader for now can only download Spotify playlists.')
-                               return false
-                       }
-               }
                addToQueue(url)
        }
 })
@@ -1056,7 +1050,8 @@ $('#tab_url_form_url').submit(function (ev) {
 function addToQueue(url, forceBitrate=null) {
        bitrate = forceBitrate ? forceBitrate : userSettings.maxBitrate
        var type = getTypeFromLink(url), id = getIDFromLink(url, type)
-       if (['track', 'playlist', 'spotifyplaylist', 'artisttop', 'album', 'artist'].indexOf(type) == -1) {
+       console.log(type, id)
+       if (['track', 'spotifytrack', 'playlist', 'spotifyplaylist', 'album', 'artist', 'artisttop'].indexOf(type) == -1) {
                M.toast({html: '<i class="material-icons left">error</i> Wrong Type!', displayLength: 5000, classes: 'rounded'})
                return false
        }
@@ -1064,7 +1059,7 @@ function addToQueue(url, forceBitrate=null) {
                M.toast({html: '<i class="material-icons left">playlist_add_check</i> Already in download-queue!', displayLength: 5000, classes: 'rounded'})
                return false
        }
-       if (id.match(/^-?[0-9]+$/) == null && type != 'spotifyplaylist') {
+       if (id.match(/^-?[0-9]+$/) == null && type.indexOf("spotify")<-1) {
                M.toast({html: '<i class="material-icons left">error</i> Wrong ID!', displayLength: 5000, classes: 'rounded'})
                return false
        }
@@ -1244,9 +1239,30 @@ function getIDFromLink(link, type) {
        }
        // Spotify
        if ((link.startsWith("http") && link.indexOf('open.spotify.com/') >= 0)){
-               return link.slice(link.indexOf("/playlist/")+10)
+               switch (type){
+                       case "spotifyplaylist":
+                               return link.slice(link.indexOf("/playlist/")+10)
+                               break
+                       case "spotifytrack":
+                               return link.slice(link.indexOf("/track/")+7)
+                               break
+                       case "spotifyalbum":
+                               return link.slice(link.indexOf("/album/")+7)
+                               break
+               }
        } else if (link.startsWith("spotify:")){
-               return link.slice(link.indexOf("playlist:")+9)
+               switch (type){
+                       case "spotifyplaylist":
+                               return link.slice(link.indexOf("playlist:")+9)
+                               break
+                       case "spotifytrack":
+                               return link.slice(link.indexOf("track:")+6)
+                               break
+                       case "spotifyalbum":
+                               return link.slice(link.indexOf("album:")+6)
+                               break
+               }
+
 
        // Deezer
        } else if(type == "artisttop") {
@@ -1259,7 +1275,10 @@ function getIDFromLink(link, type) {
 function getTypeFromLink(link) {
        var type
        if (link.indexOf('spotify') > -1){
-               type = "spotifyplaylist"
+               type = "spotify"
+               if (link.indexOf('playlist') > -1) type += "playlist"
+               else if (link.indexOf('track') > -1) type += "track"
+               else if (link.indexOf('album') > -1) type += "album"
        } else  if (link.indexOf('/track') > -1) {
                type = "track"
        } else if (link.indexOf('/playlist') > -1) {