Fixed thread issues and started working on in app login

This commit is contained in:
RemixDev 2020-08-21 22:33:30 +02:00
parent a856716b12
commit 4a3882629b
2 changed files with 73 additions and 43 deletions

View file

@ -1,9 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl, pyqtSignal
from threading import Thread, Lock from threading import Thread, Lock, Semaphore
import sys import sys
import os.path as path import os.path as path
from os import makedirs from os import makedirs
@ -15,6 +15,9 @@ from deemix.utils.localpaths import getConfigFolder
server_lock = Lock() server_lock = Lock()
class MainWindow(QMainWindow): class MainWindow(QMainWindow):
selectDownloadFolder_trigger = pyqtSignal()
appLogin_trigger = pyqtSignal()
def __init__(self, title, url, x=None, y=None, w=800, h=600): def __init__(self, title, url, x=None, y=None, w=800, h=600):
super().__init__() super().__init__()
self.resize(w, h) self.resize(w, h)
@ -24,6 +27,14 @@ class MainWindow(QMainWindow):
self.webview.page().loadFinished.connect(self.finishLoading) self.webview.page().loadFinished.connect(self.finishLoading)
self.setCentralWidget(self.webview) self.setCentralWidget(self.webview)
self.url = url self.url = url
self.downloadFolder = None
self.selectDownloadFolder_trigger.connect(self.selectDownloadFolder)
self._selectDownloadFolder_semaphore = Semaphore(0)
self.arl = None
self.appLogin_trigger.connect(self.appLogin)
self._appLogin_semaphore = Semaphore(0)
if x is not None and y is not None: if x is not None and y is not None:
self.move(x, y) self.move(x, y)
@ -37,7 +48,14 @@ class MainWindow(QMainWindow):
def selectDownloadFolder(self): def selectDownloadFolder(self):
filename = QFileDialog.getExistingDirectory(self, "Select Download Folder", options=QFileDialog.ShowDirsOnly) filename = QFileDialog.getExistingDirectory(self, "Select Download Folder", options=QFileDialog.ShowDirsOnly)
return filename self.downloadFolder = filename
self._selectDownloadFolder_semaphore.release()
def appLogin(self):
loginWindow = QWebEngineView()
loginWindow.setUrl(QUrl("https://deezer.com/login"))
loginWindow.show()
self._appLogin_semaphore.release()
def closeEvent(self, event): def closeEvent(self, event):
x = int(self.x()) x = int(self.x())

View file

@ -290,7 +290,9 @@ def openDownloadsFolder():
@socketio.on('selectDownloadFolder') @socketio.on('selectDownloadFolder')
def selectDownloadFolder(): def selectDownloadFolder():
if gui: if gui:
result = gui.selectDownloadFolder() gui.selectDownloadFolder_trigger.emit()
gui._selectDownloadFolder_semaphore.acquire()
result = gui.downloadFolder
if result: if result:
emit('downloadFolderSelected', result) emit('downloadFolderSelected', result)
else: else:
@ -298,45 +300,55 @@ def selectDownloadFolder():
@socketio.on('applogin') @socketio.on('applogin')
def applogin(): def applogin():
try: if gui:
import webview gui.appLogin_trigger.emit()
global loginWindow gui._appLogin_semaphore.acquire()
if not loginWindow: if gui.arl:
@copy_current_request_context emit('applogin_arl', gui.arl)
def get_ARL(): gui.arl = None
global loginWindow else:
window = webview.windows[loginWindow] print("No ARL")
loginWindow = False else:
window.loaded -= get_ARL print("Can't open login page, you're not running the gui")
arl = json.loads(window.get_elements("body")[0]['innerText'])['results'] #try:
window.destroy() # import webview
emit('applogin_arl', arl) # global loginWindow
@copy_current_request_context # if not loginWindow:
def check_URL(): # @copy_current_request_context
global loginWindow # def get_ARL():
window = webview.windows[loginWindow] # global loginWindow
try: # window = webview.windows[loginWindow]
url = window.get_current_url() # loginWindow = False
except: # window.loaded -= get_ARL
url = "https://www.deezer.com/us/login" # arl = json.loads(window.get_elements("body")[0]['innerText'])['results']
if not "/login" in url: # window.destroy()
window.loaded -= check_URL # emit('applogin_arl', arl)
window.loaded += get_ARL # @copy_current_request_context
window.load_url('https://www.deezer.com/ajax/gw-light.php?method=user.getArl&input=3&api_version=1.0&api_token=null') # def check_URL():
@copy_current_request_context # global loginWindow
def on_close(): # window = webview.windows[loginWindow]
global loginWindow # try:
if loginWindow: # url = window.get_current_url()
loginWindow = False # except:
if not session['dz'].logged_in: # url = "https://www.deezer.com/us/login"
window = webview.create_window('Login into your deezer account', "https://deezer.com/login") # if not "/login" in url:
loginWindow = len(webview.windows)-1 # window.loaded -= check_URL
window.loaded += check_URL # window.loaded += get_ARL
window.closed += on_close # window.load_url('https://www.deezer.com/ajax/gw-light.php?method=user.getArl&input=3&api_version=1.0&api_token=null')
else: # @copy_current_request_context
emit('logged_in', {'status': 2, 'user': session['dz'].user}) # def on_close():
except: # global loginWindow
print("Can't open folder selection, you're not running pywebview") # if loginWindow:
# loginWindow = False
# if not session['dz'].logged_in:
# window = webview.create_window('Login into your deezer account', "https://deezer.com/login")
# loginWindow = len(webview.windows)-1
# window.loaded += check_URL
# window.closed += on_close
# else:
# emit('logged_in', {'status': 2, 'user': session['dz'].user})
#except:
# print("Can't open folder selection, you're not running pywebview")
def run_server(port, host="127.0.0.1", portable=None, mainWindow=None): def run_server(port, host="127.0.0.1", portable=None, mainWindow=None):
global app, gui global app, gui