-
Notifications
You must be signed in to change notification settings - Fork 3
/
tipnem.py
296 lines (261 loc) · 9.09 KB
/
tipnem.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/user/env python3
# -*- coding: utf-8 -*-
from ed25519 import Ed25519
import websocket
import threading
import queue
import logging
import time
import json
import random
"""
Tipnemを扱うライブラリ
2017/11/07
"""
version = "2.2"
author = "namuyan"
class WebSocketClient:
timeout = 60
result = dict()
result_lock = threading.Lock()
streaming_que = queue.LifoQueue(maxsize=1000)
user_code = None
auto_login_setting = dict()
level = 0
def __init__(self, url, test=False):
self.url = url
self.test = test
self.ws = None
def _create_connect(self):
retry = 0
while True:
try:
retry += 1
websocket.enableTrace(self.test)
ws = websocket.WebSocketApp(self.url)
ws.on_message = self.on_message
ws.on_close = self.on_error
ws.on_open = self.on_open
ws.on_error = self.on_error
ws.run_forever()
if len(self.auto_login_setting) == 3:
logging.error("# disconnect! retry 60s after %s" % retry)
time.sleep(60)
threading.Thread(
target=self.login_by_key, args=(None, None, None),
name="login", daemon=True
).start()
else:
logging.error("# disconnect!")
return
except Exception as e:
logging.error(e)
logging.error("# retry connect to tipnem after 180s, %s" % retry)
time.sleep(180)
def login_by_key(self, seckey, pubkey, screen, auto_login=False):
count = 0
while self.user_code is None and count < 100:
count += 1
time.sleep(0.2)
if len(self.auto_login_setting) == 3 and seckey is None:
seckey = self.auto_login_setting["seckey"]
pubkey = self.auto_login_setting["pubkey"]
screen = self.auto_login_setting["screen"]
elif auto_login:
self.auto_login_setting = {
"seckey": seckey,
"pubkey": pubkey,
"screen": screen
}
sign = Ed25519.sign(message=self.user_code, secret_key=seckey, public_key=pubkey)
data = {
"screen_name": "@" + screen,
"sign": sign.decode()
}
ok, result = self.request(command="user/pubkey/login", data=data)
if not ok:
logging.error("pubkey login %s" % result)
return False
if result['level'] > 0:
logging.info("# auto login! level %d" % result['level'])
self.level = result['level']
return True
return False
def login_by_pin_user(self, level=2):
count = 0
while count < 20:
count += 1
print()
ok, result = self.request(command="user/upgrade", data={"require_level": level})
if not ok and result != 'pincode has been already created.':
print("# failed upgrade request: ", result)
continue
while True:
pin_code = input("pincode >> ")
if len(pin_code) != 4:
print("# need 4 letters")
continue
break
data = {"pincode": int(pin_code)}
ok, result = self.request(command="user/check", data=data)
if not ok:
print("# failed upgrade: ", result)
continue
print("# upgrade OK!")
self.level = level
return
raise Exception("failed upgrade user")
def login_by_pin_guest(self, screen):
count = 0
while count < 20:
count += 1
print()
data = {"screen_name": "@" + screen}
ok, result = self.request(command="user/offer", data=data)
if not ok and result != "pincode has been already created.":
print("# failed login request: ", result)
continue
while True:
pin_code = input("pincode >> ")
if len(pin_code) != 4:
print("# need 4 letters")
continue
break
data = {"screen_name": "@" + screen, "pincode": int(pin_code)}
ok, result = self.request(command="user/check", data=data)
if not ok:
print("# failed login: ", result)
continue
print("# login OK!")
self.level = 1
return
raise Exception("failed login user")
def start(self, name="tipnem"):
threading.Thread(
target=self._create_connect, name=name, daemon=True
).start()
while self.ws is None:
time.sleep(0.2)
logging.info("# create ws connection")
def blocking(self, uuid):
time_span = 0.005 # 1mS
for c in range(int(self.timeout / time_span)):
time.sleep(time_span)
with self.result_lock:
if uuid not in self.result:
continue
else:
data = self.result[uuid]
del (self.result[uuid])
return data['result'], data
else:
raise TimeoutError("UUID [%s]" % uuid)
def request(self, command, data=None, blocking=True):
uuid = random.randint(1, 2147483647)
message = {
"command": command,
"data": {"dummy": "data"} if data is None else data,
"uuid": uuid
}
logging.debug("id:%d,msg:%s" % (uuid, message))
self.ws.send(json.dumps(message))
if blocking is False:
return uuid
time_span = 0.005 # 1mS
for c in range(int(self.timeout / time_span)):
time.sleep(time_span)
if uuid not in self.result:
continue
else:
with self.result_lock:
r = self.result[uuid]
del(self.result[uuid])
return r['result'], r['data']
logging.error("result: %s" % self.result)
raise TimeoutError("timeout [%s] [%s] [%s]" % (uuid, command, data))
""" request """
@staticmethod
def check_key(keys, data):
for k in keys:
if k not in data:
return False
else:
return True
def on_message(self, ws, message):
logging.debug("msg: %s" % message)
try:
data = json.loads(message)
except Exception as e:
logging.error(e)
return
if self.check_key(('level', 'user_code'), data):
self.user_code = data['user_code']
return
elif self.check_key(('type', 'command'), data):
if data['type'] == 'streaming':
try:
item = (data['command'], data['data'], data['time'])
self.streaming_que.put_nowait(item=item)
except queue.Full:
pass
except Exception as e:
logging.error(e)
import traceback
traceback.print_exc()
elif data['type'] == 'request':
with self.result_lock:
self.result[data['uuid']] = data
else:
raise Exception("unknown type '%s'" % data['type'])
return
else:
raise Exception("none...")
def on_error(self, ws, error):
logging.error("error: %s" % error)
self.ws = None
self.user_code = None
self.result = dict()
self.level = 0
def on_close(self, ws):
logging.info("close: %s" % ws)
self.ws = None
self.user_code = None
self.result = dict()
self.level = 0
def on_open(self, ws):
self.ws = ws
logging.info("open: %s" % ws)
def test():
""" テストコード """
def _streaming(ws):
while True:
cmd, data, time_sec = ws.streaming_que.get()
print(cmd, data, time_sec)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(levelname)-6s] [%(threadName)-10s] [%(asctime)-24s] %(message)s')
sh = logging.StreamHandler()
sh.setLevel(logging.INFO)
sh.setFormatter(formatter)
logger.addHandler(sh)
logging.getLogger("urllib3").setLevel(logging.WARNING)
logging.getLogger("requests").setLevel(logging.WARNING)
ws = WebSocketClient(url="ws://153.122.86.46:8088")
ws.start()
threading.Thread(
target=_streaming, args=(ws,), daemon=True
).start()
ws.login_by_pin_guest(screen=input("screen >> "))
ws.login_by_pin_user()
while True:
try:
print()
command = input("cmd >> ")
data = json.loads(input("data >> "))
ok, result = ws.request(command=command, data=data)
print(ok)
print(result)
except Exception as e:
print("Error", e)
if __name__ == '__main__':
test()