2020-04-07 22:19:27 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import webbrowser
|
|
|
|
from functools import wraps
|
|
|
|
|
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
|
|
|
|
import logging
|
2020-04-07 22:19:27 +00:00
|
|
|
import webview
|
2020-04-12 19:22:06 +00:00
|
|
|
from deemix.api.deezer import Deezer
|
2020-04-07 22:19:27 +00:00
|
|
|
import deemix.app.main as app
|
2020-04-11 19:55:12 +00:00
|
|
|
import time
|
2020-04-07 22:19:27 +00:00
|
|
|
|
2020-04-08 16:43:35 +00:00
|
|
|
class CustomFlask(Flask):
|
|
|
|
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-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
|
|
|
|
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)
|
|
|
|
|
|
|
|
serverLog = logging.getLogger('werkzeug')
|
|
|
|
serverLog.disabled = True
|
|
|
|
server.logger.disabled = True
|
2020-04-07 22:19:27 +00:00
|
|
|
|
|
|
|
@server.route('/')
|
|
|
|
def landing():
|
|
|
|
return render_template('index.html', token=webview.token)
|
|
|
|
|
2020-04-11 14:24:53 +00:00
|
|
|
@server.route('/shutdown')
|
|
|
|
def closing():
|
|
|
|
app.shutdown(socket=socketio)
|
|
|
|
func = request.environ.get('werkzeug.server.shutdown')
|
|
|
|
func()
|
|
|
|
return 'server closed'
|
|
|
|
|
2020-04-12 19:22:06 +00:00
|
|
|
@socketio.on('connect')
|
|
|
|
def on_connect():
|
|
|
|
session['dz'] = Deezer()
|
|
|
|
|
2020-04-10 14:12:21 +00:00
|
|
|
@socketio.on('init')
|
|
|
|
def handle_init():
|
2020-04-11 19:55:12 +00:00
|
|
|
result = app.initialize()
|
|
|
|
emit('initialization', result)
|
|
|
|
|
|
|
|
@socketio.on('login')
|
|
|
|
def login(arl):
|
2020-04-12 19:22:06 +00:00
|
|
|
result = app.login(session['dz'], arl)
|
|
|
|
emit('logged_in', {'status': result, 'arl': arl, 'user': app.getUser(session['dz'])})
|
2020-04-11 19:55:12 +00:00
|
|
|
|
|
|
|
@socketio.on('loginpage')
|
|
|
|
def login_app():
|
|
|
|
loginWindow = webview.create_window('Login into deezer.com', 'https://www.deezer.com/login', user_agent="Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30")
|
|
|
|
while (loginWindow and loginWindow.get_current_url().startswith("https://www.deezer.com")):
|
|
|
|
time.sleep(1)
|
|
|
|
if loginWindow:
|
|
|
|
url = loginWindow.get_current_url()
|
|
|
|
loginWindow.destroy()
|
|
|
|
arl = url[url.find("arl%3D")+6:]
|
|
|
|
arl = arl[:arl.find("&")]
|
2020-04-12 19:22:06 +00:00
|
|
|
result = app.login(session['dz'], arl)
|
|
|
|
emit('logged_in', {'status': result, 'arl': arl, 'user': app.getUser(session['dz'])})
|
2020-04-11 19:55:12 +00:00
|
|
|
else:
|
|
|
|
emit('logged_in', {'status': 0})
|
2020-04-07 22:19:27 +00:00
|
|
|
|
2020-04-10 14:12:21 +00:00
|
|
|
@socketio.on('mainSearch')
|
|
|
|
def mainSearch(data):
|
2020-04-12 19:22:06 +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-12 19:22:06 +00:00
|
|
|
result = app.search(session['dz'], data['term'], data['type'], data['start'], data['nb'])
|
2020-04-09 14:06:33 +00:00
|
|
|
result['type'] = data['type']
|
2020-04-10 14:12:21 +00:00
|
|
|
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-12 19:22:06 +00:00
|
|
|
result = app.addToQueue_link(session['dz'], data['url'], socket=socketio)
|
|
|
|
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):
|
|
|
|
app.removeFromQueue_link(uuid, socket=socketio)
|
|
|
|
|
2020-04-08 21:52:08 +00:00
|
|
|
# Example code leftover, could be usefull later on
|
2020-04-07 22:19:27 +00:00
|
|
|
@server.route('/choose/path', methods=['POST'])
|
|
|
|
def choose_path():
|
|
|
|
dirs = webview.windows[0].create_file_dialog(webview.FOLDER_DIALOG)
|
|
|
|
if dirs and len(dirs) > 0:
|
|
|
|
directory = dirs[0]
|
|
|
|
if isinstance(directory, bytes):
|
|
|
|
directory = directory.decode('utf-8')
|
|
|
|
response = {'status': 'ok', 'directory': directory}
|
|
|
|
else:
|
|
|
|
response = {'status': 'cancel'}
|
|
|
|
|
|
|
|
return jsonify(response)
|
|
|
|
|
|
|
|
@server.route('/fullscreen', methods=['POST'])
|
|
|
|
def fullscreen():
|
|
|
|
webview.windows[0].toggle_fullscreen()
|
|
|
|
return jsonify({})
|
|
|
|
|
|
|
|
@server.route('/open-url', methods=['POST'])
|
|
|
|
def open_url():
|
|
|
|
url = request.json['url']
|
|
|
|
webbrowser.open_new_tab(url)
|
|
|
|
return jsonify({})
|
|
|
|
|
|
|
|
@server.route('/do/stuff', methods=['POST'])
|
|
|
|
def do_stuff():
|
|
|
|
result = app.do_stuff()
|
|
|
|
if result:
|
|
|
|
response = {'status': 'ok', 'result': result}
|
|
|
|
else:
|
|
|
|
response = {'status': 'error'}
|
|
|
|
return jsonify(response)
|
|
|
|
|
2020-04-11 14:24:53 +00:00
|
|
|
def run_server(port):
|
|
|
|
print("Starting server at http://127.0.0.1:"+str(port))
|
|
|
|
socketio.run(server, host='127.0.0.1', port=port)
|
2020-04-07 22:19:27 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-04-11 14:24:53 +00:00
|
|
|
run_server(33333)
|