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
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
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 os.path as path
from os import makedirs
@ -15,6 +15,9 @@ from deemix.utils.localpaths import getConfigFolder
server_lock = Lock()
class MainWindow(QMainWindow):
selectDownloadFolder_trigger = pyqtSignal()
appLogin_trigger = pyqtSignal()
def __init__(self, title, url, x=None, y=None, w=800, h=600):
super().__init__()
self.resize(w, h)
@ -25,6 +28,14 @@ class MainWindow(QMainWindow):
self.setCentralWidget(self.webview)
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:
self.move(x, y)
else:
@ -37,7 +48,14 @@ class MainWindow(QMainWindow):
def selectDownloadFolder(self):
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):
x = int(self.x())

View file

@ -290,7 +290,9 @@ def openDownloadsFolder():
@socketio.on('selectDownloadFolder')
def selectDownloadFolder():
if gui:
result = gui.selectDownloadFolder()
gui.selectDownloadFolder_trigger.emit()
gui._selectDownloadFolder_semaphore.acquire()
result = gui.downloadFolder
if result:
emit('downloadFolderSelected', result)
else:
@ -298,45 +300,55 @@ def selectDownloadFolder():
@socketio.on('applogin')
def applogin():
try:
import webview
global loginWindow
if not loginWindow:
@copy_current_request_context
def get_ARL():
global loginWindow
window = webview.windows[loginWindow]
loginWindow = False
window.loaded -= get_ARL
arl = json.loads(window.get_elements("body")[0]['innerText'])['results']
window.destroy()
emit('applogin_arl', arl)
@copy_current_request_context
def check_URL():
global loginWindow
window = webview.windows[loginWindow]
try:
url = window.get_current_url()
except:
url = "https://www.deezer.com/us/login"
if not "/login" in url:
window.loaded -= check_URL
window.loaded += get_ARL
window.load_url('https://www.deezer.com/ajax/gw-light.php?method=user.getArl&input=3&api_version=1.0&api_token=null')
@copy_current_request_context
def on_close():
global loginWindow
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")
if gui:
gui.appLogin_trigger.emit()
gui._appLogin_semaphore.acquire()
if gui.arl:
emit('applogin_arl', gui.arl)
gui.arl = None
else:
print("No ARL")
else:
print("Can't open login page, you're not running the gui")
#try:
# import webview
# global loginWindow
# if not loginWindow:
# @copy_current_request_context
# def get_ARL():
# global loginWindow
# window = webview.windows[loginWindow]
# loginWindow = False
# window.loaded -= get_ARL
# arl = json.loads(window.get_elements("body")[0]['innerText'])['results']
# window.destroy()
# emit('applogin_arl', arl)
# @copy_current_request_context
# def check_URL():
# global loginWindow
# window = webview.windows[loginWindow]
# try:
# url = window.get_current_url()
# except:
# url = "https://www.deezer.com/us/login"
# if not "/login" in url:
# window.loaded -= check_URL
# window.loaded += get_ARL
# window.load_url('https://www.deezer.com/ajax/gw-light.php?method=user.getArl&input=3&api_version=1.0&api_token=null')
# @copy_current_request_context
# def on_close():
# global loginWindow
# 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):
global app, gui