deemix-gui-pyweb/app.py

205 lines
6.2 KiB
Python
Raw Normal View History

2020-05-04 16:23:09 +00:00
#!/usr/bin/env python3
from deemix.app.queuemanager import addToQueue, removeFromQueue, getQueue, cancelAllDownloads, removeFinishedDownloads, restoreQueue, slimQueueItems, resetQueueItems
from deemix.utils.misc import getTypeFromLink, getIDFromLink
from deemix.app.settings import initSettings, getSettings, getDefaultSettings, saveSettings
from deemix.app.spotify import SpotifyHelper
from deemix.utils.localpaths import getConfigFolder
import os.path as path
import json
2020-06-13 08:05:29 +00:00
from os import remove
settings = {}
spotifyHelper = None
2020-05-03 13:52:42 +00:00
chartsList = []
2020-05-26 10:22:32 +00:00
configFolder = ""
2020-05-26 10:22:32 +00:00
def initialize(portable):
global settings
global spotifyHelper
global defaultSettings
2020-05-26 10:22:32 +00:00
global configFolder
if portable:
configFolder = portable
else:
configFolder = getConfigFolder()
settings = initSettings(configFolder=configFolder)
defaultSettings = getDefaultSettings()
2020-05-26 10:22:32 +00:00
spotifyHelper = SpotifyHelper(configFolder=configFolder)
def shutdown(interface=None):
2020-05-26 10:22:32 +00:00
global configFolder
if settings['saveDownloadQueue']:
(queue, queueComplete, queueList, currentItem) = getQueue()
if len(queueList) > 0:
if currentItem != "":
queue.insert(0, currentItem)
2020-05-26 10:22:32 +00:00
with open(path.join(configFolder, 'queue.json'), 'w') as f:
json.dump({
'queue': queue,
'queueComplete': queueComplete,
'queueList': resetQueueItems(queueList, queue)
}, f)
cancelAllDownloads(interface)
if interface:
interface.send("toast", {'msg': "Server is closed."})
def loadDownloadQueue(dz, interface=None):
2020-05-26 10:22:32 +00:00
global configFolder
if path.isfile(path.join(configFolder, 'queue.json')):
if interface:
interface.send('toast', {'msg': "Restoring download queue", 'icon': 'loading', 'dismiss': False,
'id': 'restoring_queue'})
2020-05-26 10:22:32 +00:00
with open(path.join(configFolder, 'queue.json'), 'r') as f:
qd = json.load(f)
if interface:
interface.send('init_downloadQueue',
{'queue': qd['queue'], 'queueComplete': qd['queueComplete'], 'queueList': slimQueueItems(qd['queueList'])})
if interface:
interface.send('toast', {'msg': "Download queue restored!", 'icon': 'done', 'dismiss': True,
'id': 'restoring_queue'})
2020-05-26 10:22:32 +00:00
remove(path.join(configFolder, 'queue.json'))
restoreQueue(qd['queue'], qd['queueComplete'], qd['queueList'], dz, interface)
def getDownloadFolder():
global settings
return settings['downloadLocation']
2020-05-03 13:52:42 +00:00
def get_charts(dz):
global chartsList
if len(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']
})
chartsList = countries
return chartsList
2020-05-05 10:55:06 +00:00
def getUserFavorites(dz):
user_id = dz.user['id']
result = {}
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(user):
if user == "" or not spotifyHelper.spotifyEnabled:
return []
2020-06-29 11:58:40 +00:00
try:
return spotifyHelper.get_user_playlists(user)
except:
return []
def updateUserPlaylists(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(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(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(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(id):
if id == "" or not spotifyHelper.spotifyEnabled:
return spotifyHelper.emptyPlaylist
return spotifyHelper.get_playlist_tracklist(id)
# Search functions
def mainSearch(dz, term):
return dz.search_main_gw(term)
def search(dz, term, type, start, nb):
return dz.search(term, type, nb, start)
# Queue functions
def addToQueue_link(dz, url, bitrate=None, interface=None):
if ';' in url:
url = url.split(";")
return addToQueue(dz, spotifyHelper, url, settings, bitrate, interface)
def removeFromQueue_link(uuid, interface=None):
removeFromQueue(uuid, interface)
def cancelAllDownloads_link(interface=None):
cancelAllDownloads(interface)
2020-04-14 14:48:13 +00:00
def removeFinishedDownloads_link(interface=None):
removeFinishedDownloads(interface)
2020-04-14 14:48:13 +00:00
def initDownloadQueue():
(queue, queueComplete, queueList, currentItem) = getQueue()
return (queue, queueComplete, slimQueueItems(queueList), currentItem)
def analyzeLink(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 getDefaultSettings_link():
return defaultSettings
def getSettings_link():
return getSettings()
def saveSettings_link(newSettings):
global settings
settings = newSettings
return saveSettings(newSettings)
def getSpotifyCredentials():
return spotifyHelper.getCredentials()
def setSpotifyCredentials(newCredentials):
return spotifyHelper.setCredentials(newCredentials)