-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweixind.py
executable file
·838 lines (659 loc) · 25.2 KB
/
weixind.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Filename: weixind.py
import os
import web
import time
import json
import types
import hashlib
import base64
import memcache
import threading
import RPi.GPIO as GPIO
from lxml import etree
from datetime import datetime, timedelta
from multiprocessing import Process, Manager
from weixin import WeiXinClient
from weixin import APIError
from weixin import AccessTokenError
from sakshat import SAKSHAT
#import raspberry
#Declare the SAKS Board
SAKS = SAKSHAT()
# 在这里设定闹钟定时时间
__alarm_time = [07, 30, 00]
__alarm_beep_status = False
#在检测到轻触开关触发时自动执行此函数
def tact_event_handler(pin, status):
'''
called while the status of tacts changed
:param pin: pin number which stauts of tact is changed
:param status: current status
:return: void
'''
global __alarm_beep_status
# 停止闹钟响铃(按下任何轻触开关均可触发)
__alarm_beep_status = False
def display_time():
global __alarm_time
global __alarm_beep_status
__dp = True
__alarm_beep_times = 0
#设定轻触开关回调函数
SAKS.tact_event_handler = tact_event_handler
while True:
# 以下代码获取系统时间、时、分、秒、星期的数值
t = time.localtime()
curTime = [t.tm_hour, t.tm_min, t.tm_sec]
if curTime == __alarm_time:
__alarm_beep_status = True
__alarm_beep_times = 0
if __dp:
# 数码管显示小时和分,最后一位的小点每秒闪烁一次
SAKS.digital_display.show(("%02d%02d." % (t.tm_hour, t.tm_min)))
else:
SAKS.digital_display.show(("%02d%02d" % (t.tm_hour, t.tm_min)))
if __alarm_beep_status == True:
__alarm_beep_times = __alarm_beep_times + 1
if __dp:
#SAKS.buzzer.on()
SAKS.ledrow.on()
else:
#SAKS.buzzer.off()
SAKS.ledrow.off()
if __alarm_beep_times > 20:
__alarm_beep_status = False
__dp = not __dp
time.sleep(0.5)
def set_alarm_time(strTime):
global __alarm_time
try:
temp = time.strptime(strTime, "%H:%M")
__alarm_time[0] = temp.tm_hour
__alarm_time[1] = temp.tm_min
__alarm_time[2] = 0
return True
except:
return False
def calc_remain_time(strTime):
curTime = time.localtime()
alarm_time = time.strptime(strTime, "%H:%M")
rTime = [alarm_time.tm_hour - curTime.tm_hour, alarm_time.tm_min - curTime.tm_min]
if rTime[1] < 0:
rTime[0] = rTime[0] - 1
rTime[1] = rTime[1] + 60
if rTime[0] < 0:
rTime[0] = rTime[0] + 24
return rTime
def get_room_temp():
#从 ds18b20 读取温度(摄氏度为单位)
#返回值为 -128.0 表示读取失败
temp = SAKS.ds18b20.temperature
#数码管显示温度数值,5位(含小数点)、精确到小数点1后1位
#SAKS.digital_display.show(("%5.1f" % temp).replace(' ','#'))
return temp
def __HW_PROC__():
display_time()
##########################################################################
###############################################################################
_TOKEN = 'dreamtale90'
#URL路径,处理类
_URLS = (
'/weixin', 'weixinserver',
'/heartbeat', 'checkserver',
)
root = 'oWB27wAZB7_ITyqO8ML_CafFfvgc'
class checkserver:
def GET(self):
return 'OK4LIVE'
def my_print(data):
curTime = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
print '[%s] %s' %(curTime, data)
def _check_hash(data):
signature = data.signature
timestamp = data.timestamp
nonce = data.nonce
list = [_TOKEN, timestamp, nonce]
list.sort()
sha1 = hashlib.sha1()
map(sha1.update, list)
hashcode = sha1.hexdigest()
if hashcode == signature: return True
return False
def _check_user(user_id):
user_list = [root, 'oWB27wHY_yeuDG27iJ91MudguUks', 'oWB27wEjytw9wY6_3o4W8tKrHtRA']
if user_id in user_list:
return True
return False
user_cache = dict()
def get_user_name(userInfo, user_id):
if user_id in user_cache:
return user_cache[user_id]
user_info = userInfo.info.dget(openid=user_id, lang='zh_CN')
user_cache[user_id] = user_info.nickname
return user_info.nickname
def _punctuation_clear(ostr):
'''Clear XML or dict using special punctuation'''
return str(ostr).translate(None, '\'\"<>&')
def _cpu_and_gpu_temp():
'''Get from pi'''
import commands
try:
fd = open('/sys/class/thermal/thermal_zone0/temp')
ctemp = fd.read()
fd.close()
gtemp = commands.getoutput('/opt/vc/bin/vcgencmd measure_temp').replace('temp=', '').replace('\'C', '')
except Exception, e:
return (0, 0)
return (float(ctemp) / 1000, float(gtemp))
def _udp_client(addr, data):
import select
import socket
mm = '{"errno":1, "msg":"d2FpdCByZXNwb25zZSB0aW1lb3V0"}'
c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
c.setblocking(False)
inputs = [c]
c.connect(addr)
c.sendall(data)
readable, writeable, exceptional = select.select(inputs, [], [], 3)
try:
if readable: mm = c.recv(2000)
except Exception, e:
mm = '{"errno":1, "msg":"%s"}' %(base64.b64encode(_punctuation_clear(e)))
finally:
c.close()
return mm
def _take_snapshot(client, curTime):
import picamera
camera = None
save_file = './../send_to_user/image/' + curTime + '.jpg'
try:
camera = picamera.PiCamera()
#camera.led = False
camera.annotate_text_size = 20
camera.annotate_foreground = picamera.Color('#FFFF00')
camera.annotate_text = ' ' + curTime
camera.resolution = (720, 480)
#camera.sharpness = 0
#camera.contrast = 0
#camera.brightness = 50
#camera.saturation = 0
#camera.ISO = 0
#camera.video_stabilization = False
#camera.exposure_compensation = 0
#camera.exposure_mode = 'auto'
#camera.meter_mode = 'average'
#camera.awb_mode = 'auto'
#camera.image_effect = 'none'
#camera.color_effects = None
#camera.rotation = 180
#camera.hflip = False
#camera.vflip = False
#camera.crop = (0.0, 0.0, 1.0, 1.0)
time.sleep(0.5)
camera.capture(save_file)
finally:
camera.close()
return client.media.upload.file(type='image', jpg=open(save_file, 'rb'))
def _take_video(client, curTime):
import picamera
import commands
camera = None
temp_file = '/tmp/input_raw.h264'
image_file = './../send_to_user/video/' + curTime + '.jpg'
video_file = './../send_to_user/video/' + curTime + '.mp4'
try:
camera = picamera.PiCamera()
camera.resolution = (720, 480)
camera.start_recording(temp_file)
camera.capture(image_file, resize = (160, 120))
camera.wait_recording(9)
camera.stop_recording()
#need to convert h264 to mp4
shell_cmd = 'MP4Box -add %s %s' %(temp_file, video_file)
commands.getoutput(shell_cmd)
finally:
camera.close()
thumb = client.media.upload.file(type='image', jpg=open(image_file, 'rb'))
video = client.media.upload.file(type='video', mpeg4=open(video_file, 'rb'))
return [video.media_id, thumb.media_id]
def _talk_with_simsimi(topic):
import urllib2
topic = topic.encode('UTF-8')
entopic = urllib2.quote(topic)
send_headers = {
'Cookie':'Filtering=0.0; Filtering=0.0; isFirst=1; isFirst=1; simsimi_uid=50840753; simsimi_uid=50840753; teach_btn_url=talk; teach_btn_url=talk; sid=s%3AzwUdofEDCGbrhxyE0sxhKEkF.1wDJhD%2BASBfDiZdvI%2F16VvgTJO7xJb3ZZYT8yLIHVxw; selected_nc=zh; selected_nc=zh; menuType=web; menuType=web; __utma=119922954.2139724797.1396516513.1396516513.1396703679.3; __utmc=119922954; __utmz=119922954.1396516513.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)'
}
baseurl = r'http://www.simsimi.com/func/reqN?lc=zh&ft=0.0&req='
url = baseurl+entopic
req = urllib2.Request(url,headers=send_headers)
resp = urllib2.urlopen(req)
answer = json.loads(resp.read())
return answer
def _do_event_subscribe(server, fromUser, toUser, doc):
send_info_to_root(server.client, fromUser, 'text', 'subscribe' + ' (' + fromUser + ')')
return server._reply_text(fromUser, toUser, u'hello!')
def _do_event_unsubscribe(server, fromUser, toUser, doc):
return server._reply_text(fromUser, toUser, u'bye!')
def _do_event_SCAN(server, fromUser, toUser, doc):
pass
def _do_event_LOCATION(server, fromUser, toUser, doc):
pass
last_user = 'username'
last_event = 'event_type'
last_time = 0
def _do_event_CLICK(server, fromUser, toUser, doc):
global last_user, last_event, last_time
t = time.localtime()
curTime = t.tm_hour*60 + t.tm_min*60 + t.tm_sec
key = doc.find('EventKey').text
user = get_user_name(server.client.user, fromUser)
# the same operate from the same user one time in 5s.
if (last_user == user and
last_event == key and
(curTime - last_time) < 5):
return 'success'
last_user = user
last_event = key
last_time = curTime
send_info_to_root(server.client, fromUser, 'text', key)
try:
return _weixin_click_table[key](server, fromUser, toUser, doc)
except KeyError, e:
#print '_do_event_CLICK: %s' %e
return server._reply_text(fromUser, toUser, u'Unknow click: '+key)
_weixin_event_table = {
'subscribe' : _do_event_subscribe,
'unsbscribe' : _do_event_unsubscribe,
'SCAN' : _do_event_SCAN,
'LOCATION' : _do_event_LOCATION,
'CLICK' : _do_event_CLICK,
}
def _do_click_V1001_TEMPERATURE(server, fromUser, toUser, doc):
thread = threading.Thread(target = assistant, args = (fromUser, 'temp', None))
thread.start()
return 'success'
def _do_click_V1002_PICTURES(server, fromUser, toUser, doc):
if not _check_user(fromUser):
return server._reply_text(fromUser, toUser, u'Permission denied…')
thread = threading.Thread(target = assistant, args = (fromUser, 'image', None))
thread.start()
return 'success'
def _do_click_V1003_VOICE(server, fromUser, toUser, doc):
if not _check_user(fromUser):
return server._reply_text(fromUser, toUser, u'Permission denied…')
return server._reply_text(fromUser, toUser, u'This feature is still under development. Stay tuned !')
def _do_click_V1004_VIDEO(server, fromUser, toUser, doc):
if not _check_user(fromUser):
return server._reply_text(fromUser, toUser, u'Permission denied…')
thread = threading.Thread(target = assistant, args = (fromUser, 'video', None))
thread.start()
return 'success'
def _do_click_V2001_FORWARD(server, fromUser, toUser, doc):
if not _check_user(fromUser):
return server._reply_text(fromUser, toUser, u'Permission denied…')
# BOARD编号方式,基于插座引脚编号
GPIO.setmode(GPIO.BCM)
# 输出模式
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.output(17, GPIO.HIGH)
GPIO.output(27, GPIO.LOW)
GPIO.output(23, GPIO.HIGH)
GPIO.output(24, GPIO.LOW)
time.sleep(2)
GPIO.cleanup()
return 'success'
def _do_click_V2002_BACKWARD(server, fromUser, toUser, doc):
if not _check_user(fromUser):
return server._reply_text(fromUser, toUser, u'Permission denied…')
# BOARD编号方式,基于插座引脚编号
GPIO.setmode(GPIO.BCM)
# 输出模式
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.output(17, GPIO.LOW)
GPIO.output(27, GPIO.HIGH)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.HIGH)
time.sleep(2)
GPIO.cleanup()
return 'success'
def _do_click_V2003_TURNLEFT(server, fromUser, toUser, doc):
if not _check_user(fromUser):
return server._reply_text(fromUser, toUser, u'Permission denied…')
# BOARD编号方式,基于插座引脚编号
GPIO.setmode(GPIO.BCM)
# 输出模式
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.output(17, GPIO.LOW)
GPIO.output(27, GPIO.HIGH)
GPIO.output(23, GPIO.HIGH)
GPIO.output(24, GPIO.LOW)
time.sleep(0.22)
GPIO.cleanup()
return 'success'
def _do_click_V2004_TURNRIGHT(server, fromUser, toUser, doc):
if not _check_user(fromUser):
return server._reply_text(fromUser, toUser, u'Permission denied…')
# BOARD编号方式,基于插座引脚编号
GPIO.setmode(GPIO.BCM)
# 输出模式
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.output(17, GPIO.HIGH)
GPIO.output(27, GPIO.LOW)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.HIGH)
time.sleep(0.22)
GPIO.cleanup()
return 'success'
def _do_click_V2002_FUNC(server, fromUser, toUser, doc):
return server._reply_text(fromUser, toUser, u'This feature is still under development. Stay tuned !')
def _do_click_V3001_FUNC(server, fromUser, toUser, doc):
return server._reply_text(fromUser, toUser, u'This feature is still under development. Stay tuned !')
def _do_click_V3002_FUNC(server, fromUser, toUser, doc):
return server._reply_text(fromUser, toUser, u'This feature is still under development. Stay tuned !')
_weixin_click_table = {
'V1001_TEMPERATURE' : _do_click_V1001_TEMPERATURE,
'V1002_PICTURES' : _do_click_V1002_PICTURES,
'V1003_VOICE' : _do_click_V1003_VOICE,
'V1004_VIDEO' : _do_click_V1004_VIDEO,
'V2001_FORWARD' : _do_click_V2001_FORWARD,
'V2002_BACKWARD' : _do_click_V2002_BACKWARD,
'V2003_TURNLEFT' : _do_click_V2003_TURNLEFT,
'V2004_TURNRIGHT' : _do_click_V2004_TURNRIGHT,
'V3001_FUNC' : _do_click_V3001_FUNC,
'V3002_FUNC' : _do_click_V3002_FUNC,
}
def _do_get_temp():
c, g = _cpu_and_gpu_temp()
t = get_room_temp()
reply_msg = 'CPU : %.02f℃\nGPU : %.02f℃\nRoom : %.02f℃' %(c, g, t)
return reply_msg
def _do_text_command(server, fromUser, toUser, content):
temp = content.split(' ')
try:
return _weixin_text_command_table[temp[0]](server, fromUser, toUser, content[len(temp[0]) + 1:])
except KeyError, e:
return server._reply_text(fromUser, toUser, u'Unknow command: '+temp[0])
def _do_exec_command(server, fromUser, toUser, cmd):
import commands
save_file = '/tmp/wx_command.txt'
if not _check_user(fromUser):
return server._reply_text(fromUser, toUser, u'Permission denied…')
err_msg = 'exec cmd fail: '
try:
shell_cmd = cmd + ' > ' + save_file + ' 2>&1'
commands.getoutput(shell_cmd)
result = ''
fd = open(save_file, 'rb')
for line in fd:
text = line.rstrip()
if len(text) == 0:
continue
#Max Text Message Length 2048 bytes(UTF-8)
if len(result) + len(text) > 2000:
break
result += text + '\n'
fd.close()
if result == '':
result = u'No Output'
return server._reply_text(fromUser, toUser, result)
except Exception, e:
err_msg += _punctuation_clear(str(e))
my_print(err_msg)
return server._reply_text(fromUser, toUser, err_msg)
def _do_set_alarm(server, fromUser, toUser, content):
#check content and set alarm
if set_alarm_time(content) == True:
rTime = calc_remain_time(content)
reply_msg = u'Alarm set for %s hours and %s minutes from now' %(rTime[0], rTime[1])
return server._reply_text(fromUser, toUser, reply_msg)
else:
return server._reply_text(fromUser, toUser, u'set alarm failed(e.g. 06:30)')
def _do_print_help(server, fromUser, toUser, para):
data = "commands:\n"
for (k, v) in _weixin_text_command_table.items():
data += "\t%s\n" %(k)
return server._reply_text(fromUser, toUser, data)
_weixin_text_command_table = {
'help' : _do_print_help,
'cmd' : _do_exec_command,
'alarm' : _do_set_alarm,
#'image' : _do_set_camera,
}
menu = '''{
"button":[
{
"name":"监 控",
"sub_button":[
{
"type":"click",
"name":" 温 度 ",
"key":"V1001_TEMPERATURE"
},
{
"type":"click",
"name":" 拍 照 ",
"key":"V1002_PICTURES"
},
{
"type":"click",
"name":" 录 音 ",
"key":"V1003_VOICE"
},
{
"type":"click",
"name":" 录 像 ",
"key":"V1004_VIDEO"
}]
}]
,
"button":[
{
"name":"移 动",
"sub_button":[
{
"type":"click",
"name":" 前 进 ",
"key":"V2001_FORWARD"
},
{
"type":"click",
"name":" 后 退 ",
"key":"V2002_BACKWARD"
},
{
"type":"click",
"name":" 左 转 ",
"key":"V2003_TURNLEFT"
},
{
"type":"click",
"name":" 右 转 ",
"key":"V2004_TURNRIGHT"
}]
}]
,
"button":[
{
"name":"Menu3",
"sub_button":[
{
"type":"click",
"name":"Func5",
"key":"V3001_FUNC"
},
{
"type":"click",
"name":"Func6",
"key":"V3002_FUNC"
}]
}]
}'''
def assistant(toUser, msgType, content):
ret = None
wx = weixinserver()
if msgType == 'text':
ret = wx._send_text(toUser, content)
if msgType == 'image':
ret = wx._send_image(toUser)
if msgType == 'voice':
ret = wx._send_voice(toUser)
if msgType == 'video':
ret = wx._send_video(toUser)
if msgType == 'temp':
ret = wx._send_text(toUser, _do_get_temp())
def send_info_to_root(server, fromUser, msgType, content):
curTime = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
if server is None or fromUser is None:
message = '[%s] %s' %(curTime, content)
else:
message = '[%s] %s -> %s' %(curTime, get_user_name(server.user, fromUser), content)
print (message)
if fromUser == root:
return
thread = threading.Thread(target = assistant, args = (root, msgType, message))
thread.start()
class weixinserver:
def __init__(self):
#获取执行文件路径
self.app_root = os.path.dirname(__file__)
#设置回复模板路径
self.templates_root = os.path.join(self.app_root, 'templates')
#初始化回复模板
self.render = web.template.render(self.templates_root)
#微信测试公众号
self.client = WeiXinClient('wxaece866e46e9d4a6', 'c104ddad7eef2e369acb1aee01bf8341')
try:
self.client.request_access_token()
self.client.menu.delete.post()
self.client.menu.create.post(body=menu)
except Exception, e:
self.client.set_access_token('ThisIsAFakeToken', 1800, persistence=True)
def _recv_text(self, fromUser, toUser, doc):
content = doc.find('Content').text
send_info_to_root(self.client, fromUser, 'text', content)
if content[0] == '.':
return _do_text_command(self, fromUser, toUser, content[1:])
reply_msg = content
#reply_msg = _talk_with_simsimi(content)
return self._reply_text(fromUser, toUser, reply_msg)
def _recv_event(self, fromUser, toUser, doc):
event = doc.find('Event').text
try:
return _weixin_event_table[event](self, fromUser, toUser, doc)
except KeyError, e:
return self._reply_text(fromUser, toUser, u'Unknow event:%s' %event)
def _recv_image(self, fromUser, toUser, doc):
url = doc.find('PicUrl').text
mid = doc.find('MediaId').text
rm = self.client.media.get.file(media_id=mid)
fname = '/home/pi/Downloads/wx/wx_%s.jpg' %(time.strftime("%Y_%m_%dT%H_%M_%S", time.localtime()))
fd = open(fname, 'wb'); fd.write(rm.read()); fd.close(); rm.close()
return self._reply_text(fromUser, toUser, u'upload to:%s' %url)
def _recv_voice(self, fromUser, toUser, doc):
#import subprocess
cmd = doc.find('Recognition').text
mid = doc.find('MediaId').text
rm = self.client.media.get.file(media_id=mid)
fname = '/home/pi/Downloads/wx/wx_%s.amr' %(time.strftime("%Y_%m_%dT%H_%M_%S", time.localtime()))
fd = open(fname, 'wb'); fd.write(rm.read()); fd.close(); rm.close()
#subprocess.call(['omxplayer', '-o', 'local', fname])
if cmd is None:
return self._reply_text(fromUser, toUser, u'no Recognition, no command');
return self._reply_text(fromUser, toUser, u'Unknow recognition:%s' %cmd);
def _recv_video(self, fromUser, toUser, doc):
pass
def _recv_shortvideo(self, fromUser, toUser, doc):
mid = doc.find('MediaId').text
rm = self.client.media.get.file(media_id=mid)
fname = '/home/pi/Downloads/wx/wx_%s.mp4' %(time.strftime("%Y_%m_%dT%H_%M_%S", time.localtime()))
fd = open(fname, 'wb'); fd.write(rm.read()); fd.close(); rm.close()
return self._reply_text(fromUser, toUser, u'shortvideo:%s' %fname);
def _recv_location(self, fromUser, toUser, doc):
pass
def _recv_link(self, fromUser, toUser, doc):
pass
def _reply_text(self, toUser, fromUser, msg):
return self.render.reply_text(toUser, fromUser, int(time.time()), msg)
def _reply_image(self, toUser, fromUser, media_id):
return self.render.reply_image(toUser, fromUser, int(time.time()), media_id)
def _reply_news(self, toUser, fromUser, title, descrip, picUrl, hqUrl):
return self.render.reply_news(toUser, fromUser, int(time.time()), title, descrip, picUrl, hqUrl)
def _send_text(self, toUser, content):
data = '{"touser":"%s", "msgtype":"text", "text":{"content":"%s"}}' %(toUser, content)
return self.client.message.custom.send.post(body = data)
def _send_image(self, toUser):
image = None
curTime = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
send_info_to_root(self.client, toUser, 'text', 'image:' + curTime)
try:
image = _take_snapshot(self.client, curTime)
except Exception, e:
err_msg = 'take snapshot fail: ' + _punctuation_clear(str(e))
my_print(err_msg)
return None
data = '{"touser":"%s", "msgtype":"image", "image":{"media_id":"%s"}}' %(toUser, image.media_id)
return self.client.message.custom.send.post(body = data)
def _send_voice(self, toUser):
return
def _send_video(self, toUser):
mediaID = [0, 0]
curTime = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
send_info_to_root(self.client, toUser, 'text', 'video:' + curTime)
try:
mediaID = _take_video(self.client, curTime)
except Exception, e:
err_msg = 'take snapshot fail: ' + _punctuation_clear(str(e))
my_print(err_msg)
data = '{"touser":"%s", "msgtype":"video", "video":{"media_id":"%s", "thumb_media_id":"%s", "title":"%s", "description":"%s"}}' %(toUser, mediaID[0], mediaID[1], 'provide by dreamtale90', curTime)
return self.client.message.custom.send.post(body = data)
def GET(self):
data = web.input()
try:
if _check_hash(data):
return data.echostr
except Exception, e:
return None
def POST(self):
str_xml = web.data()
doc = etree.fromstring(str_xml)
msgType = doc.find('MsgType').text
fromUser = doc.find('FromUserName').text
toUser = doc.find('ToUserName').text
if msgType == 'text':
return self._recv_text(fromUser, toUser, doc)
if msgType == 'event':
return self._recv_event(fromUser, toUser, doc)
if msgType == 'image':
return self._recv_image(fromUser, toUser, doc)
if msgType == 'voice':
return self._recv_voice(fromUser, toUser, doc)
if msgType == 'video':
return self._recv_video(fromUser, toUser, doc)
if msgType == 'shortvideo':
return self._recv_shortvideo(fromUser, toUser, doc)
if msgType == 'location':
return self._recv_location(fromUser, toUser, doc)
if msgType == 'link':
return self._recv_link(fromUser, toUser, doc)
else:
return self._reply_text(fromUser, toUser, u'Unknow msg:' + msgType)
#raspberry = Process(target = __HW_PROC__)
application = web.application(_URLS, globals())
if __name__ == "__main__":
#send_info_to_root(None, None, 'text', 'WeChat Start !')
#raspberry.start()
application.run()