2020-05-05 06:58:52 +00:00
|
|
|
#!/usr/env/bin python3
|
2020-04-22 09:27:09 +00:00
|
|
|
import logging
|
2020-04-07 22:19:27 +00:00
|
|
|
import os
|
2020-04-22 09:27:09 +00:00
|
|
|
import sys
|
2020-05-05 08:27:48 +00:00
|
|
|
from os import path
|
2020-04-07 22:19:27 +00:00
|
|
|
|
2020-04-12 19:22:06 +00:00
|
|
|
from flask import Flask, render_template, request, session
|
2020-04-10 14:12:21 +00:00
|
|
|
from flask_socketio import SocketIO, emit
|
2020-04-22 09:27:09 +00:00
|
|
|
|
|
|
|
import app
|
2020-04-12 19:22:06 +00:00
|
|
|
from deemix.api.deezer import Deezer
|
2020-04-15 12:49:40 +00:00
|
|
|
from deemix.app.MessageInterface import MessageInterface
|
2020-05-05 08:27:48 +00:00
|
|
|
from deemix.utils import localpaths
|
2020-04-22 09:27:09 +00:00
|
|
|
|
2020-04-07 22:19:27 +00:00
|
|
|
|
2020-04-08 16:43:35 +00:00
|
|
|
class CustomFlask(Flask):
|
2020-04-22 09:27:09 +00:00
|
|
|
jinja_options = Flask.jinja_options.copy()
|
|
|
|
jinja_options.update(dict(
|
|
|
|
block_start_string='$$',
|
|
|
|
block_end_string='$$',
|
|
|
|
variable_start_string='$',
|
|
|
|
variable_end_string='$',
|
|
|
|
comment_start_string='$#',
|
|
|
|
comment_end_string='#$',
|
|
|
|
))
|
2020-04-08 16:43:35 +00:00
|
|
|
|
2020-04-15 12:49:40 +00:00
|
|
|
|
2020-04-07 22:19:27 +00:00
|
|
|
gui_dir = os.path.join(os.path.dirname(__file__), 'public') # development path
|
|
|
|
if not os.path.exists(gui_dir): # frozen executable path
|
2020-04-22 09:27:09 +00:00
|
|
|
gui_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'public')
|
2020-04-08 16:43:35 +00:00
|
|
|
server = CustomFlask(__name__, static_folder=gui_dir, template_folder=gui_dir)
|
2020-04-07 22:19:27 +00:00
|
|
|
server.config['SEND_FILE_MAX_AGE_DEFAULT'] = 1 # disable caching
|
2020-04-10 14:12:21 +00:00
|
|
|
socketio = SocketIO(server)
|
|
|
|
|
2020-04-15 12:49:40 +00:00
|
|
|
class SocketInterface(MessageInterface):
|
2020-04-22 09:27:09 +00:00
|
|
|
def send(self, message, value=None):
|
|
|
|
if value:
|
|
|
|
socketio.emit(message, value)
|
|
|
|
else:
|
|
|
|
socketio.emit(message)
|
|
|
|
|
2020-04-15 12:49:40 +00:00
|
|
|
|
|
|
|
socket_interface = SocketInterface()
|
|
|
|
|
2020-04-10 14:12:21 +00:00
|
|
|
serverLog = logging.getLogger('werkzeug')
|
|
|
|
serverLog.disabled = True
|
|
|
|
server.logger.disabled = True
|
2020-04-07 22:19:27 +00:00
|
|
|
|
2020-04-13 17:22:34 +00:00
|
|
|
app.initialize()
|
|
|
|
|
2020-04-22 09:27:09 +00:00
|
|
|
|
2020-04-07 22:19:27 +00:00
|
|
|
@server.route('/')
|
|
|
|
def landing():
|
2020-04-22 09:27:09 +00:00
|
|
|
return render_template('index.html')
|
|
|
|
|
2020-04-07 22:19:27 +00:00
|
|
|
|
2020-04-11 14:24:53 +00:00
|
|
|
@server.route('/shutdown')
|
|
|
|
def closing():
|
2020-04-22 09:27:09 +00:00
|
|
|
app.shutdown(interface=socket_interface)
|
|
|
|
func = request.environ.get('werkzeug.server.shutdown')
|
|
|
|
func()
|
|
|
|
return 'server closed'
|
|
|
|
|
2020-05-05 08:27:48 +00:00
|
|
|
serverwide_arl = "--serverwide-arl" in sys.argv
|
2020-05-05 14:15:16 +00:00
|
|
|
if serverwide_arl:
|
|
|
|
print("Server-wide ARL enabled.")
|
2020-04-11 14:24:53 +00:00
|
|
|
|
2020-04-12 19:22:06 +00:00
|
|
|
@socketio.on('connect')
|
|
|
|
def on_connect():
|
2020-04-22 09:27:09 +00:00
|
|
|
session['dz'] = Deezer()
|
|
|
|
settings = app.getSettings_link()
|
|
|
|
spotifyCredentials = app.getSpotifyCredentials()
|
2020-05-03 14:21:37 +00:00
|
|
|
defaultSettings = app.getDefaultSettings_link()
|
|
|
|
emit('init_settings', (settings, spotifyCredentials, defaultSettings))
|
2020-05-05 08:27:48 +00:00
|
|
|
|
|
|
|
arl_file_path = path.join(localpaths.getConfigFolder(), '.arl')
|
|
|
|
if serverwide_arl and path.isfile(arl_file_path):
|
|
|
|
with open(arl_file_path, 'r') as file:
|
|
|
|
arl = file.read()
|
2020-05-06 08:24:32 +00:00
|
|
|
login(arl)
|
2020-05-05 08:27:48 +00:00
|
|
|
|
2020-04-22 09:27:09 +00:00
|
|
|
queue, queueComplete, queueList, currentItem = app.getQueue_link()
|
|
|
|
emit('init_downloadQueue',
|
|
|
|
{'queue': queue, 'queueComplete': queueComplete, 'queueList': queueList, 'currentItem': currentItem})
|
2020-04-29 08:36:30 +00:00
|
|
|
emit('init_home', session['dz'].get_charts())
|
2020-05-03 13:52:42 +00:00
|
|
|
emit('init_charts', app.get_charts(session['dz']))
|
2020-04-22 09:27:09 +00:00
|
|
|
|
2020-04-11 19:55:12 +00:00
|
|
|
|
|
|
|
@socketio.on('login')
|
2020-04-12 22:14:34 +00:00
|
|
|
def login(arl, force=False):
|
2020-04-22 09:27:09 +00:00
|
|
|
emit('toast', {'msg': "Logging in...", 'icon': 'loading', 'dismiss': False, 'id': "login-toast"})
|
|
|
|
if not session['dz'].logged_in:
|
|
|
|
result = session['dz'].login_via_arl(arl)
|
|
|
|
else:
|
|
|
|
if force:
|
|
|
|
session['dz'] = Deezer()
|
|
|
|
result = session['dz'].login_via_arl(arl)
|
|
|
|
if result == 1:
|
|
|
|
result = 3
|
|
|
|
else:
|
|
|
|
result = 2
|
|
|
|
emit('logged_in', {'status': result, 'arl': arl, 'user': app.getUser(session['dz'])})
|
2020-05-05 10:55:06 +00:00
|
|
|
emit('init_favorites', app.getUserFavorites(session['dz']))
|
2020-04-22 09:27:09 +00:00
|
|
|
|
2020-04-11 19:55:12 +00:00
|
|
|
|
2020-04-12 22:14:34 +00:00
|
|
|
@socketio.on('logout')
|
|
|
|
def logout():
|
2020-04-22 09:27:09 +00:00
|
|
|
status = 0
|
|
|
|
if session['dz'].logged_in:
|
|
|
|
session['dz'] = Deezer()
|
|
|
|
status = 0
|
|
|
|
else:
|
|
|
|
status = 1
|
|
|
|
emit('logged_out', status)
|
|
|
|
|
2020-04-12 22:14:34 +00:00
|
|
|
|
2020-04-10 14:12:21 +00:00
|
|
|
@socketio.on('mainSearch')
|
|
|
|
def mainSearch(data):
|
2020-04-22 09:27:09 +00:00
|
|
|
emit('mainSearch', app.mainSearch(session['dz'], data['term']))
|
|
|
|
|
2020-04-09 14:06:33 +00:00
|
|
|
|
2020-04-10 14:12:21 +00:00
|
|
|
@socketio.on('search')
|
|
|
|
def search(data):
|
2020-04-22 09:27:09 +00:00
|
|
|
result = app.search(session['dz'], data['term'], data['type'], data['start'], data['nb'])
|
|
|
|
result['type'] = data['type']
|
|
|
|
emit('search', result)
|
|
|
|
|
2020-04-08 21:52:08 +00:00
|
|
|
|
2020-04-10 14:12:21 +00:00
|
|
|
@socketio.on('addToQueue')
|
|
|
|
def addToQueue(data):
|
2020-04-22 09:27:09 +00:00
|
|
|
result = app.addToQueue_link(session['dz'], data['url'], data['bitrate'], interface=socket_interface)
|
|
|
|
if result == "Not logged in":
|
|
|
|
emit('toast', {'msg': "You need to log in to download tracks!", 'icon': 'report'})
|
|
|
|
|
2020-04-08 21:52:08 +00:00
|
|
|
|
2020-04-11 13:43:59 +00:00
|
|
|
@socketio.on('removeFromQueue')
|
|
|
|
def removeFromQueue(uuid):
|
2020-04-22 09:27:09 +00:00
|
|
|
app.removeFromQueue_link(uuid, interface=socket_interface)
|
|
|
|
|
2020-04-11 13:43:59 +00:00
|
|
|
|
2020-04-14 14:48:13 +00:00
|
|
|
@socketio.on('removeFinishedDownloads')
|
|
|
|
def removeFinishedDownloads():
|
2020-04-22 09:27:09 +00:00
|
|
|
app.removeFinishedDownloads_link(interface=socket_interface)
|
|
|
|
|
2020-04-14 14:48:13 +00:00
|
|
|
|
|
|
|
@socketio.on('cancelAllDownloads')
|
|
|
|
def cancelAllDownloads():
|
2020-04-22 09:27:09 +00:00
|
|
|
app.cancelAllDownloads_link(interface=socket_interface)
|
|
|
|
|
2020-04-14 14:48:13 +00:00
|
|
|
|
2020-04-14 17:58:54 +00:00
|
|
|
@socketio.on('saveSettings')
|
2020-05-05 13:08:04 +00:00
|
|
|
def saveSettings(settings, spotifyCredentials, spotifyUser):
|
2020-04-22 09:27:09 +00:00
|
|
|
app.saveSettings_link(settings)
|
|
|
|
app.setSpotifyCredentials(spotifyCredentials)
|
|
|
|
socketio.emit('updateSettings', (settings, spotifyCredentials))
|
2020-05-05 13:08:04 +00:00
|
|
|
if spotifyUser != False:
|
|
|
|
emit('updated_userSpotifyPlaylists', app.updateUserSpotifyPlaylists(spotifyUser))
|
2020-04-22 09:27:09 +00:00
|
|
|
|
2020-04-07 22:19:27 +00:00
|
|
|
|
2020-04-17 17:39:51 +00:00
|
|
|
@socketio.on('getTracklist')
|
|
|
|
def getTracklist(data):
|
2020-04-22 09:27:09 +00:00
|
|
|
if data['type'] == 'artist':
|
|
|
|
artistAPI = session['dz'].get_artist(data['id'])
|
|
|
|
artistAlbumsAPI = session['dz'].get_artist_albums(data['id'])['data']
|
|
|
|
tracksData = {'all': []}
|
|
|
|
for release in artistAlbumsAPI:
|
|
|
|
if not release['record_type'] in tracksData:
|
|
|
|
tracksData[release['record_type']] = []
|
|
|
|
tracksData[release['record_type']].append(release)
|
|
|
|
tracksData['all'].append(release)
|
|
|
|
artistAPI['releases'] = tracksData
|
|
|
|
emit('show_artist', artistAPI)
|
2020-05-05 13:08:04 +00:00
|
|
|
elif data['type'] == 'spotifyplaylist':
|
|
|
|
playlistAPI = app.getSpotifyPlaylistTracklist(data['id'])
|
|
|
|
for i in range(len(playlistAPI['tracks'])):
|
|
|
|
playlistAPI['tracks'][i] = playlistAPI['tracks'][i]['track']
|
|
|
|
playlistAPI['tracks'][i]['selected'] = False
|
|
|
|
emit('show_spotifyplaylist', playlistAPI)
|
2020-04-22 09:27:09 +00:00
|
|
|
else:
|
|
|
|
releaseAPI = getattr(session['dz'], 'get_' + data['type'])(data['id'])
|
|
|
|
releaseTracksAPI = getattr(session['dz'], 'get_' + data['type'] + '_tracks')(data['id'])['data']
|
|
|
|
tracks = []
|
|
|
|
showdiscs = False
|
|
|
|
if data['type'] == 'album' and len(releaseTracksAPI) and releaseTracksAPI[-1]['disk_number'] != 1:
|
|
|
|
current_disk = 0
|
|
|
|
showdiscs = True
|
|
|
|
for track in releaseTracksAPI:
|
|
|
|
if showdiscs and int(track['disk_number']) != current_disk:
|
|
|
|
current_disk = int(track['disk_number'])
|
|
|
|
tracks.append({'type': 'disc_separator', 'number': current_disk})
|
|
|
|
track['selected'] = False
|
|
|
|
tracks.append(track)
|
|
|
|
releaseAPI['tracks'] = tracks
|
|
|
|
emit('show_' + data['type'], releaseAPI)
|
|
|
|
|
2020-04-26 12:27:54 +00:00
|
|
|
@socketio.on('analyzeLink')
|
|
|
|
def analyzeLink(link):
|
|
|
|
(type, data) = app.analyzeLink(session['dz'], link)
|
|
|
|
emit('analyze_'+type, data)
|
2020-04-17 17:39:51 +00:00
|
|
|
|
2020-05-03 13:52:42 +00:00
|
|
|
@socketio.on('getChartTracks')
|
|
|
|
def getChartTracks(id):
|
|
|
|
emit('setChartTracks', session['dz'].get_playlist_tracks(id)['data'])
|
|
|
|
|
2020-05-05 13:08:04 +00:00
|
|
|
@socketio.on('update_userSpotifyPlaylists')
|
|
|
|
def update_userSpotifyPlaylists(spotifyUser):
|
|
|
|
if spotifyUser != False:
|
|
|
|
emit('updated_userSpotifyPlaylists', app.updateUserSpotifyPlaylists(spotifyUser))
|
|
|
|
|
|
|
|
@socketio.on('update_userPlaylists')
|
|
|
|
def update_userPlaylists():
|
|
|
|
emit('updated_userPlaylists', app.updateUserPlaylists(session['dz']))
|
|
|
|
|
|
|
|
@socketio.on('update_userAlbums')
|
|
|
|
def update_userAlbums():
|
|
|
|
emit('updated_userAlbums', app.updateUserAlbums(session['dz']))
|
|
|
|
|
|
|
|
@socketio.on('update_userArtists')
|
|
|
|
def update_userArtists():
|
|
|
|
emit('updated_userArtists', app.updateUserArtists(session['dz']))
|
|
|
|
|
|
|
|
@socketio.on('update_userTracks')
|
|
|
|
def update_userTracks():
|
|
|
|
emit('updated_userTracks', app.updateUserTracks(session['dz']))
|
|
|
|
|
2020-04-11 14:24:53 +00:00
|
|
|
def run_server(port):
|
2020-04-22 09:27:09 +00:00
|
|
|
print("Starting server at http://127.0.0.1:" + str(port))
|
|
|
|
socketio.run(server, host='0.0.0.0', port=port)
|
|
|
|
|
2020-04-07 22:19:27 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-05-05 13:36:05 +00:00
|
|
|
port = 33333
|
2020-04-22 09:27:09 +00:00
|
|
|
if len(sys.argv) >= 2:
|
2020-05-05 13:36:05 +00:00
|
|
|
try:
|
|
|
|
port = int(sys.argv[1])
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2020-04-22 09:27:09 +00:00
|
|
|
run_server(port)
|