diff --git a/LunaTranslator/LunaTranslator/LunaTranslator.py b/LunaTranslator/LunaTranslator/LunaTranslator.py index f7606415..dc720f0c 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator.py @@ -687,8 +687,6 @@ class MAINUI: zip_directory(path, savedir + "/" + data_head) def autohookmonitorthread(self): - for game in savehook_new_data: - checkifnewgame(game) while self.isrunning: self.onwindowloadautohook() time.sleep( diff --git a/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py b/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py index 9c1e8cd3..a58e1da2 100644 --- a/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py +++ b/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py @@ -59,7 +59,12 @@ import gobject from myutils.config import _TR, _TRL, globalconfig, static_data import winsharedutils from myutils.wrapper import Singleton_close, Singleton, threader, tryprint -from myutils.utils import checkifnewgame, vidchangedtask +from myutils.utils import ( + checkifnewgame, + vidchangedtask, + titlechangedtask, + imgchangedtask, +) from gui.usefulwidget import ( yuitsu_switch, saveposwindow, @@ -629,7 +634,6 @@ class dialog_setting_game(QDialog): global _global_dialog_setting_game _global_dialog_setting_game = self self.isopened = True - checkifnewgame(exepath) vbox = QVBoxLayout(self) # 配置layout self.setLayout(vbox) formwidget = QWidget() @@ -659,9 +663,7 @@ class dialog_setting_game(QDialog): titleedit = QLineEdit(savehook_new_data[exepath]["title"]) def _titlechange(x): - savehook_new_data[exepath]["title"] = x - savehook_new_data[exepath]["istitlesetted"] = True - savehook_new_data[exepath]["searchnoresulttime"] = 0 + titlechangedtask(exepath, x) self.setWindowTitle(x) titleedit.textChanged.connect(_titlechange) @@ -679,8 +681,7 @@ class dialog_setting_game(QDialog): _pixmap = QPixmap(res) if _pixmap.isNull() == False: - savehook_new_data[exepath]["imagepath"] = res - savehook_new_data[exepath]["isimagepathusersetted"] = True + imgchangedtask(exepath, res) imgpath.setText(res) vndbid = QLineEdit(str(savehook_new_data[exepath]["vid"])) diff --git a/LunaTranslator/LunaTranslator/gui/selecthook.py b/LunaTranslator/LunaTranslator/gui/selecthook.py index 1cba6cc1..6e009e89 100644 --- a/LunaTranslator/LunaTranslator/gui/selecthook.py +++ b/LunaTranslator/LunaTranslator/gui/selecthook.py @@ -42,10 +42,9 @@ from gui.usefulwidget import ( dialog_showinfo, getsimplecombobox, getsimpleswitch, - getcolorbutton, textbrowappendandmovetoend, ) -from myutils.utils import checkchaos, checkifnewgame +from myutils.utils import checkchaos from gui.dialog_savedgame import dialog_setting_game @@ -566,7 +565,6 @@ class hookselect(closeashidewindow): ) if _isusing: _text = "取消内嵌翻译" - checkifnewgame(gobject.baseobject.textsource.pname) if ss[-2][:8] == "UserHook": needinserthookcode = savehook_new_data[ @@ -908,7 +906,6 @@ class hookselect(closeashidewindow): gobject.baseobject.textsource.lock.acquire() - checkifnewgame(gobject.baseobject.textsource.pname) if key in gobject.baseobject.textsource.selectedhook: gobject.baseobject.textsource.selectedhook.remove(key) diff --git a/LunaTranslator/LunaTranslator/myutils/utils.py b/LunaTranslator/LunaTranslator/myutils/utils.py index 1b5ecd82..db1c070b 100644 --- a/LunaTranslator/LunaTranslator/myutils/utils.py +++ b/LunaTranslator/LunaTranslator/myutils/utils.py @@ -21,7 +21,7 @@ from myutils.config import ( ) import threading import re, heapq -from myutils.vndb import searchforidimage +from myutils.vndb import searchfordata, getvidbytitle from myutils.wrapper import tryprint @@ -44,6 +44,9 @@ class PriorityQueue: return bool(len(self._heap) == 0) +searchvndbqueue = PriorityQueue() + + def checkimage(gamepath): return (savehook_new_data[gamepath]["imagepath"] is None) or ( os.path.exists(savehook_new_data[gamepath]["imagepath"]) == False @@ -58,36 +61,32 @@ def checkinfo(gamepath): def checkvid(gamepath): - if savehook_new_data[gamepath]["vid"]: - return ( - checkimage(gamepath) - or checkinfo(gamepath) - or (len(savehook_new_data[gamepath]["vndbtags"]) == 0) - or (len(savehook_new_data[gamepath]["developers"]) == 0) - ) - else: - return ( - time.time() - savehook_new_data[gamepath]["searchnoresulttime"] - > 3600 * 24 * 7 - ) - -def checkneed(gamepath): - return ((gamepath in savehook_new_data) and (gamepath in savehook_new_list)) and ( - (checkvid(gamepath)) + return ( + checkimage(gamepath) + or checkinfo(gamepath) + or ( + (len(savehook_new_data[gamepath]["vndbtags"]) == 0) + and (len(savehook_new_data[gamepath]["developers"]) == 0) + ) ) -searchvndbqueue = PriorityQueue() - - def dispatachtask(gamepath): - if checkneed(gamepath) == False: - return + __t = [] if savehook_new_data[gamepath]["vid"]: - searchvndbqueue.put((gamepath, [savehook_new_data[gamepath]["vid"]]), 0) + if not checkvid(gamepath): + return + print(gamepath) + searchvndbqueue.put((1, gamepath, savehook_new_data[gamepath]["vid"])) else: + if ( + time.time() + < savehook_new_data[gamepath]["searchnoresulttime"] + 3600 * 24 * 7 + ): + return + print(gamepath) for _ in [ savehook_new_data[gamepath]["title"], os.path.basename(os.path.dirname(gamepath)), @@ -111,56 +110,55 @@ def dispatachtask(gamepath): if (len(t) < 10) and (all(ord(c) < 128 for c in t)): continue lst.append(t) - searchvndbqueue.put((gamepath, lst), 0) + searchvndbqueue.put((0, gamepath, lst)) + + +def parsetask(_type, gamepath, arg): + if _type == 2: + dispatachtask(gamepath) + elif _type == 0: + searchargs = arg + vid = None + for arg in searchargs: + vid = getvidbytitle(arg) + if vid: + break + if not vid: + return + savehook_new_data[gamepath]["vid"] = int(vid[1:]) + savehook_new_data[gamepath]["searchnoresulttime"] = time.time() + searchvndbqueue.put((1, gamepath, int(vid[1:]))) + + elif _type == 1: + vid = arg + data = searchfordata(vid) + + imagepath = data.get("imagepath", None) + infopath = data.get("infopath", None) + title = data.get("title", None) + namemap = data.get("namemap", None) + developers = data.get("developers", None) + vndbtags = data.get("vndbtags", None) + + if imagepath and (not savehook_new_data[gamepath]["isimagepathusersetted"]): + savehook_new_data[gamepath]["imagepath"] = imagepath + if title and (not savehook_new_data[gamepath]["istitlesetted"]): + savehook_new_data[gamepath]["title"] = title + if infopath: + savehook_new_data[gamepath]["infopath"] = infopath + if namemap: + savehook_new_data[gamepath]["namemap"] = namemap + if vndbtags: + savehook_new_data[gamepath]["vndbtags"] = vndbtags + if developers: + savehook_new_data[gamepath]["developers"] = developers def everymethodsthread(): while True: _ = searchvndbqueue.get() - if isinstance(_, tuple): - gamepath, searchargs = _ - else: - gamepath = _ - dispatachtask(gamepath) - continue - - if checkneed(gamepath) == False: - continue - print(gamepath) - succ = False - for searcharg in searchargs: - try: - data = searchforidimage(searcharg) - except: - print_exc() - continue - imagepath = data.get("imagepath", None) - infopath = data.get("infopath", None) - vid = data.get("vid", None) - title = data.get("title", None) - namemap = data.get("namemap", None) - developers = data.get("developers", None) - vndbtags = data.get("vndbtags", None) - if not vid: - continue - print(data) - savehook_new_data[gamepath]["vid"] = int(vid[1:]) - if imagepath and (not savehook_new_data[gamepath]["isimagepathusersetted"]): - savehook_new_data[gamepath]["imagepath"] = imagepath - if title and (not savehook_new_data[gamepath]["istitlesetted"]): - savehook_new_data[gamepath]["title"] = title - if infopath: - savehook_new_data[gamepath]["infopath"] = infopath - if namemap: - savehook_new_data[gamepath]["namemap"] = namemap - if vndbtags: - savehook_new_data[gamepath]["vndbtags"] = vndbtags - if developers: - savehook_new_data[gamepath]["developers"] = developers - succ = True - break - if succ == False: - savehook_new_data[gamepath]["searchnoresulttime"] = time.time() + _type, gamepath, arg = _ + tryprint(parsetask)(_type, gamepath, arg) threading.Thread(target=everymethodsthread).start() @@ -172,9 +170,18 @@ def vidchangedtask(gamepath, vid): except: return savehook_new_data[gamepath]["vid"] = vid - savehook_new_data[gamepath]["infopath"] = None - savehook_new_data[gamepath]["searchnoresulttime"] = 0 - searchvndbqueue.put((gamepath, [vid]), 1) + searchvndbqueue.put((1, gamepath, vid), 1) + + +def titlechangedtask(gamepath, title): + savehook_new_data[gamepath]["title"] = title + savehook_new_data[gamepath]["istitlesetted"] = True + searchvndbqueue.put((0, gamepath, [title]), 1) + + +def imgchangedtask(gamepath, res): + savehook_new_data[gamepath]["imagepath"] = res + savehook_new_data[gamepath]["isimagepathusersetted"] = True def checkifnewgame(gamepath, title=None): @@ -182,7 +189,7 @@ def checkifnewgame(gamepath, title=None): savehook_new_list.insert(0, gamepath) if gamepath not in savehook_new_data: savehook_new_data[gamepath] = getdefaultsavehook(gamepath, title) - searchvndbqueue.put(gamepath) + searchvndbqueue.put((2, gamepath, None)) kanjichs2ja = str.maketrans(static_data["kanjichs2ja"]) diff --git a/LunaTranslator/LunaTranslator/myutils/vndb.py b/LunaTranslator/LunaTranslator/myutils/vndb.py index f52b7381..b6952b56 100644 --- a/LunaTranslator/LunaTranslator/myutils/vndb.py +++ b/LunaTranslator/LunaTranslator/myutils/vndb.py @@ -135,12 +135,16 @@ def getvidbytitle_vn(title): def getvidbytitle_release(title): return safegetvndbjson( "https://api.vndb.org/kana/release", - {"filters": ["search", "=", title], "fields": "id", "sort": "searchrank"}, - lambda js: js["results"][0]["id"], + { + "filters": ["search", "=", title], + "fields": "id,vns.id", + "sort": "searchrank", + }, + lambda js: js["results"][0]["vns"][0]["id"], ) -def getdevelopersbyid(vid): +def getdevelopersbyid(vid): def _js(js): _ = [] @@ -203,7 +207,8 @@ def safedownload(): with open("./cache/vndb/vndb-tags-latest.json.gz", "wb") as ff: ff.write(resp.content) decompress_gzip_file( - "./cache/vndb/vndb-tags-latest.json.gz", "./cache/vndb/vndb-tags-latest.json" + "./cache/vndb/vndb-tags-latest.json.gz", + "./cache/vndb/vndb-tags-latest.json", ) with open("./cache/vndb/vndb-tags-latest.json", "r", encoding="utf8") as ff: js = json.load(ff) @@ -251,15 +256,10 @@ def getvntagsbyid(vid): return tags -def searchforidimage(titleorid): - print(titleorid) +def searchfordata(vid): + os.makedirs("./cache/vndb", exist_ok=True) - if isinstance(titleorid, str): - vid = getvidbytitle(titleorid) - if not vid: - return {} - elif isinstance(titleorid, int): - vid = "v{}".format(titleorid) + vid = "v{}".format(vid) img = getimgbyid(vid) title = gettitlebyid(vid) namemap = getcharnamemapbyid(vid) @@ -268,7 +268,6 @@ def searchforidimage(titleorid): return { "namemap": namemap, "title": title, - "vid": vid, "infopath": vndbdowloadinfo(vid), "imagepath": vndbdownloadimg(img), "vndbtags": vndbtags,