Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
HIllya51 committed Jan 12, 2025
1 parent 17a8e0c commit 5801975
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 202 deletions.
2 changes: 1 addition & 1 deletion cpp/version.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

set(VERSION_MAJOR 6)
set(VERSION_MINOR 20)
set(VERSION_PATCH 0)
set(VERSION_PATCH 1)
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)
Expand Down
44 changes: 9 additions & 35 deletions py/LunaTranslator/cishu/cishubase.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from myutils.config import globalconfig
from myutils.wrapper import threader
from traceback import print_exc
from myutils.proxy import getproxy
from myutils.utils import SafeFormatter, getlangtgt, getlangsrc
from myutils.commonbase import ArgsEmptyExc, proxysession
from myutils.utils import SafeFormatter
from myutils.commonbase import commonbase
import re, uuid
from tinycss2 import parse_stylesheet, serialize
from tinycss2.ast import (
Expand All @@ -13,30 +12,23 @@
ParseError,
LiteralToken,
)
from language import Languages


class DictTree:
def text(self) -> str: ...
def childrens(self) -> list: ...


class cishubase:
typename = None
class cishubase(commonbase):

def init(self):
pass

def search(self, word):
return word

@property
def proxy(self):
return getproxy(("cishu", self.typename))

def __init__(self, typename) -> None:
self.typename = typename
self.proxysession = proxysession("cishu", self.typename)
super().__init__(typename)
self.callback = print
self.needinit = True
try:
Expand All @@ -45,6 +37,9 @@ def __init__(self, typename) -> None:
except:
print_exc()

_globalconfig_key = "cishu"
_setting_dict = globalconfig["cishu"]

@threader
def safesearch(self, sentence, callback):
try:
Expand All @@ -64,10 +59,6 @@ def safesearch(self, sentence, callback):
except:
pass

@property
def config(self):
return globalconfig["cishu"][self.typename]["args"]

def _gptlike_createquery(self, query, usekey, tempk):
user_prompt = (
self.config.get(tempk, "") if self.config.get(usekey, False) else ""
Expand All @@ -82,25 +73,8 @@ def _gptlike_createsys(self, usekey, tempk):
template = self.config[tempk]
else:
template = "You are a professional dictionary assistant whose task is to help users search for information such as the meaning, pronunciation, etymology, synonyms, antonyms, and example sentences of {srclang} words. You should be able to handle queries in multiple languages and provide in-depth information or simple definitions according to user needs. You should reply in {tgtlang}."
tgt = getlangtgt()
src = getlangsrc()
langmap = Languages.create_langmap(Languages.createenglishlangmap())
tgtlang = langmap.get(tgt, tgt)
srclang = langmap.get(src, src)
return fmt.format(template, srclang=srclang, tgtlang=tgtlang)

def checkempty(self, items):
emptys = []
for item in items:
if (self.config[item]) == "":
emptys.append(item)
if len(emptys):
emptys_s = []
argstype = self.config.get("argstype", {})
for e in emptys:
name = argstype.get(e, {}).get("name", e)
emptys_s.append(name)
raise ArgsEmptyExc(emptys_s)

return fmt.format(template, srclang=self.srclang, tgtlang=self.tgtlang)

def markdown_to_html(self, markdown_text: str):
print(markdown_text)
Expand Down
5 changes: 2 additions & 3 deletions py/LunaTranslator/cishu/goo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import requests
from urllib.parse import quote
import re
from myutils.utils import localcachehelper
Expand All @@ -13,14 +12,14 @@ def init(self):

def search(self, word):
url = "https://dictionary.goo.ne.jp/srch/all/{}/m1u/".format(quote(word))
x = requests.get(url, proxies=self.proxy).text
x = self.proxysession.get(url).text
xx = re.findall("<section>([\\s\\S]*?)</section>", x)
if not xx:
return
xx = "".join(xx).replace('href="/', 'href="https://dictionary.goo.ne.jp/')
cssurl = "https://dictionary.goo.ne.jp/mix/css/app.css"
if not self.cache[cssurl]:
origin = requests.get(cssurl, proxies=self.proxy).text
origin = self.proxysession.get(cssurl).text
origin = (
origin.replace("width:1004px", "")
.replace("width:1024px", "")
Expand Down
8 changes: 2 additions & 6 deletions py/LunaTranslator/cishu/japandict.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import requests
from urllib.parse import quote
from cishu.cishubase import cishubase
from myutils.utils import get_element_by, localcachehelper
Expand All @@ -13,10 +12,7 @@ def init(self):

def search(self, word):
url = "https://www.japandict.com/?s={}&lang=eng&list=1".format(quote(word))
html = requests.get(
url,
proxies=self.proxy,
).text
html = self.proxysession.get(url).text

check = get_element_by("class", "alert-heading", html)
if check:
Expand All @@ -27,7 +23,7 @@ def search(self, word):
res = re.sub('href="(.*?)"', 'href="https://www.japandict.com\\1"', res)
csslink = "https://www.japandict.com/static/css/japandict.ac087f3ecbc8.css"
if not self.style[csslink]:
css = requests.get(csslink, proxies=self.proxy).text
css = self.proxysession.get(csslink).text
css = css.replace("padding-top:60px !important", "")
css = self.parse_stylesheet(css, self.klass)
self.style[csslink] = css
Expand Down
8 changes: 2 additions & 6 deletions py/LunaTranslator/cishu/jisho.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import requests
from urllib.parse import quote
import re, threading
from cishu.cishubase import cishubase
Expand Down Expand Up @@ -99,10 +98,7 @@ def paradown(self, word, key, saver):

link = "https://jisho.org/{}/{}".format(key, quote(word))
url = link
html = requests.get(
url,
proxies=self.proxy,
).text
html = self.proxysession.get(url).text

if get_element_by("id", "no-matches", html):
return
Expand Down Expand Up @@ -135,7 +131,7 @@ def paradown(self, word, key, saver):
link = ss.group()[6:-1]
if not self.style.get(link):
self.style[link] = self.parse_stylesheet(
requests.get(link, proxies=self.proxy).text, self.klass
self.proxysession.get(link).text, self.klass
)
saver["style"] = self.style[link]
saver["primary"] = get_element_by("id", "result_area", res) + res.replace(
Expand Down
8 changes: 3 additions & 5 deletions py/LunaTranslator/cishu/mojidict.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import requests, re, json, uuid
import re, json, uuid

from cishu.cishubase import cishubase

Expand Down Expand Up @@ -33,11 +33,10 @@ def mojiclicksearch(self, word):
ensure_ascii=False,
).encode()

response = requests.post(
response = self.proxysession.post(
"https://api.mojidict.com/parse/functions/word-clickSearchV2",
headers=headers,
data=data,
proxies=self.proxy,
)

result = response.json()["result"]["result"]
Expand Down Expand Up @@ -573,7 +572,7 @@ def mojiclicksearch(self, word):


def mojizonghe(self, word):
response = requests.post(
response = self.proxysession.post(
"https://api.mojidict.com/parse/functions/union-api",
json={
"functions": [
Expand Down Expand Up @@ -610,7 +609,6 @@ def mojizonghe(self, word):
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
},
proxies=self.proxy,
)

result = ""
Expand Down
16 changes: 8 additions & 8 deletions py/LunaTranslator/cishu/weblio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import requests
from urllib.parse import quote, unquote
from cishu.cishubase import cishubase
from myutils.utils import simplehtmlparser_all, simplehtmlparser, localcachehelper
Expand All @@ -13,7 +12,7 @@ def init(self):

def search(self, word):
url = "https://www.weblio.jp/content/" + quote(word)
html = requests.get(url, proxies=self.proxy).text
html = self.proxysession.get(url).text
head = simplehtmlparser_all(html, "div", '<div class="pbarT">')
content = simplehtmlparser_all(html, "div", '<div class="kijiWrp">')
if not content:
Expand Down Expand Up @@ -62,7 +61,9 @@ def __(match):
}</script>
"""
links = []
style = self.parse_stylesheet(simplehtmlparser(html, "style", "<style>")[7:-8], self.klass)
style = self.parse_stylesheet(
simplehtmlparser(html, "style", "<style>")[7:-8], self.klass
)
for link in simplehtmlparser_all(html, "link", '<link rel="stylesheet"'):
for _ in re.findall('href="(.*?)"', link):
links.append("https:" + _)
Expand All @@ -73,13 +74,12 @@ def __(match):
for t in ts:
t.join()
style += "".join(self.cache.get(link) for link in links)
return '<style>{}</style><div class="{}">{}</div>'.format(style, self.klass, join)
return '<style>{}</style><div class="{}">{}</div>'.format(
style, self.klass, join
)

def makelink(self, link):
if not self.cache.get(link):
req = requests.get(
link,
proxies=self.proxy,
)
req = self.proxysession.get(link)
html = req.text if req.status_code == 200 else ""
self.cache[link] = self.parse_stylesheet(html, self.klass)
9 changes: 4 additions & 5 deletions py/LunaTranslator/cishu/youdao.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import requests
from urllib.parse import quote
import re, os
import re
from cishu.cishubase import cishubase
from myutils.utils import get_element_by, simplehtmlparser, getlangsrc
from myutils.utils import get_element_by, simplehtmlparser
from myutils.config import isascii
from language import Languages


class youdao(cishubase):

def search(self, word: str):
lang = getlangsrc()
lang = self.srclang
if lang == Languages.Auto:
if isascii(word):
lang = Languages.English
else:
lang = Languages.Japanese
url = "https://dict.youdao.com/result?word={}&lang={}".format(quote(word), lang)
text = requests.get(url, proxies=self.proxy).text
text = self.proxysession.get(url).text
if not get_element_by("class", "word-head", text):
return
text = re.sub("<header([\\s\\S]*?)></header>", "", text)
Expand Down
4 changes: 0 additions & 4 deletions py/LunaTranslator/myutils/commonbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ def langmap_(self):
def __init__(self, typename) -> None:
self.typename = typename
self.renewsesion()
self.level2init()

def renewsesion(self):
self.proxysession = proxysession(self._globalconfig_key, self.typename)

def level2init(self):
pass
4 changes: 4 additions & 0 deletions py/LunaTranslator/ocrengines/baseocrclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def raise_cant_be_auto_lang(self):
if l == Languages.Auto:
raise Exception(_TR("当前OCR引擎不支持设置语言为自动"))

def __init__(self, typename):
super().__init__(typename)
self.level2init()

def level2init(self):
self.needinit = True
try:
Expand Down
21 changes: 9 additions & 12 deletions py/LunaTranslator/ocrengines/volcengine.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import json
from collections import OrderedDict
import requests
from urllib.parse import urlencode
from functools import reduce
import hmac, base64
import datetime
import pytz
import hashlib, os
import hashlib
import sys, threading
from urllib.parse import quote

Expand Down Expand Up @@ -382,8 +381,7 @@ def __str__(self):
class Service(object):
def __init__(self, service_info, api_info):
self.service_info = service_info
self.api_info = api_info
self.session = requests.session()
self.api_info = api_info

def set_ak(self, ak):
self.service_info.credentials.set_ak(ak)
Expand Down Expand Up @@ -414,7 +412,7 @@ def get_sign_url(self, api, params):

return SignerV4.sign_url(r, self.service_info.credentials)

def post(self, api, params, form, proxy):
def post(self, api, params, form, session):
if not (api in self.api_info):
raise Exception("no such api")
api_info = self.api_info[api]
Expand All @@ -426,11 +424,10 @@ def post(self, api, params, form, proxy):

url = r.build()

resp = self.session.post(
resp = session.post(
url,
headers=r.headers,
data=r.form,
proxies=proxy,
)
if resp.status_code == 200:
return resp.text
Expand Down Expand Up @@ -511,10 +508,10 @@ def get_service_info():
)
return service_info

def common_handler(self, api, form, proxy):
def common_handler(self, api, form, session):
params = dict()
try:
res = self.post(api, params, form, proxy)
res = self.post(api, params, form, session)
res_json = json.loads(res)
return res_json
except Exception as e:
Expand All @@ -525,9 +522,9 @@ def common_handler(self, api, form, proxy):
except:
raise Exception(str(e))

def ocr_api(self, action, form, proxy):
def ocr_api(self, action, form, session):
try:
res_json = self.common_handler(action, form, proxy)
res_json = self.common_handler(action, form, session)
return res_json
except Exception as e:
raise Exception(str(e))
Expand Down Expand Up @@ -561,7 +558,7 @@ def ocr(self, imagebinary):

b64 = base64.b64encode(imagebinary)
form["image_base64"] = b64
resp = visual_service.ocr_api("MultiLanguageOCR", form, self.proxy)
resp = visual_service.ocr_api("MultiLanguageOCR", form, self.proxysession)
try:
texts = [box["text"] for box in resp["data"]["ocr_infos"]]
boxs = self.flatten4point(
Expand Down
3 changes: 2 additions & 1 deletion py/LunaTranslator/translator/basetranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def __getitem__(self2, __key):
using_gpt_dict = False
_compatible_flag_is_sakura_less_than_5_52_3 = True

def level2init(self):
def __init__(self, typename):
super().__init__(typename)
if (self.transtype == "offline") and (not self.is_gpt_like):
globalconfig["fanyi"][self.typename]["useproxy"] = False
self.multiapikeycurrentidx = -1
Expand Down
Loading

0 comments on commit 5801975

Please sign in to comment.