diff --git a/cpp/version.cmake b/cpp/version.cmake
index 175df5a8..f6a31e27 100644
--- a/cpp/version.cmake
+++ b/cpp/version.cmake
@@ -1,7 +1,7 @@
set(VERSION_MAJOR 6)
set(VERSION_MINOR 17)
-set(VERSION_PATCH 1)
+set(VERSION_PATCH 2)
set(VERSION_REVISION 0)
set(LUNA_VERSION "{${VERSION_MAJOR},${VERSION_MINOR},${VERSION_PATCH},${VERSION_REVISION}}")
add_library(VERSION_DEF ${CMAKE_CURRENT_LIST_DIR}/version_def.cpp)
diff --git a/py/LunaTranslator/cishu/goo.py b/py/LunaTranslator/cishu/goo.py
index 944545cd..58bf59c5 100644
--- a/py/LunaTranslator/cishu/goo.py
+++ b/py/LunaTranslator/cishu/goo.py
@@ -1,35 +1,34 @@
import requests
from urllib.parse import quote
import re
+from myutils.utils import localcachehelper
from cishu.cishubase import cishubase
class goo(cishubase):
def init(self):
- self.style = None
- self.klass = None
+ self.cache = localcachehelper("cishucss/goo")
+ self.klass = "lunagoocsswrapper"
def search(self, word):
url = "https://dictionary.goo.ne.jp/srch/all/{}/m1u/".format(quote(word))
x = requests.get(url, proxies=self.proxy).text
xx = re.findall("", x)
-
+ if not xx:
+ return
xx = "".join(xx).replace('href="/', 'href="https://dictionary.goo.ne.jp/')
- if not self.style:
- self.style, self.klass = self.parse_stylesheet(
- (
- requests.get(
- "https://dictionary.goo.ne.jp/mix/css/app.css",
- proxies=self.proxy,
- )
- .text.replace("width:1004px", "")
- .replace("width:1024px", "")
- .replace("width:644px", "")
- )
+ cssurl = "https://dictionary.goo.ne.jp/mix/css/app.css"
+ if not self.cache[cssurl]:
+ origin = requests.get(cssurl, proxies=self.proxy).text
+ origin = (
+ origin.replace("width:1004px", "")
+ .replace("width:1024px", "")
+ .replace("width:644px", "")
)
+ origin = self.parse_stylesheet(origin, self.klass)
+ self.cache[cssurl] = origin
- if len(xx):
- return '
'.format(
- self.style, self.klass, xx
- )
+ return ''.format(
+ self.cache[cssurl], self.klass, xx
+ )
diff --git a/py/LunaTranslator/cishu/japandict.py b/py/LunaTranslator/cishu/japandict.py
index 93283ce5..b6af74fc 100644
--- a/py/LunaTranslator/cishu/japandict.py
+++ b/py/LunaTranslator/cishu/japandict.py
@@ -1,15 +1,15 @@
import requests
from urllib.parse import quote
from cishu.cishubase import cishubase
-from myutils.utils import get_element_by
+from myutils.utils import get_element_by, localcachehelper
import re
class japandict(cishubase):
def init(self):
- self.style = None
- self.klass = None
+ self.style = localcachehelper("cishucss/japandict")
+ self.klass = "lunajapandictcsswrapper"
def search(self, word):
url = "https://www.japandict.com/?s={}&lang=eng&list=1".format(quote(word))
@@ -25,13 +25,12 @@ class japandict(cishubase):
if not res:
return
res = re.sub('href="(.*?)"', 'href="https://www.japandict.com\\1"', res)
- if not self.style:
- self.style, self.klass = self.parse_stylesheet(
- requests.get(
- "https://www.japandict.com/static/css/japandict.ac087f3ecbc8.css",
- proxies=self.proxy,
- ).text.replace("padding-top:60px !important", "")
- )
+ csslink = "https://www.japandict.com/static/css/japandict.ac087f3ecbc8.css"
+ if not self.style[csslink]:
+ css = requests.get(csslink, proxies=self.proxy).text
+ css = css.replace("padding-top:60px !important", "")
+ css = self.parse_stylesheet(css, self.klass)
+ self.style[csslink] = css
return '{}
'.format(
- self.style, self.klass, res
+ self.style[csslink], self.klass, res
)
diff --git a/py/LunaTranslator/cishu/jisho.py b/py/LunaTranslator/cishu/jisho.py
index ff30fe06..10f46647 100644
--- a/py/LunaTranslator/cishu/jisho.py
+++ b/py/LunaTranslator/cishu/jisho.py
@@ -2,13 +2,14 @@ import requests
from urllib.parse import quote
import re, threading
from cishu.cishubase import cishubase
-from myutils.utils import get_element_by, simplehtmlparser_all
+from myutils.utils import get_element_by, simplehtmlparser_all, localcachehelper
class jisho(cishubase):
def init(self):
- self.style = {}
+ self.style = localcachehelper("cishucss/jisho")
+ self.klass = "lunajishocsswrapper"
def generatehtml_tabswitch(self, allres):
btns = []
@@ -132,7 +133,9 @@ function onclickbtn_xxxxxx_internal(_id) {
)
link = ss.group()[6:-1]
if not self.style.get(link):
- self.style[link] = requests.get(link, proxies=self.proxy).text
+ self.style[link] = self.parse_stylesheet(
+ requests.get(link, proxies=self.proxy).text, self.klass
+ )
saver["style"] = self.style[link]
saver["primary"] = get_element_by("id", "result_area", res) + res.replace(
get_element_by("id", "main_results", res),
@@ -163,7 +166,6 @@ function onclickbtn_xxxxxx_internal(_id) {
res.append(("Others", saver["secondary"]))
if not res:
return
- style, klass = self.parse_stylesheet(saver.get("style", ""))
return '{}
'.format(
- style, klass, self.generatehtml_tabswitch(res)
+ saver.get("style", ""), self.klass, self.generatehtml_tabswitch(res)
)
diff --git a/py/LunaTranslator/cishu/weblio.py b/py/LunaTranslator/cishu/weblio.py
index 21b2b9a5..d08346cc 100644
--- a/py/LunaTranslator/cishu/weblio.py
+++ b/py/LunaTranslator/cishu/weblio.py
@@ -1,14 +1,15 @@
import requests
from urllib.parse import quote, unquote
from cishu.cishubase import cishubase
-from myutils.utils import simplehtmlparser_all, simplehtmlparser
+from myutils.utils import simplehtmlparser_all, simplehtmlparser, localcachehelper
import re, threading
class weblio(cishubase):
def init(self):
- self.style = {}
+ self.cache = localcachehelper("cishucss/weblio")
+ self.klass = "lunawebliocsswrapper"
def search(self, word):
url = "https://www.weblio.jp/content/" + quote(word)
@@ -51,7 +52,7 @@ class weblio(cishubase):
return '''href="javascript:safe_weblio_search_word('{}')"'''.format(word)
join = re.sub('href="https://www.weblio.jp/content/(.*?)"', __, join)
- join+=r'''
+ join += r"""
-'''
+"""
links = []
style = simplehtmlparser(html, "style", "{}
'.format(style, klass, join)
+ style += "".join(self.cache.get(link) for link in links)
+ return '{}
'.format(style, self.klass, join)
def makelink(self, link):
- if not self.style.get(link):
+ if not self.cache.get(link):
req = requests.get(
link,
proxies=self.proxy,
)
html = req.text if req.status_code == 200 else ""
- self.style[link] = html
+ self.cache[link] = self.parse_stylesheet(html, self.klass)
diff --git a/py/LunaTranslator/myutils/utils.py b/py/LunaTranslator/myutils/utils.py
index e3d78c0a..64ba5c84 100644
--- a/py/LunaTranslator/myutils/utils.py
+++ b/py/LunaTranslator/myutils/utils.py
@@ -26,6 +26,35 @@ from html.parser import HTMLParser
from myutils.audioplayer import bass_code_cast
+class localcachehelper:
+ def __init__(self, name):
+ self.name = name
+ self.cache = {}
+
+ def __getitem__(self, url: str):
+ _ = self.cache.get(url)
+ if _:
+ return _
+ b64 = hashlib.md5(url.encode("utf8")).hexdigest()
+ fn = gobject.getcachedir(os.path.join(self.name, b64))
+ if not os.path.isfile(fn):
+ return None
+ with open(fn, "r", encoding="utf8") as ff:
+ data = ff.read()
+ self.cache[url] = data
+ return data
+
+ def __setitem__(self, url: str, data):
+ self.cache[url] = data
+ b64 = hashlib.md5(url.encode("utf8")).hexdigest()
+ fn = gobject.getcachedir(os.path.join(self.name, b64))
+ with open(fn, "w", encoding="utf8") as ff:
+ ff.write(data)
+
+ get = __getitem__
+ set = __setitem__
+
+
def qimage2binary(qimage: QImage, fmt="BMP"):
byte_array = QByteArray()
buffer = QBuffer(byte_array)