Spaces:
Runtime error
Runtime error
tomaseo2022
commited on
Commit
·
58607b6
1
Parent(s):
7778353
Upload 2 files
Browse files
urls.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""
|
3 |
+
Predefined URLs used to make google translate requests.
|
4 |
+
"""
|
5 |
+
BASE = 'https://translate.google.com'
|
6 |
+
TRANSLATE = 'https://{host}/translate_a/single'
|
utils.py
CHANGED
@@ -1,37 +1,79 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""A conversion module for googletrans"""
|
2 |
+
import json
|
3 |
+
import re
|
4 |
+
|
5 |
+
|
6 |
+
def build_params(query, src, dest, token, override):
|
7 |
+
params = {
|
8 |
+
'client': 'webapp',
|
9 |
+
'sl': src,
|
10 |
+
'tl': dest,
|
11 |
+
'hl': dest,
|
12 |
+
'dt': ['at', 'bd', 'ex', 'ld', 'md', 'qca', 'rw', 'rm', 'ss', 't'],
|
13 |
+
'ie': 'UTF-8',
|
14 |
+
'oe': 'UTF-8',
|
15 |
+
'otf': 1,
|
16 |
+
'ssel': 0,
|
17 |
+
'tsel': 0,
|
18 |
+
'tk': token,
|
19 |
+
'q': query,
|
20 |
+
}
|
21 |
+
|
22 |
+
if override is not None:
|
23 |
+
for key, value in get_items(override):
|
24 |
+
params[key] = value
|
25 |
+
|
26 |
+
return params
|
27 |
+
|
28 |
+
|
29 |
+
def legacy_format_json(original):
|
30 |
+
# save state
|
31 |
+
states = []
|
32 |
+
text = original
|
33 |
+
|
34 |
+
# save position for double-quoted texts
|
35 |
+
for i, pos in enumerate(re.finditer('"', text)):
|
36 |
+
# pos.start() is a double-quote
|
37 |
+
p = pos.start() + 1
|
38 |
+
if i % 2 == 0:
|
39 |
+
nxt = text.find('"', p)
|
40 |
+
states.append((p, text[p:nxt]))
|
41 |
+
|
42 |
+
# replace all weired characters in text
|
43 |
+
while text.find(',,') > -1:
|
44 |
+
text = text.replace(',,', ',null,')
|
45 |
+
while text.find('[,') > -1:
|
46 |
+
text = text.replace('[,', '[null,')
|
47 |
+
|
48 |
+
# recover state
|
49 |
+
for i, pos in enumerate(re.finditer('"', text)):
|
50 |
+
p = pos.start() + 1
|
51 |
+
if i % 2 == 0:
|
52 |
+
j = int(i / 2)
|
53 |
+
nxt = text.find('"', p)
|
54 |
+
# replacing a portion of a string
|
55 |
+
# use slicing to extract those parts of the original string to be kept
|
56 |
+
text = text[:p] + states[j][1] + text[nxt:]
|
57 |
+
|
58 |
+
converted = json.loads(text)
|
59 |
+
return converted
|
60 |
+
|
61 |
+
|
62 |
+
def get_items(dict_object):
|
63 |
+
for key in dict_object:
|
64 |
+
yield key, dict_object[key]
|
65 |
+
|
66 |
+
|
67 |
+
def format_json(original):
|
68 |
+
try:
|
69 |
+
converted = json.loads(original)
|
70 |
+
except ValueError:
|
71 |
+
converted = legacy_format_json(original)
|
72 |
+
|
73 |
+
return converted
|
74 |
+
|
75 |
+
|
76 |
+
def rshift(val, n):
|
77 |
+
"""python port for '>>>'(right shift with padding)
|
78 |
+
"""
|
79 |
+
return (val % 0x100000000) >> n
|