From 1240c11c9dfc4340afb845dac2a40e8169e9c528 Mon Sep 17 00:00:00 2001 From: Gogodr Date: Sat, 30 Apr 2022 00:45:31 -0500 Subject: [PATCH] refresh token bugfix and log scroll to bottom --- README.md | 6 +++++- index.py | 2 +- modules/db.py | 56 ++++++++++++++++++++++++++++++++++++--------------- ui/log/log.py | 5 ++++- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 5c14b06..ccc215a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## LostArk Market Watcher 0.4.4 +## LostArk Market Watcher 0.4.6 This app listens to the screenshot folder for new files. Starting on the version 0.3.0 this app needs to be launched through the [Lost Ark Market Launcher](https://github.com/gogodr/LostArk-Market-Launcher) Each new file is scanned and if the market window is detected in the picture then the image is segmented. @@ -28,6 +28,10 @@ Audio files from [MixKit](https://mixkit.co/) - Add Gold / Royal Crystal / Blue Crystal converstions support ### Changelog +### 0.4.6 +- Bugfix: Refresh season token every 30 minutes +- Log: Scroll to bottom automatically + ### 0.4.5 - Add Adventure Tome items diff --git a/index.py b/index.py index d73490d..c373695 100644 --- a/index.py +++ b/index.py @@ -21,7 +21,7 @@ from ui.config.config import LostArkMarketWatcherConfig from ui.log.log import LostArkMarketWatcherLog -version = '0.4.5' +version = '0.4.6' debug = False diff --git a/modules/db.py b/modules/db.py index ef873e7..9521504 100644 --- a/modules/db.py +++ b/modules/db.py @@ -1,6 +1,6 @@ import traceback -from datetime import datetime +from datetime import datetime, timedelta from slugify import slugify from google.oauth2.credentials import Credentials from google.cloud.firestore import Client @@ -17,30 +17,54 @@ class MarketDb(QObject): - log = Signal(str) - region = None - creds = None - db = None - uid = None + log: Signal = Signal(str) + region: str = None + id_token: str = None + refresh_token: str = None + uid: str = None + creds: Credentials = None + db: Client = None + last_refresh: datetime = None def __init__(self): try: - super(MarketDb, self).__init__() - _, self.refresh_token, _ = get_tokens() - self.id_token, self.refresh_token, self.uid = refresh_token( - self.refresh_token) - self.creds = Credentials( - token=self.id_token, - refresh_token=self.refresh_token - ) + super(MarketDb, self).__init__() + self.refresh_credentials() self.db = Client(project=project, credentials=self.creds) self.region = self.db.document( f'collaborators/{self.uid}').get().get('region') except NoTokenError: traceback.print_exc() + def refresh_credentials(self): + needs_refresh = False + if self.last_refresh is None: + needs_refresh = True + elif self.last_refresh + timedelta(minutes=30) < datetime.now(): + needs_refresh = True + + if needs_refresh: + self.log.emit(f"Refresh credentials") + try: + if self.refresh_token is None: + _, self.refresh_token, _ = get_tokens() + self.id_token, self.refresh_token, self.uid = refresh_token( + self.refresh_token) + self.creds = Credentials( + token=self.id_token, + refresh_token=self.refresh_token + ) + self.last_refresh = datetime.now() + except: + self.error.emit("Error getting credentials") + self.error.emit(traceback.format_exc) + + def add_entry(self, market_line: MarketLine, play_audio=True): try: + # Refresh credentials if needed + self.refresh_credentials() + # Get item data based on dictionary item = process_item(market_line) @@ -79,7 +103,7 @@ def add_entry(self, market_line: MarketLine, play_audio=True): if play_audio == True: playPulse() except: - self.log.emit(f"Add Entry Failed: {market_line.name}") + self.error.emit(f"Add Entry Failed: {market_line.name}") + self.error.emit(traceback.format_exc()) if play_audio == True: playError() - traceback.print_exc() diff --git a/ui/log/log.py b/ui/log/log.py index 7eb800c..b28f286 100644 --- a/ui/log/log.py +++ b/ui/log/log.py @@ -3,7 +3,7 @@ from pathlib import Path import sys -from PySide6.QtWidgets import QApplication, QMainWindow, QListWidgetItem +from PySide6.QtWidgets import QApplication, QMainWindow, QListWidgetItem, QListWidget from PySide6.QtCore import QFile, Qt, Signal from PySide6.QtUiTools import QUiLoader from PySide6.QtGui import QColor @@ -30,6 +30,8 @@ def load_ui(self): self.ui.setWindowFlags(Qt.Window | Qt.FramelessWindowHint) self.ui.btnClose.clicked.connect(self.close) self.ui.lLog.setWordWrap(True) + self.ui.lLog.setAutoScroll(True) + self.ui.lLog.setAutoScrollMargin(20) self.ui.lblTitle.setText( f"Lost Ark Market Watcher v{self.version} - {self.region} - Log") ui_file.close() @@ -53,6 +55,7 @@ def log(self, txt, error=False, save_log=False): with open(f'{datetime.now().strftime("%m-%d-%Y")}.log', "a") as file_object: file_object.write(f"{log_txt}\n") self.ui.lLog.addItem(i) + self.ui.lLog.scrollToBottom() if __name__ == "__main__":