-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGetTK.py
159 lines (109 loc) · 3.39 KB
/
GetTK.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
"""This module creates the TK GET parameter for Google translate.
This is just a code port to python. All credits should go to the original
creators of the code @tehmaestro and @helen5106.
For more info see: https://github.com/Stichoza/google-translate-php/issues/32
Usage:
Call this python script from the command line.
$ python tk_generator.py <word>
Use this module from another python script.
>>> import tk_generator
>>> tk_generator.get_tk('dog')
Attributes:
_ENCODING (string): Default encoding to be used during the string
encode-decode process.
"""
__all__ = ["getTk"]
import sys
from datetime import datetime
_ENCODING = "UTF-8"
# Helper functions
def _mb_strlen(string):
"""Get the length of the encoded string."""
return len(string.decode(_ENCODING))
def _mb_substr(string, start, length):
"""Get substring from the encoded string."""
return string.decode(_ENCODING)[start: start + length]
##################################################
def _shr32(x, bits):
if bits <= 0:
return x
if bits >= 32:
return 0
x_bin = bin(x)[2:]
x_bin_length = len(x_bin)
if x_bin_length > 32:
x_bin = x_bin[x_bin_length - 32: x_bin_length]
if x_bin_length < 32:
x_bin = x_bin.zfill(32)
return int(x_bin[:32 - bits].zfill(32), 2)
def _char_code_at(string, index):
return ord(_mb_substr(string, index, 1))
#OLD Function
def _generateB():
start = datetime(1970, 1, 1)
now = datetime.now()
diff = now - start
return int(diff.total_seconds() / 3600)
def _TKK():
"""Replacement for _generateB function."""
return [406604, 1836941114]
def _RL(a, b):
for c in range(0, len(b) - 2, 3):
d = b[c + 2]
if d >= 'a':
d = _char_code_at(d, 0) - 87
else:
d = int(d)
if b[c + 1] == '+':
d = _shr32(a, d)
else:
d = a << d
if b[c] == '+':
a = a + d & (pow(2, 32) - 1)
else:
a = a ^ d
return a
def _TL(a):
#b = _generateB()
tkk = _TKK()
b = tkk[0]
d = []
for f in range(0, _mb_strlen(a)):
g = _char_code_at(a, f)
if g < 128:
d.append(g)
else:
if g < 2048:
d.append(g >> 6 | 192)
else:
if ((g & 0xfc00) == 0xd800 and
f + 1 < _mb_strlen(a) and
(_char_code_at(a, f + 1) & 0xfc00) == 0xdc00):
f += 1
g = 0x10000 + ((g & 0x3ff) << 10) + (_char_code_at(a, f) & 0x3ff)
d.append(g >> 18 | 240)
d.append(g >> 12 & 63 | 128)
else:
d.append(g >> 12 | 224)
d.append(g >> 6 & 63 | 128)
d.append(g & 63 | 128)
a = b
for e in range(0, len(d)):
a += d[e]
a = _RL(a, "+-a^+6")
a = _RL(a, "+-3^+b+-f")
a = a ^ tkk[1]
if a < 0:
a = (a & (pow(2, 31) - 1)) + pow(2, 31)
a %= pow(10, 6)
return "%d.%d" % (a, a ^ b)
def getTk(word):
"""Returns the tk parameter for the given word."""
# print type(word)
if isinstance(word, unicode):
word = word.encode(_ENCODING)
return _TL(word)
if __name__ == '__main__':
print getTk("""(GMT+07:00) Bangkok, Jakarta""")