-
Notifications
You must be signed in to change notification settings - Fork 10
/
tk.py
102 lines (83 loc) · 3.1 KB
/
tk.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import calendar
import math
import time
from requests import get
import re
class Token:
SALT_1 = "+-a^+6"
SALT_2 = "+-3^+b+-f"
def __init__(self):
self.token_key = None
def get_tkk(self, searchnow=True):
if not searchnow:
return '445758.528423148'
tkk_expr = False
times = 5
while(not tkk_expr):
times -= 1
response = get("https://translate.google.com/")
tkk_expr = re.search("(tkk:.*?),", response.text)
if not tkk_expr:
if times == 0:
print("NOT FOUND TTK")
return '445758.528423148'
else:
continue
tkk_expr = tkk_expr.group(1)
# if not tkk_expr:
# raise ValueError(
# "Unable to find token seed! Did https://translate.google.com change?"
# )
return tkk_expr
def calculate_token(self, text, seed=None):
if seed is None:
seed = self._get_token_key()
[first_seed, second_seed] = seed.split(".")
try:
d = bytearray(text.encode('UTF-8'))
except UnicodeDecodeError:
# This will probably only occur when d is actually a str containing UTF-8 chars, which means we don't need
# to encode.
d = bytearray(text)
a = int(first_seed)
for value in d:
a += value
a = self._work_token(a, self.SALT_1)
a = self._work_token(a, self.SALT_2)
a ^= int(second_seed)
if 0 > a:
a = (a & 2147483647) + 2147483648
a %= 1E6
a = int(a)
return str(a) + "." + str(a ^ int(first_seed))
def _get_token_key(self):
if self.token_key is not None:
return self.token_key
tkk_expr = self.get_tkk(True)# If TTK is not work, change to True and let it search by it self
try:
# Grab the token directly if already generated by function call
result = re.search("\d{6}\.[0-9]+", tkk_expr).group(0)
except AttributeError:
# Generate the token using algorithm
timestamp = calendar.timegm(time.gmtime())
hours = int(math.floor(timestamp / 3600))
a = re.search("a\\\\x3d(-?\d+);", tkk_expr).group(1)
b = re.search("b\\\\x3d(-?\d+);", tkk_expr).group(1)
result = str(hours) + "." + str(int(a) + int(b))
self.token_key = result
return result
""" Functions used by the token calculation algorithm """
def _rshift(self, val, n):
return val >> n if val >= 0 else (val + 0x100000000) >> n
def _work_token(self, a, seed):
for i in range(0, len(seed) - 2, 3):
char = seed[i + 2]
d = ord(char[0]) - 87 if char >= "a" else int(char)
d = self._rshift(a, d) if seed[i + 1] == "+" else a << d
a = a + d & 4294967295 if seed[i] == "+" else a ^ d
return a
if __name__ == '__main__':
token = Token()
text = "ruirui ai weiwei"
print("text:", text)
print("tk:", token.calculate_token(text))