Merge pull request 'Updated pyweb to use library version 1.2.0' (#11) from dev into main

Reviewed-on: https://codeberg.org/RemixDev/deemix-pyweb/pulls/11
This commit is contained in:
RemixDev 2020-08-16 12:39:48 +02:00
commit 018aa975bd
4 changed files with 171 additions and 191 deletions

317
app.py
View file

@ -1,204 +1,171 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from deemix.app.queuemanager import addToQueue, removeFromQueue, getQueue, cancelAllDownloads, removeFinishedDownloads, restoreQueue, slimQueueItems, resetQueueItems 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
from deemix.utils.misc import getTypeFromLink, getIDFromLink 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 from deemix.utils.localpaths import getConfigFolder
#from deemix.app.queuemanager import addToQueue, removeFromQueue, getQueue, cancelAllDownloads, removeFinishedDownloads, restoreQueue, slimQueueItems, resetQueueItems
#
#from deemix.app.settings import initSettings, getSettings, getDefaultSettings, saveSettings
#from deemix.app.spotify import SpotifyHelper
#
import os.path as path import os.path as path
import json import json
from os import remove
settings = {}
spotifyHelper = None
chartsList = []
configFolder = ""
def initialize(portable): class deemix:
global settings def __init__(self, portable):
global spotifyHelper self.configFolder = portable
global defaultSettings if not portable:
global configFolder self.configFolder = getConfigFolder()
if portable: self.set = Settings(self.configFolder)
configFolder = portable self.sp = SpotifyHelper(self.configFolder)
else: self.qm = QueueManager()
configFolder = getConfigFolder()
settings = initSettings(configFolder=configFolder)
defaultSettings = getDefaultSettings()
spotifyHelper = SpotifyHelper(configFolder=configFolder)
self.chartsList = []
def shutdown(interface=None): def shutdown(self, interface=None):
global configFolder if self.set.settings['saveDownloadQueue']:
if settings['saveDownloadQueue']: self.qm.saveQueue(self.configFolder)
(queue, queueComplete, queueList, currentItem) = getQueue() self.qm.cancelAllDownloads(interface)
if len(queueList) > 0:
if currentItem != "":
queue.insert(0, currentItem)
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):
global configFolder
if path.isfile(path.join(configFolder, 'queue.json')):
if interface: if interface:
interface.send('toast', {'msg': "Restoring download queue", 'icon': 'loading', 'dismiss': False, interface.send("toast", {'msg': "Server is closed."})
'id': 'restoring_queue'})
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'})
remove(path.join(configFolder, 'queue.json'))
restoreQueue(qd['queue'], qd['queueComplete'], qd['queueList'], dz, interface)
def getDownloadFolder(): def restoreDownloadQueue(self, dz, interface=None):
global settings self.qm.loadQueue(self.configFolder, self.set.settings, interface)
return settings['downloadLocation']
def get_charts(dz): def queueRestored(self, dz, interface=None):
global chartsList self.qm.nextItem(dz, self.sp, interface)
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
def getUserFavorites(dz): def get_charts(self, dz):
user_id = dz.user['id'] if len(self.chartsList) == 0:
result = {} temp = dz.get_charts_countries()
try: countries = []
result['playlists'] = dz.get_user_playlists(user_id)['data'] for i in range(len(temp)):
result['albums'] = dz.get_user_albums(user_id)['data'] countries.append({
result['artists'] = dz.get_user_artists(user_id)['data'] 'title': temp[i]['title'].replace("Top ", ""),
result['tracks'] = dz.get_user_tracks(user_id)['data'] 'id': temp[i]['id'],
except: 'picture_small': temp[i]['picture_small'],
result['playlists'] = dz.get_user_playlists_gw(user_id) 'picture_medium': temp[i]['picture_medium'],
result['albums'] = dz.get_user_albums_gw(user_id) 'picture_big': temp[i]['picture_big']
result['artists'] = dz.get_user_artists_gw(user_id) })
result['tracks'] = dz.get_user_tracks_gw(user_id) self.chartsList = countries
return result return self.chartsList
def updateUserSpotifyPlaylists(user): def getDownloadFolder(self):
if user == "" or not spotifyHelper.spotifyEnabled: return self.set.settings['downloadLocation']
return []
try: def getUserFavorites(self, dz):
return spotifyHelper.get_user_playlists(user) user_id = dz.user['id']
except: result = {}
return [] 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)
# Queue functions
def addToQueue(self, dz, url, bitrate=None, interface=None):
if ';' in url:
url = url.split(";")
self.qm.addToQueue(dz, self.sp, url, self.set.settings, bitrate, interface)
def updateUserPlaylists(dz): def removeFromQueue(self, uuid, interface=None):
user_id = dz.user['id'] self.qm.removeFromQueue(uuid, interface)
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): def cancelAllDownloads(self, interface=None):
return dz.search(term, type, nb, start) self.qm.cancelAllDownloads(interface)
# Queue functions def removeFinishedDownloads(self, interface=None):
def addToQueue_link(dz, url, bitrate=None, interface=None): self.qm.removeFinishedDownloads(interface)
if ';' in url:
url = url.split(";")
return addToQueue(dz, spotifyHelper, url, settings, bitrate, interface)
def removeFromQueue_link(uuid, interface=None): def initDownloadQueue(self):
removeFromQueue(uuid, interface) (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)
def cancelAllDownloads_link(interface=None): # Settings functions
cancelAllDownloads(interface) def getDefaultSettings(self):
return self.set.defaultSettings
def getSettings(self):
return self.set.settings
def removeFinishedDownloads_link(interface=None): def saveSettings(self, newSettings):
removeFinishedDownloads(interface) return self.set.saveSettings(newSettings)
def getSpotifyCredentials(self):
return self.sp.getCredentials()
def initDownloadQueue(): def setSpotifyCredentials(self, newCredentials):
(queue, queueComplete, queueList, currentItem) = getQueue() return self.sp.setCredentials(newCredentials)
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)

View file

@ -1,4 +1,4 @@
deemix>=1.1.28 deemix>=1.2.0
flask flask
flask-socketio flask-socketio
pywebview>=3.3.2 pywebview>=3.3.2

View file

@ -9,9 +9,9 @@ from flask import Flask, render_template, request, session, redirect, copy_curre
from flask_socketio import SocketIO, emit from flask_socketio import SocketIO, emit
from werkzeug.middleware.proxy_fix import ProxyFix from werkzeug.middleware.proxy_fix import ProxyFix
import app from app import deemix
from deemix.api.deezer import Deezer from deemix.api.deezer import Deezer
from deemix.app.MessageInterface import MessageInterface from deemix.app.messageinterface import MessageInterface
# Workaround for MIME type error in certain Windows installs # Workaround for MIME type error in certain Windows installs
# https://github.com/pallets/flask/issues/1045#issuecomment-42202749 # https://github.com/pallets/flask/issues/1045#issuecomment-42202749
@ -24,6 +24,8 @@ mimetypes.add_type('text/javascript', '.js')
from engineio.payload import Payload from engineio.payload import Payload
Payload.max_decode_packets = 500 Payload.max_decode_packets = 500
app = None
class CustomFlask(Flask): class CustomFlask(Flask):
jinja_options = Flask.jinja_options.copy() jinja_options = Flask.jinja_options.copy()
jinja_options.update(dict( jinja_options.update(dict(
@ -82,7 +84,7 @@ def not_found_handler(e):
def closing(): def closing():
app.shutdown(interface=socket_interface) app.shutdown(interface=socket_interface)
socketio.stop() socketio.stop()
return 'server closed' return 'Server Closed'
serverwide_arl = "--serverwide-arl" in sys.argv serverwide_arl = "--serverwide-arl" in sys.argv
if serverwide_arl: if serverwide_arl:
@ -91,9 +93,9 @@ if serverwide_arl:
@socketio.on('connect') @socketio.on('connect')
def on_connect(): def on_connect():
session['dz'] = Deezer() session['dz'] = Deezer()
settings = app.getSettings_link() settings = app.getSettings()
spotifyCredentials = app.getSpotifyCredentials() spotifyCredentials = app.getSpotifyCredentials()
defaultSettings = app.getDefaultSettings_link() defaultSettings = app.getDefaultSettings()
emit('init_settings', (settings, spotifyCredentials, defaultSettings)) emit('init_settings', (settings, spotifyCredentials, defaultSettings))
emit('init_autologin') emit('init_autologin')
@ -104,8 +106,13 @@ def on_connect():
login(arl) login(arl)
queue, queueComplete, queueList, currentItem = app.initDownloadQueue() queue, queueComplete, queueList, currentItem = app.initDownloadQueue()
emit('init_downloadQueue', if len(queueList.keys()):
{'queue': queue, 'queueComplete': queueComplete, 'queueList': queueList, 'currentItem': currentItem}) emit('init_downloadQueue',{
'queue': queue,
'queueComplete': queueComplete,
'queueList': queueList,
'currentItem': currentItem
})
emit('init_home', session['dz'].get_charts()) emit('init_home', session['dz'].get_charts())
emit('init_charts', app.get_charts(session['dz'])) emit('init_charts', app.get_charts(session['dz']))
@ -130,7 +137,7 @@ def login(arl, force=False, child=0):
emit('logged_in', {'status': result, 'arl': arl, 'user': session['dz'].user}) emit('logged_in', {'status': result, 'arl': arl, 'user': session['dz'].user})
if firstConnection and result in [1, 3]: if firstConnection and result in [1, 3]:
firstConnection = False firstConnection = False
app.loadDownloadQueue(session['dz'], socket_interface) app.restoreDownloadQueue(session['dz'], socket_interface)
if result != 0: if result != 0:
emit('familyAccounts', session['dz'].childs) emit('familyAccounts', session['dz'].childs)
emit('init_favorites', app.getUserFavorites(session['dz'])) emit('init_favorites', app.getUserFavorites(session['dz']))
@ -167,31 +174,36 @@ def search(data):
emit('search', result) emit('search', result)
@socketio.on('queueRestored')
def queueRestored():
app.queueRestored(session['dz'], socket_interface)
@socketio.on('addToQueue') @socketio.on('addToQueue')
def addToQueue(data): def addToQueue(data):
result = app.addToQueue_link(session['dz'], data['url'], data['bitrate'], interface=socket_interface) result = app.addToQueue(session['dz'], data['url'], data['bitrate'], interface=socket_interface)
if result == "Not logged in": if result == "Not logged in":
emit('loginNeededToDownload') emit('loginNeededToDownload')
@socketio.on('removeFromQueue') @socketio.on('removeFromQueue')
def removeFromQueue(uuid): def removeFromQueue(uuid):
app.removeFromQueue_link(uuid, interface=socket_interface) app.removeFromQueue(uuid, interface=socket_interface)
@socketio.on('removeFinishedDownloads') @socketio.on('removeFinishedDownloads')
def removeFinishedDownloads(): def removeFinishedDownloads():
app.removeFinishedDownloads_link(interface=socket_interface) app.removeFinishedDownloads(interface=socket_interface)
@socketio.on('cancelAllDownloads') @socketio.on('cancelAllDownloads')
def cancelAllDownloads(): def cancelAllDownloads():
app.cancelAllDownloads_link(interface=socket_interface) app.cancelAllDownloads(interface=socket_interface)
@socketio.on('saveSettings') @socketio.on('saveSettings')
def saveSettings(settings, spotifyCredentials, spotifyUser): def saveSettings(settings, spotifyCredentials, spotifyUser):
app.saveSettings_link(settings) app.saveSettings(settings)
app.setSpotifyCredentials(spotifyCredentials) app.setSpotifyCredentials(spotifyCredentials)
socketio.emit('updateSettings', (settings, spotifyCredentials)) socketio.emit('updateSettings', (settings, spotifyCredentials))
if spotifyUser != False: if spotifyUser != False:
@ -327,7 +339,8 @@ def applogin():
print("Can't open folder selection, you're not running pywebview") print("Can't open folder selection, you're not running pywebview")
def run_server(port, host="127.0.0.1", portable=None): def run_server(port, host="127.0.0.1", portable=None):
app.initialize(portable) global app
app = deemix(portable)
print("Starting server at http://" + host + ":" + str(port)) print("Starting server at http://" + host + ":" + str(port))
socketio.run(server, host=host, port=port) socketio.run(server, host=host, port=port)

2
webui

@ -1 +1 @@
Subproject commit 26d68a9e41d11df1011ea7f06a870d004a336c51 Subproject commit 6c72c1fb2fd48a3e847c5c7cfbd59fc75f071ae3