Started adding Spotify Features

Added support for tracks and albums
This commit is contained in:
RemixDev 2020-04-06 15:57:10 +02:00
parent a032bb13b4
commit d94dbcf835
4 changed files with 110 additions and 7 deletions

View File

@ -3,6 +3,7 @@ from deemix.api.deezer import APIError, USER_AGENT_HEADER
from deemix.utils.taggers import tagID3, tagFLAC
from deemix.utils.pathtemplates import generateFilename, generateFilepath, settingsRegexAlbum, settingsRegexArtist
from deemix.utils.misc import changeCase
from deemix.utils.spotifyHelper import get_trackid_spotify, get_albumid_spotify
import os.path
from os import makedirs, remove
from requests import get
@ -565,6 +566,15 @@ def download_track(dz, id, settings, overwriteBitrate=False):
result = downloadTrackObj(dz, trackAPI, settings, overwriteBitrate)
return after_download_single(result, settings)
def download_spotifytrack(dz, id, settings, overwriteBitrate=False):
track_id = get_trackid_spotify(dz, id, settings['fallbackSearch'])
if track_id == "Not Enabled":
print("Spotify Features is not setted up correctly.")
if track_id != 0:
return download_track(dz, track_id, settings, overwriteBitrate)
else:
print("Track not found on deezer!")
return None
def download_album(dz, id, settings, overwriteBitrate=False):
albumAPI = dz.get_album(id)
@ -590,6 +600,16 @@ def download_album(dz, id, settings, overwriteBitrate=False):
return after_download(playlist, settings)
def download_spotifyalbum(dz, id, settings, overwriteBitrate=False):
album_id = get_albumid_spotify(dz, id)
if album_id == "Not Enabled":
print("Spotify Features is not setted up correctly.")
if album_id != 0:
return download_album(dz, album_id, settings, overwriteBitrate)
else:
print("Album not found on deezer!")
return None
def download_artist(dz, id, settings, overwriteBitrate=False):
artistAPI = dz.get_artist_albums(id)
for album in artistAPI['data']:

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
from deemix.api.deezer import Deezer
from deemix.utils.misc import getIDFromLink, getTypeFromLink, getBitrateInt
from deemix.app.downloader import download_track, download_album, download_playlist, download_artist
from deemix.app.downloader import download_track, download_album, download_playlist, download_artist, download_spotifytrack, download_spotifyalbum
from os import system as execute
dz = Deezer()
@ -21,6 +21,10 @@ def downloadLink(url, settings, bitrate=None):
folder = download_playlist(dz, id, settings, forcedBitrate)
elif type == "artist":
download_artist(dz, id, settings, forcedBitrate)
elif type == "spotifytrack":
folder = download_spotifytrack(dz, id, settings, forcedBitrate)
elif type == "spotifyalbum":
folder = download_spotifyalbum(dz, id, settings, forcedBitrate)
else:
print("URL not supported yet")
return None

View File

@ -44,18 +44,18 @@ def getIDFromLink(link, type):
if link.startswith("http") and 'open.spotify.com/' in link:
if type == "spotifyplaylist":
return link[link.find("/playlist/") + 10]
return link[link.find("/playlist/") + 10:]
if type == "spotifytrack":
return link[link.find("/track/") + 7]
return link[link.find("/track/") + 7:]
if type == "spotifyalbum":
return link[link.find("/album/") + 7]
return link[link.find("/album/") + 7:]
elif link.startswith("spotify:"):
if type == "spotifyplaylist":
return link[link.find("playlist:") + 9]
return link[link.find("playlist:") + 9:]
if type == "spotifytrack":
return link[link.find("track:") + 6]
return link[link.find("track:") + 6:]
if type == "spotifyalbum":
return link[link.find("album:") + 6]
return link[link.find("album:") + 6:]
elif type == "artisttop":
return re.search(r"\/artist\/(\d+)\/top_track", link)[1]
else:

View File

@ -0,0 +1,79 @@
#!/usr/bin/env python3
import os.path as path
from os import mkdir, rmdir
import json
import deemix.utils.localpaths as localpaths
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
credentials = {}
spotifyEnabled = False
def getCredentials():
global credentials
global spotifyEnabled
configFolder = localpaths.getConfigFolder()
if not path.isdir(configFolder):
mkdir(configFolder)
if not path.isfile(path.join(configFolder, 'authCredentials.json')):
with open(path.join(configFolder, 'authCredentials.json'), 'w') as f:
json.dump({'clientId': "", 'clientSecret': ""}, f, indent=2)
with open(path.join(configFolder, 'authCredentials.json'), 'r') as credentialsFile:
credentials = json.load(credentialsFile)
checkCredentials()
def checkCredentials():
global credentials
global spotifyEnabled
if credentials['clientId'] == "" or credentials['clientSecret'] == "":
spotifyEnabled = False
else:
spotifyEnabled = True
getCredentials()
if spotifyEnabled:
client_credentials_manager = SpotifyClientCredentials(client_id=credentials['clientId'], client_secret=credentials['clientSecret'])
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
def get_trackid_spotify(dz, track_id, fallbackSearch):
global spotifyEnabled
if not spotifyEnabled:
return "Not Enabled"
spotify_track = sp.track(track_id)
dz_track = 0
if 'external_ids' in spotify_track and 'isrc' in spotify_track['external_ids']:
try:
dz_track = dz.get_track_by_ISRC(spotify_track['external_ids']['isrc'])
dz_track = dz_track['id'] if 'id' in dz_track else 0
except:
dz_track = dz.get_track_from_metadata(spotify_track['artists'][0]['name'], spotify_track['name'], spotify_track['album']['name']) if fallbackSearch else 0
elif fallbackSearch:
dz_track = dz.get_track_from_metadata(spotify_track['artists'][0]['name'], spotify_track['name'], spotify_track['album']['name'])
return dz_track
def get_albumid_spotify(dz, album_id):
global spotifyEnabled
if not spotifyEnabled:
return "Not Enabled"
spotify_album = sp.album(album_id)
dz_album = 0
if 'external_ids' in spotify_album and 'upc' in spotify_album['external_ids']:
try:
dz_album = dz.get_album_by_UPC(spotify_album['external_ids']['upc'])
dz_album = dz_album['id'] if 'id' in dz_album else 0
except:
try:
dz_album = dz.get_album_by_UPC(int(spotify_album['external_ids']['upc']))
dz_album = dz_album['id'] if 'id' in dz_album else 0
except:
dz_album = 0
return dz_album
def convert_spotify_playlist(dz, playlist_id):
global spotifyEnabled
if not spotifyEnabled:
return "Not Enabled"
spotify_playlist = sp.playlist(playlist_id)
print(spotify_playlist)