Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Commit

Permalink
[0.1.3] 寻找更新时不会堵塞主线程
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraZiling committed May 17, 2023
1 parent 8b6b2f6 commit 59db4d8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/assets/configs/application.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"application_version": "0.1.1",
"application_version": "0.1.3",
"ui_version": "0.8.7"
}
4 changes: 3 additions & 1 deletion src/modules/Core/GachaReport/MihoyoAPI/by_web_cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from pathlib import Path
from urllib import parse
from win32api import GetTempFileName, GetTempPath, CopyFile

from ....Scripts.Utils.tools import Tools
Expand All @@ -12,6 +12,8 @@ def getURL(gameDataPath):
url = None
webCacheData = Path(gameDataPath) / "webCaches/Cache/Cache_Data/data_2"
webCacheDataTmp = Path(GetTempFileName(GetTempPath(), f"webCacheData", 0)[0])
if not os.path.exists(str(webCacheData)):
return None
CopyFile(str(webCacheData), str(webCacheDataTmp))
results = [extractAPI(result) for result in [result.split(b"\x00")[0].decode(errors="ignore") for result in webCacheDataTmp.read_bytes().split(b"1/0/")]]
results = [result for result in results if result]
Expand Down
6 changes: 4 additions & 2 deletions src/modules/Core/GachaReport/gacha_report_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ def extractAPI(url):


def convertAPI(url):
return "https://api-takumi.mihoyo.com/common/gacha_record/api/getGachaLog?" + url.split("?")[1]

if url:
return "https://api-takumi.mihoyo.com/common/gacha_record/api/getGachaLog?" + url.split("?")[1]
else:
return None

def updateAPI(url, gachaType, size, page, end_id):
apiParameters = dict(parse.parse_qsl(parse.urlparse(url).query))
Expand Down
29 changes: 27 additions & 2 deletions src/modules/Views/ViewFunctions/settingsFunctions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import os
import time

import requests
from PySide6.QtCore import Signal, QThread

from ...constant import GITHUB_RELEASE_URL
from ...Scripts.Utils import updater, config_utils
from ...Scripts.Utils.updater import cleanUpdateZip

utils = config_utils.ConfigUtils()


class UpdateThread(QThread):
Expand Down Expand Up @@ -39,4 +43,25 @@ def run(self):
if chunk:
f.write(chunk)
self.trigger.emit(0, f"正在下载: {f.tell()} 字节/{count} 字节")
self.trigger.emit(2, "更新下载完毕")
self.trigger.emit(2, "更新下载完毕")


class IsNeedUpdateThread(QThread):
trigger = Signal(int, object)

def __init__(self, appVersion, parent=None):
super(IsNeedUpdateThread, self).__init__(parent)
self.appVersion = appVersion
self.newVersion = {}

def run(self):
cleanUpdateZip()
self.newVersion = updater.isNeedUpdate(utils.appVersion)
if self.newVersion is None:
self.trigger.emit(1, "Asta 无需更新")
return
elif isinstance(self.newVersion, tuple):
self.trigger.emit(2,
f"Asta 更新请求超过限额\n请于{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(self.newVersion[1]['X-RateLimit-Reset'])))}之后再试")
return
self.trigger.emit(0, dict(self.newVersion))
4 changes: 2 additions & 2 deletions src/modules/Views/gacha_report_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def gachaReportStatusChanged(self, msg: tuple):
self.headerRightFullUpdateDropBtn.setEnabled(True)

def __headerRightFullUpdateDropBtnWebCache(self):
gachaURL = convertAPI(by_web_cache.getURL(getDefaultGameDataPath()))
gachaURL = convertAPI(by_web_cache.getURL(cfg.gameDataFolder.value))
if gachaURL:
resp = MessageBox("成功", "请求已被获取,是否更新数据?", self)
if resp.exec():
Expand All @@ -158,7 +158,7 @@ def __headerRightFullUpdateDropBtnWebCache(self):
self.gachaReportThread.start()
self.gachaReportThread.trigger.connect(self.gachaReportStatusChanged)
else:
MessageBox("失败", "无法从游戏缓存中获取请求", self)
InfoBar.error("失败", "无法从游戏缓存中获取请求", InfoBarPosition.TOP_RIGHT, parent=self)

def __headerRightFullUpdateDropBtnURL(self):
w = URLDialog("输入URL", "请在下方输入MiHoYoAPI的URL", self)
Expand Down
41 changes: 26 additions & 15 deletions src/modules/Views/settings_frame.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding:utf-8
import json
import time

from PySide6 import QtGui
Expand All @@ -11,7 +12,7 @@
from qfluentwidgets import FluentIcon, InfoBarPosition, qconfig

from .ViewConfigs.config import cfg
from .ViewFunctions.settingsFunctions import UpdateThread
from .ViewFunctions.settingsFunctions import UpdateThread, IsNeedUpdateThread
from ..Core.GachaReport import gacha_report_read
from ..Core.GachaReport.gacha_report_utils import getDefaultGameDataPath
from ..Scripts.UI import custom_icon, custom_dialog
Expand All @@ -33,8 +34,11 @@ def __init__(self, parent):
self.settingLabel = QLabel("设置", self)

self.configPath = utils.configPath
self.newVersion = None
self.updateThread = None
self.updateThreadStateTooltip = None
self.isNeedUpdateThread = None
self.isNeedUpdateThreadStateTooltip = None

# Game

Expand Down Expand Up @@ -283,21 +287,28 @@ def __updateReturnSignal(self, msg):
self.updateThread.start()
self.updateThread.trigger.connect(self.updateThreadStatusChanged)

def isNeedUpdateThreadStatusChanged(self, status, content):
if status == 1:
InfoBar.success("提示", content, InfoBarPosition.TOP_RIGHT, parent=self)
elif status == 2:
InfoBar.error("错误", content, InfoBarPosition.TOP_RIGHT, parent=self)
elif status == 0:
self.newVersion = content
self.isNeedUpdateThreadStateTooltip.setState(True)
self.isNeedUpdateThreadStateTooltip = None
w = ComboboxDialog("更新", f"发现新版本: {self.newVersion['tag_name']}\n是否更新?",
["Coding Artifact (国内推荐)", "Github Release (国外推荐)"], self)
w.returnSignal.connect(self.__updateReturnSignal)
w.exec()

def __updateCheckCardClicked(self):
cleanUpdateZip()
self.newVersion = updater.isNeedUpdate(utils.appVersion)
if self.newVersion is None:
InfoBar.success("提示", "Asta 无需更新", InfoBarPosition.TOP_RIGHT, parent=self.window())
return
elif isinstance(self.newVersion, tuple):
InfoBar.error("错误",
f"Asta 更新请求超过限额\n请于{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(self.newVersion[1]['X-RateLimit-Reset'])))}之后再试",
InfoBarPosition.TOP_RIGHT, parent=self.window())
return
w = ComboboxDialog("更新", f"发现新版本: {self.newVersion['tag_name']}\n是否更新?",
["Coding Artifact (国内推荐)", "Github Release (国外推荐)"], self)
w.returnSignal.connect(self.__updateReturnSignal)
w.exec()
self.updateCheckCard.setEnabled(False)
self.isNeedUpdateThread = IsNeedUpdateThread(utils.appVersion)
self.isNeedUpdateThreadStateTooltip = StateToolTip("更新", "正在获取版本号...", self)
self.isNeedUpdateThreadStateTooltip.move(5, 5)
self.isNeedUpdateThreadStateTooltip.show()
self.isNeedUpdateThread.start()
self.isNeedUpdateThread.trigger.connect(self.isNeedUpdateThreadStatusChanged)

def __connectSignalToSlot(self):
cfg.appRestartSig.connect(lambda: InfoBar.warning("警告", self.tr(
Expand Down

0 comments on commit 59db4d8

Please sign in to comment.