deemix-gui-pyweb/app.py

196 lines
6.4 KiB
Python
Raw Normal View History

2020-05-04 16:23:09 +00:00
#!/usr/bin/env python3
from deemix.api.deezer import Deezer
from deemix.app.settings import Settings
from deemix.app.queuemanager import QueueManager
from deemix.app.spotifyhelper import SpotifyHelper, emptyPlaylist as emptySpotifyPlaylist
2020-09-20 09:49:29 +00:00
from deemix.utils import getTypeFromLink, getIDFromLink
from deemix.utils.localpaths import getConfigFolder
import os.path as path
import json
class deemix:
def __init__(self, portable):
2020-08-18 13:18:00 +00:00
self.configFolder = portable or getConfigFolder()
self.set = Settings(self.configFolder)
self.sp = SpotifyHelper(self.configFolder)
2020-09-12 11:08:50 +00:00
self.qm = QueueManager(self.sp)
self.chartsList = []
2020-08-22 20:41:51 +00:00
self.homeCache = None
def shutdown(self, interface=None):
if self.set.settings['saveDownloadQueue']:
self.qm.saveQueue(self.configFolder)
self.qm.cancelAllDownloads(interface)
if interface:
interface.send("toast", {'msg': "Server is closed."})
def getConfigArl(self):
tempDeezer = Deezer()
arl = None
if path.isfile(path.join(self.configFolder, '.arl')):
with open(path.join(self.configFolder, '.arl'), 'r') as f:
arl = f.readline().rstrip("\n")
if not arl or not tempDeezer.login_via_arl(arl):
while True:
arl = input("Paste here your arl:")
if tempDeezer.login_via_arl(arl):
break
with open(path.join(self.configFolder, '.arl'), 'w') as f:
f.write(arl)
return arl
def restoreDownloadQueue(self, dz, interface=None):
2020-08-16 10:34:04 +00:00
self.qm.loadQueue(self.configFolder, self.set.settings, interface)
def queueRestored(self, dz, interface=None):
2020-09-12 11:08:50 +00:00
self.qm.nextItem(dz, interface)
def get_charts(self, dz):
if len(self.chartsList) == 0:
temp = dz.get_charts_countries()
countries = []
for i in range(len(temp)):
countries.append({
'title': temp[i]['title'].replace("Top ", ""),
'id': temp[i]['id'],
'picture_small': temp[i]['picture_small'],
'picture_medium': temp[i]['picture_medium'],
'picture_big': temp[i]['picture_big']
})
self.chartsList = countries
return self.chartsList
2020-08-22 20:41:51 +00:00
def get_home(self, dz):
if not self.homeCache:
2020-09-17 20:57:01 +00:00
self.homeCache = dz.get_charts()
2020-08-22 20:41:51 +00:00
return self.homeCache
def getDownloadFolder(self):
return self.set.settings['downloadLocation']
def getUserFavorites(self, dz):
result = {}
if dz.logged_in:
user_id = dz.user['id']
try:
result['playlists'] = dz.get_user_playlists(user_id)['data']
result['albums'] = dz.get_user_albums(user_id)['data']
result['artists'] = dz.get_user_artists(user_id)['data']
result['tracks'] = dz.get_user_tracks(user_id)['data']
except:
result['playlists'] = dz.get_user_playlists_gw(user_id)
result['albums'] = dz.get_user_albums_gw(user_id)
result['artists'] = dz.get_user_artists_gw(user_id)
result['tracks'] = dz.get_user_tracks_gw(user_id)
return result
def updateUserSpotifyPlaylists(self, user):
if user == "" or not self.sp.spotifyEnabled:
return []
try:
return self.sp.get_user_playlists(user)
except:
return []
def updateUserPlaylists(self, dz):
user_id = dz.user['id']
try:
return dz.get_user_playlists(user_id)['data']
except:
return dz.get_user_playlists_gw(user_id)
def updateUserAlbums(self, dz):
user_id = dz.user['id']
try:
return dz.get_user_albums(user_id)['data']
except:
return dz.get_user_albums_gw(user_id)
def updateUserArtists(self, dz):
user_id = dz.user['id']
try:
return dz.get_user_artists(user_id)['data']
except:
return dz.get_user_artists_gw(user_id)
def updateUserTracks(self, dz):
user_id = dz.user['id']
try:
return dz.get_user_tracks(user_id)['data']
except:
return dz.get_user_tracks_gw(user_id)
def getSpotifyPlaylistTracklist(self, id):
if id == "" or not self.sp.spotifyEnabled:
return emptySpotifyPlaylist
return self.sp.get_playlist_tracklist(id)
# Search functions
def mainSearch(self, dz, term):
return dz.search_main_gw(term)
def search(self, dz, term, type, start, nb):
return dz.search(term, type, nb, start)
def searchAlbum(self, dz, term, start, nb):
return dz.search_album_gw(term, start, nb)
def newReleases(self, dz):
return dz.get_new_releases()
# Queue functions
2020-09-14 14:58:33 +00:00
def addToQueue(self, dz, url, bitrate=None, interface=None, ack=None):
if ';' in url:
url = url.split(";")
2020-09-14 14:58:33 +00:00
self.qm.addToQueue(dz, url, self.set.settings, bitrate, interface, ack)
def removeFromQueue(self, uuid, interface=None):
self.qm.removeFromQueue(uuid, interface)
def cancelAllDownloads(self, interface=None):
self.qm.cancelAllDownloads(interface)
def removeFinishedDownloads(self, interface=None):
self.qm.removeFinishedDownloads(interface)
def initDownloadQueue(self):
(queue, queueComplete, queueList, currentItem) = self.qm.getQueue()
return (queue, queueComplete, queueList, currentItem)
def analyzeLink(self, dz, link):
type = getTypeFromLink(link)
relID = getIDFromLink(link, type)
if type in ["track", "album"]:
data = getattr(dz, 'get_' + type)(relID)
else:
data = {}
return (type, data)
# Settings functions
def getAllSettings(self):
return (self.set.settings, self.sp.getCredentials(), self.set.defaultSettings)
def getDefaultSettings(self):
return self.set.defaultSettings
def getSettings(self):
return self.set.settings
2020-09-20 09:49:29 +00:00
def saveSettings(self, newSettings, dz=None):
return self.set.saveSettings(newSettings, dz)
def getSpotifyCredentials(self):
return self.sp.getCredentials()
def setSpotifyCredentials(self, newCredentials):
return self.sp.setCredentials(newCredentials)