forked from GitFree/wx-btchelper
/
btchelper.py
336 lines (284 loc) · 10 KB
/
btchelper.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#encoding=utf-8
from bottle import request, get, post
import hashlib
import time
import xml.etree.ElementTree as ET
import settings
from fetcher import Mtgox, BTCE, BTCChina, Fxbtc, CN42BTC
from fetcher import FetcherThread, get_usd_cny_currency
WEIXIN_TOKEN = '55ac87b3ffb018bd583248873385f775'
class ResponsePost():
def __init__(self, msg_dic):
self.msg_dic = msg_dic
def nonce():
return str(int(time.time() * 1e6))
def response_txt(self, content, flag=0):
"""response a text message
flag 0--DONT star this message
1--star this message
"""
return settings.RESPONSE_TXT % (
self.msg_dic['FromUserName'],
self.msg_dic['ToUserName'],
int(time.time()),
'text',
content,
flag)
def help_info(self):
"""帮助信息
"""
return self.response_txt(settings.RESPONSE_HELP)
def btc(self):
mt_usd = Mtgox(currency='USD')
#mt_cny = Mtgox(currency='CNY')
btce = BTCE()
btcc = BTCChina()
fxbtc = Fxbtc()
cn42btc = CN42BTC()
list_instance = [mt_usd, btce, btcc, fxbtc, cn42btc]
list_thread = []
for instance in list_instance:
t = FetcherThread(instance)
list_thread.append(t)
t.daemon = True
t.start()
# wait for all thread finish
for t in list_thread:
t.join()
for instance in list_instance:
if instance.error:
instance = None
usd_cny_currency = get_usd_cny_currency()
content = u"""比特币实时行情汇总
----------------
Mtgox价格:$%.2f,合¥%.2f
MtGox日交量:%s
BTC-E价格:$%.2f,合¥%.2f
BTC-E日交量:%.4f BTC
BTCChina价格:¥%.2f
BTCChina日交量:%.4f BTC
FXBTC价格:¥%.2f
FXBTC日交量:%.4f BTC
42BTC价格:¥%.2f
42BTC日交量:%.4f BTC
""" %\
(0 if mt_usd is None else mt_usd.last_all,
mt_usd.last_all * usd_cny_currency,
0 if mt_usd is None else mt_usd.volume,
0 if btce is None else btce.last_all,
btce.last_all * usd_cny_currency,
0 if btce is None else btce.volume,
0 if btcc is None else btcc.last_all,
0 if btcc is None else btcc.volume,
0 if fxbtc is None else fxbtc.last_all,
0 if fxbtc is None else fxbtc.volume,
0 if cn42btc is None else cn42btc.last_all,
0 if cn42btc is None else cn42btc.volume)
return self.response_txt(content)
def ltc(self):
btce_ltcusd = BTCE(coin='ltc_usd')
btce_ltcbtc = BTCE(coin='ltc_btc')
fx_ltccny = Fxbtc(coin='ltc_cny')
fx_ltcbtc = Fxbtc(coin='ltc_btc')
list_instance = [btce_ltcusd, btce_ltcbtc, fx_ltccny, fx_ltcbtc]
list_thread = []
for instance in list_instance:
t = FetcherThread(instance)
list_thread.append(t)
t.daemon = True
t.start()
# wait for all thread finish
for t in list_thread:
t.join()
for instance in list_instance:
if instance.error:
instance = None
usd_cny_currency = get_usd_cny_currency()
content = u"""利特币实时行情汇总
-----------------
BTC-E价格1:$%.2f,合¥%.2f
BTC-E价格2:%.4f BTC
BTC-E日交量:%.2f LTC
FXBTC价格1:¥%.2f
FXBTC价格2:%.4f BTC
FXBTC日交量:%.2f LTC""" %\
(0 if btce_ltcusd is None else btce_ltcusd.last_all,
btce_ltcusd.last_all * usd_cny_currency,
0 if btce_ltcbtc is None else btce_ltcbtc.last_all,
0 if (btce_ltcusd and btce_ltcbtc) is None else btce_ltcusd.volume + btce_ltcbtc.volume,
0 if fx_ltccny is None else fx_ltccny.last_all,
0 if fx_ltcbtc is None else btce_ltcbtc.last_all,
0 if (fx_ltccny and fx_ltcbtc) is None else fx_ltccny.volume + fx_ltcbtc.volume)
return self.response_txt(content)
def mtgox(self):
mt_usd = Mtgox(currency='USD')
mt_usd.get_ticker()
if mt_usd.error:
return self.response_txt(mt_usd.error)
usd_cny_currency = get_usd_cny_currency()
content = u"""MtGox比特币实时行情
---------------
最新成交价:$%.2f,合¥%.2f
日交量:%s
最高成交价:$%.2f,合¥%.2f
最低成交价:$%.2f,合¥%.2f
最新买入价:$%.2f,合¥%.2f
最新卖出价:$%.2f,合¥%.2f
加权平均价:$%.2f,合¥%.2f""" %\
(mt_usd.last_all,
mt_usd.last_all * usd_cny_currency,
mt_usd.volume,
mt_usd.high,
mt_usd.high * usd_cny_currency,
mt_usd.low,
mt_usd.low * usd_cny_currency,
mt_usd.last_buy,
mt_usd.last_buy * usd_cny_currency,
mt_usd.last_sell,
mt_usd.last_sell * usd_cny_currency,
mt_usd.vwap,
mt_usd.vwap * usd_cny_currency)
return self.response_txt(content)
def btce(self):
btce = BTCE()
btce.get_ticker()
if btce.error:
return self.response_txt(btce.error)
usd_cny_currency = get_usd_cny_currency()
content = u"""BTC-E比特币实时行情
---------------
最新成交价:$%.2f,合¥%.2f
日交量:%.4f BTC
最高成交价:$%.2f,合¥%.2f
最低成交价:$%.2f,合¥%.2f
最新买入价:$%.2f,合¥%.2f
最新卖出价:$%.2f,合¥%.2f""" %\
(btce.last_all,
btce.last_all * usd_cny_currency,
btce.volume,
btce.high,
btce.high * usd_cny_currency,
btce.low,
btce.low * usd_cny_currency,
btce.last_buy,
btce.last_buy * usd_cny_currency,
btce.last_sell,
btce.last_sell * usd_cny_currency)
return self.response_txt(content)
def btcchina(self):
btcc = BTCChina()
btcc.get_ticker()
if btcc.error:
return self.response_txt(btcc.error)
content = u"""BTCChina比特币实时行情
---------------
最新成交价:¥%.2f
日交量:%.4f BTC
最高成交价:¥%.2f
最低成交价:¥%.2f
最新买入价:¥%.2f
最新卖出价:¥%.2f""" %\
(btcc.last_all, btcc.volume, btcc.high,
btcc.low, btcc.last_buy, btcc.last_sell)
return self.response_txt(content)
def fxbtc(self):
fxbtc = Fxbtc()
fxbtc.get_ticker()
if fxbtc.error:
return self.response_txt(fxbtc.error)
content = u"""FXBTC比特币实时行情
---------------
最新成交价:¥%.2f
日交量:%.4f BTC
最高成交价:¥%.2f
最低成交价:¥%.2f
最新买入价:¥%.2f
最新卖出价:¥%.2f""" %\
(fxbtc.last_all, fxbtc.volume, fxbtc.high,
fxbtc.low, fxbtc.last_buy, fxbtc.last_sell)
return self.response_txt(content)
def cn42btc(self):
cn42btc = CN42BTC()
cn42btc.get_ticker()
if cn42btc.error:
return self.response_txt(cn42btc.error)
content = u"""42BTC比特币实时行情
---------------
最新成交价:¥%.2f
日交量:%.4f BTC
最高成交价:¥%.2f
最低成交价:¥%.2f
平均成交价:¥%.2f
最新买入价:¥%.2f
最新卖出价:¥%.2f""" %\
(cn42btc.last_all, cn42btc.volume, cn42btc.high,
cn42btc.low, cn42btc.average, cn42btc.last_buy, cn42btc.last_sell)
return self.response_txt(content)
def todo(self):
"""TODO list"""
return self.response_txt(settings.RESPONSE_TODO)
def others(self):
return self.response_txt(u'不支持的命令,输入 h 或 help 查看帮助。', 1)
def recvmsg2dic():
"""parse received message to dictionary
"""
recvmsg = request.body.read()
root = ET.fromstring(recvmsg)
msg_dic = {}
for child in root:
msg_dic[child.tag] = child.text
return msg_dic
def handle_post(msg_dic):
resp = ResponsePost(msg_dic)
if msg_dic['MsgType'] == 'event':
if msg_dic['Event'] == 'subscribe': # new user subscribe
return resp.response_txt(settings.RESPONSE_SUBSCRIBE)
elif msg_dic['MsgType'] != 'text': # only text post supported
return resp.response_txt(settings.RESPONSE_UNSUPPORTED_TYPE, 1)
else: # text type post received
content = msg_dic['Content'].lower().strip()
if content in settings.KEYWORDS_DIC['help']:
return resp.help_info()
if content in settings.KEYWORDS_DIC['btc']:
return resp.btc()
if content in settings.KEYWORDS_DIC['ltc']:
return resp.ltc()
if content in settings.KEYWORDS_DIC['mtgox']:
return resp.mtgox()
if content in settings.KEYWORDS_DIC['btce']:
return resp.btce()
if content in settings.KEYWORDS_DIC['btcchina']:
return resp.btcchina()
if content in settings.KEYWORDS_DIC['fxbtc']:
return resp.fxbtc()
if content in settings.KEYWORDS_DIC['42btc']:
return resp.cn42btc()
if content in settings.KEYWORDS_DIC['todo']:
return resp.todo()
return resp.others()
@get('/btchelper')
def check_signature():
"""check if the get request was from winxin.
1.sorting signature,timestamp,nonce
2.sha1 the three args
3.return echoStr to winxin
"""
global WEIXIN_TOKEN
signature = request.GET.get("signature", None)
timestamp = request.GET.get("timestamp", None)
nonce = request.GET.get("nonce", None)
echoStr = request.GET.get("echostr", None)
token = WEIXIN_TOKEN
tmpList = [token, timestamp, nonce]
tmpList.sort()
tmpstr = "%s%s%s" % tuple(tmpList)
tmpstr = hashlib.sha1(tmpstr).hexdigest()
print tmpstr
if tmpstr == signature:
return echoStr
else:
return 'signature wrong!!'
@post('/btchelper')
def response_post():
msg_dic = recvmsg2dic()
return handle_post(msg_dic)