Skip to content

Commit 33a0ee6

Browse files
committed
Remake to urllib3, preparation for proxy implementation
1 parent ad408c2 commit 33a0ee6

File tree

9 files changed

+1907
-619
lines changed

9 files changed

+1907
-619
lines changed

addon/globalPlugins/CloudVision/__init__.py

Lines changed: 705 additions & 514 deletions
Large diffs are not rendered by default.

addon/globalPlugins/CloudVision/chrome_ocr_engine.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
import re
33
import json
44
import base64
5-
import urllib.request
5+
import urllib3
66
from .cvhelpers import get_image_content_from_image
77

8-
class ChromeOCRError(Exception): pass
8+
9+
class ChromeOCRError(Exception):
10+
pass
11+
12+
913
def chromeOCREngine(image: any, lang: str = "en"):
1014
image_content = get_image_content_from_image(image)
1115
image_id = str(uuid4()).replace("-", "")
@@ -22,21 +26,17 @@ def chromeOCREngine(image: any, lang: str = "en"):
2226
}
2327
]
2428
}
25-
json_data = json.dumps(request_data).encode("utf-8")
26-
request = urllib.request.Request(BASE_URL + "pixels", data=json_data, method="POST")
27-
request.add_header("X-Goog-Api-Key", "AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw")
28-
request.add_header(
29-
"User-Agent",
30-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
31-
)
32-
request.add_header("Content-Type", "application/json")
33-
request.add_header("Sec-Fetch-Site", "none")
34-
request.add_header("Sec-Fetch-Mode", "no-cors")
35-
request.add_header(
36-
"Accept-Language", "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,it;q=0.6"
37-
)
38-
content = urllib.request.urlopen(request, timeout=39).read()
39-
resp = json.loads(content)
29+
http = urllib3.PoolManager()
30+
http.headers = {
31+
"X-Goog-Api-Key": "AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw",
32+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
33+
"Content-Type": "application/json",
34+
"Sec-Fetch-Site": "none",
35+
"Sec-Fetch-Mode": "no-cors",
36+
"Accept-Language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,it;q=0.6",
37+
}
38+
content = http.request("POST", BASE_URL + "pixels", json=request_data)
39+
resp = content.json()
4040
try:
4141
if "ocrRegions" not in resp["results"][0]["engineResults"][0]["ocrEngine"]:
4242
return ""
@@ -50,6 +50,6 @@ def chromeOCREngine(image: any, lang: str = "en"):
5050
text = text + word["detectedText"] + " "
5151
text = text + "\n"
5252

53-
text = re.sub(r' ([?><-_:!@№#$%^&*])', r'\1', text)
54-
text = re.sub(r' ([./!?,])', r'\1', text)
53+
text = re.sub(r" ([?><-_:!@№#$%^&*])", r"\1", text)
54+
text = re.sub(r" ([./!?,])", r"\1", text)
5555
return text

addon/globalPlugins/CloudVision/cvconf.py

Lines changed: 81 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,89 +3,100 @@
33
import globalVars
44
import languageHandler
55
import config
6+
67
try:
7-
from cBytesIO import BytesIO ## 2
8-
from cStringIO import StringIO ## 2
8+
from cBytesIO import BytesIO ## 2
9+
from cStringIO import StringIO ## 2
910
except ImportError:
10-
from io import BytesIO ## 3
11-
from io import StringIO ## 3
11+
from io import BytesIO ## 3
12+
from io import StringIO ## 3
1213

1314
## for Windows XP
1415
if sys.version_info.major == 2:
15-
sys.path.insert(0, ".")
16-
import myconfigobj as configobj
17-
del sys.path[0]
16+
sys.path.insert(0, ".")
17+
import myconfigobj as configobj
18+
19+
del sys.path[0]
1820
else:
19-
import configobj
21+
import configobj
2022

2123
try:
22-
import validate
24+
import validate
2325
except ImportError:
24-
import configobj.validate as validate
26+
import configobj.validate as validate
2527

26-
__all__ = ['getConfig','CONFIGDIR','bm_chat_id_file','bm_token_file','getDefaultLanguage','supportedLocales']
28+
__all__ = [
29+
"getConfig",
30+
"CONFIGDIR",
31+
"bm_chat_id_file",
32+
"bm_token_file",
33+
"getDefaultLanguage",
34+
"supportedLocales",
35+
]
2736

2837
CONFIGDIR = globalVars.appArgs.configPath
2938
bm_token_file = os.path.join(CONFIGDIR, "bm_token.txt")
3039
bm_chat_id_file = os.path.join(CONFIGDIR, "bm_chat_id.txt")
3140

3241
supportedLocales = [
33-
"am",
34-
"ar",
35-
"an",
36-
"af_ZA",
37-
"bg",
38-
"ca",
39-
"cs",
40-
"da",
41-
"de",
42-
"el",
43-
"en",
44-
"es",
45-
"fi",
46-
"fr",
47-
"hr",
48-
"hu",
49-
"hi",
50-
"id",
51-
"it",
52-
"ja",
53-
"ko",
54-
"lt",
55-
"lv",
56-
"my",
57-
"nl",
58-
"pl",
59-
"pt",
60-
"ro",
61-
"ru",
62-
"sk",
63-
"sl",
64-
"sr",
65-
"sv",
66-
"sq",
67-
"tg",
68-
"tr",
69-
"uk",
70-
"vi",
71-
"zh_CN",
72-
"zh_TW"
42+
"am",
43+
"ar",
44+
"an",
45+
"af_ZA",
46+
"bg",
47+
"ca",
48+
"cs",
49+
"da",
50+
"de",
51+
"el",
52+
"en",
53+
"es",
54+
"fi",
55+
"fr",
56+
"hr",
57+
"hu",
58+
"hi",
59+
"id",
60+
"it",
61+
"ja",
62+
"ko",
63+
"lt",
64+
"lv",
65+
"my",
66+
"nl",
67+
"pl",
68+
"pt",
69+
"ro",
70+
"ru",
71+
"sk",
72+
"sl",
73+
"sr",
74+
"sv",
75+
"sq",
76+
"tg",
77+
"tr",
78+
"uk",
79+
"vi",
80+
"zh_CN",
81+
"zh_TW",
7382
]
7483

7584

7685
def getDefaultLanguage():
77-
lang = languageHandler.getLanguage()
86+
lang = languageHandler.getLanguage()
87+
88+
if lang not in supportedLocales and "_" in lang:
89+
lang = lang.split("_")[0]
7890

79-
if lang not in supportedLocales and "_" in lang:
80-
lang = lang.split("_")[0]
81-
82-
if lang not in supportedLocales:
83-
lang = "en"
91+
if lang not in supportedLocales:
92+
lang = "en"
93+
94+
return lang
8495

85-
return lang
8696

8797
_config = None
88-
configspec = StringIO(u"""
98+
configspec = StringIO(
99+
"""
89100
prefer_navigator=boolean(default=False)
90101
sound=boolean(default=False)
91102
textonly=boolean(default=True)
@@ -94,12 +105,15 @@ def getDefaultLanguage():
94105
qronly=boolean(default=False)
95106
trtext=boolean(default=False)
96107
language=string(default={defaultLanguage})
97-
""".format(defaultLanguage=getDefaultLanguage()))
108+
""".format(defaultLanguage=getDefaultLanguage())
109+
)
110+
111+
98112
def getConfig():
99-
global _config
100-
if not _config:
101-
path = os.path.join(config.getUserDefaultConfigPath(), "CloudVision.ini")
102-
_config = configobj.ConfigObj(path, configspec=configspec)
103-
val = validate.Validator()
104-
_config.validate(val)
105-
return _config
113+
global _config
114+
if not _config:
115+
path = os.path.join(config.getUserDefaultConfigPath(), "CloudVision.ini")
116+
_config = configobj.ConfigObj(path, configspec=configspec)
117+
val = validate.Validator()
118+
_config.validate(val)
119+
return _config
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
__all__ = ['APIError',]
2-
class APIError(Exception): pass
1+
__all__ = [
2+
"APIError",
3+
]
4+
5+
6+
class APIError(Exception):
7+
pass

addon/globalPlugins/CloudVision/cvhelpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ def get_image_content_from_image(image: any):
2525
except Exception:
2626
return False
2727

28-
return image_content+b"\x00"
28+
return image_content + b"\x00"

addon/globalPlugins/CloudVision/piccy_bot.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .cvlangnames import LANGNAMES
22
import base64
33
import json
4-
import urllib.request
4+
import urllib3
55
from .cvhelpers import get_image_content_from_image
66

77

@@ -43,22 +43,14 @@ def piccyBot(image: any, lang: str = "en"):
4343
"exp": True,
4444
}
4545

46-
json_data = json.dumps(chat_data).encode("utf-8")
47-
48-
# Настраиваем запрос
49-
request = urllib.request.Request(url, data=json_data, method="POST")
50-
request.add_header("Content-Type", "application/json")
51-
request.add_header(
52-
"User-Agent", "PiccyBot/2.17.6 CFNetwork/1498.700.2 Darwin/23.6.0"
53-
)
54-
55-
# Отправляем запрос и получаем ответ
56-
with urllib.request.urlopen(request, timeout=59) as response:
57-
response_data = response.read()
58-
59-
data = json.loads(response_data)
46+
http = urllib3.PoolManager()
47+
http.headers = {
48+
"Content-Type": "application/json",
49+
"User-Agent": "PiccyBot/2.17.6 CFNetwork/1498.700.2 Darwin/23.6.0",
50+
}
51+
response = http.request("POST", url, json=chat_data)
52+
data = response.json()
6053

61-
# Обработка ответа
6254
txt = data.get("Text", "")
6355
if txt == "":
6456
raise PBAPIError(response_data)

0 commit comments

Comments
 (0)