-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode.py
More file actions
executable file
·114 lines (103 loc) · 5.48 KB
/
code.py
File metadata and controls
executable file
·114 lines (103 loc) · 5.48 KB
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
#!/usr/bin/env python
#coding=utf-8
import web
import hashlib, time, re
import urllib, urllib2
import MySQLdb
from lxml import etree
from xiezhua import xiezhua
import hanzi
import photo
#import timeline
db = web.database(dbn='mysql',user='root',pw='password',db='info')
render = web.template.render('templates/')
urls = (
'/', 'index'
)
class index:
def GET(self):
token = "xiezhua"
params = web.input()
args = [token, params['timestamp'], params['nonce']]
args.sort()
if hashlib.sha1("".join(args)).hexdigest() == params['signature']:
if params.has_key('echostr'):
return params['echostr']
def POST(self):
str_xml = web.data()
xml = etree.fromstring(str_xml)
msgType = xml.find("MsgType").text
fromUser= xml.find("FromUserName").text
toUser = xml.find("ToUserName").text
if msgType == 'event':
event = xml.find("Event").text
if event == 'subscribe' :
return render.weixin(fromUser,toUser,int(time.time()),hanzi.hello)
elif event == 'unsubscribe':
db.delete('info', where="weixin_id=$fromUser",vars=locals())
else:
pass
elif msgType == 'text' :
content = xml.find("Content").text
id = db.select('info' ,what="id" ,where="weixin_id=$fromUser",vars=locals())
if len(id) == 0:
if len(content) > 50:
return render.weixin(fromUser,toUser,int(time.time()),hanzi.hello)
else:
code,xiezhua_xml = xiezhua(content.strip(),hanzi.bind_ok,hanzi.weixin,'talk')
if code != 200:
return render.weixin(fromUser,toUser,int(time.time()),str(code)+hanzi.hello)
else:
xml0 = etree.fromstring(xiezhua_xml)
id = xml0[0][6][0].text
db.insert('info', id=id ,weixin_id=fromUser ,xiezhua_id=content ,sendfrom=hanzi.weixin)
return render.weixin(fromUser,toUser,int(time.time()),hanzi.bind_ok)
else:
xiezhua_id = db.select('info' ,what="xiezhua_id",where="weixin_id=$fromUser" ,vars=locals())
sendfrom = db.select('info' ,what="sendfrom" ,where="weixin_id=$fromUser" ,vars=locals())
if content == 'help':
return render.weixin(fromUser,toUser,int(time.time()),hanzi.help)
elif content[0:8:] == 'sendfrom':
sendfrom = content[8::]
db.update('info' ,where="weixin_id=$fromUser" ,sendfrom=sendfrom ,vars=locals())
sendfrom = db.select('info',what="sendfrom" ,where="weixin_id=$fromUser" ,vars=locals())
if sendfrom[0].sendfrom == content[8::]:
code,xiezhua_xml = xiezhua(xiezhua_id[0].xiezhua_id,hanzi.sendfrom_ok+content[8::],content[8::],'talk')
return render.weixin(fromUser,toUser,int(time.time()),hanzi.sendfrom_ok+content[8::])
else:
return render.weixin(fromUser,toUser,int(time.time()),hanzi.sendfrom_fail)
code,xiezhua_xml = xiezhua(xiezhua_id[0].xiezhua_id,hanzi.sendfrom_fail,sendfrom[0].sendfrom.encode('raw_unicode_escape'),'talk')
elif content==hanzi.timeline:
timeline = db.select('info',what="timeline",where="weixin_id=$fromUser" ,vars=locals())
timeline,string = timeline.get_string(xiezhua_id,timeline)
if timeline == 0:
return render.weixin(fromUser,toUser,int(time.time()),hanzi.nomore)
else:
db.update('info' ,where="weixin_id=$fromUser" ,timeline=timeline ,vars=locals())
return render.weixin(fromUser,toUser,int(time.time()),string)
else:
code,xiezhua_xml = xiezhua(xiezhua_id[0].xiezhua_id,content,sendfrom[0].sendfrom,'talk')
if code == 200:
return render.weixin(fromUser,toUser,int(time.time()),hanzi.send_ok)
else:
db.delete('info', where="weixin_id=$fromUser" ,vars=locals())
return render.weixin(fromUser,toUser,int(time.time()),hanzi.hello)
elif msgType == 'image' :
picurl = xml.find("PicUrl").text
msgid = xml.find("MediaId").text
photo.save(msgid,picurl)
id = db.select('info' ,what="id" ,where="weixin_id=$fromUser",vars=locals())
if len(id) == 0:
return render.weixin(fromUser,toUser,int(time.time()),hanzi.hello)
else:
xiezhua_id = db.select('info' ,what="xiezhua_id",where="weixin_id=$fromUser" ,vars=locals())
sendfrom = db.select('info' ,what="sendfrom" ,where="weixin_id=$fromUser" ,vars=locals())
pic = urllib2.urlopen(picurl).read()
code,xiezhua_xml = xiezhua(xiezhua_id[0].xiezhua_id,'''[img]'''+picurl+'''[/img]''',sendfrom[0].sendfrom,'talk')
if code == 200:
return render.weixin(fromUser,toUser,int(time.time()),hanzi.send_ok)
if __name__ == "__main__":
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
app = web.application(urls, globals())
time.sleep(0.01)
app.run()