www-moai-toki /app_shimosheji/weixin.py

Language Python Lines 330
MD5 Hash 06e08d3f87dfcd3f403f7842a3c4af29
Repository https://github.com/rongoman/www-moai-toki.git View Raw File
  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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import xml.etree.ElementTree as ET
import hashlib
import time
import log
import re
import data_weixin
from session_mc import BaseHandler

import sys
reload(sys)
sys.setdefaultencoding('utf8')


patt_ms = [
        re.compile('美食'),
        re.compile('海鲜'),
        re.compile('小吃'),
        re.compile('吃'),
    ]


patt_kz = [
        re.compile('客栈'),
        re.compile('酒店'),
        re.compile('旅馆'),
        re.compile('住'),
    ]


patt_jd = [
        re.compile('景点'),
        re.compile('玩'),
    ]


patt_xc = [
        re.compile('行程'),
        re.compile('行'),
    ]


patt_jt = [
        re.compile('交通'),
    ]


def find( patt, p_str ) :
    res = None
    for one in patt :
        res = res or one.search( p_str )
    return res


class CgiHandler( BaseHandler ) :

    def get( self ) :
        #获取输入参数
        signature = self.get_argument( 'signature', default=None )
        timestamp = self.get_argument( 'timestamp', default=None )
        nonce = self.get_argument( 'nonce', default=None )
        echostr = self.get_argument( 'echostr', default=None )
        if not signature or not timestamp or not nonce or not echostr :
            return
        #自己的token
        token = data_weixin.weixin_token
        #字典序排序
        list=[token,timestamp,nonce]
        list.sort()
        sha1=hashlib.sha1()
        map(sha1.update,list)
        #sha1加密算法
        hashcode=sha1.hexdigest()
        #如果是来自微信的请求,则回复echostr
        if hashcode == signature :
            self.write( echostr )
            return
        else :
            return


    def post( self ) :
        # 从request中获取请求文本
        rawStr = self.request.body
        # 将文本进行解析,得到请求的数据
        log.i('weixin call : %s',rawStr)
        xml = ET.fromstring(rawStr)
        fromUser = xml.find('FromUserName').text
        toUser = xml.find('ToUserName').text
        createTime = xml.find('CreateTime').text
        msgType = xml.find('MsgType').text
        if msgType == 'text':
            content = str( xml.find('Content').text )
            if find( patt_ms, content ) :
                items = [
                        {
                            'title' : '海鲜------品种丰富 个头肥大 新鲜生猛',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdcgS5xEZia3yTv4ZaKKESZrU3qmgVpNcicTbYGv44va7U9tnJdrLXvTsQ/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=1&sn=bb77bfaec43128afb38b865bb2ceedb9#rd',
                        },
                        {
                            'title' : '特色菜------天然无污染的食材',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdOcnyOZQoWmxEYo1KocjEtWwOvs5c36nY2Gfgoicbl5YUFCaLdEGmesQ/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=2&sn=d6d0fa551ac22085be8304ab384acaee#rd',
                        },
                        {
                            'title' : '风味小吃------让人流连忘返的风味美食',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jd0JWDb9qmBxSHIuwPe0YVDibooqkWN3EpzPFxTTAQxKzpFmzgyBPyjcw/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=3&sn=eef0724571ea1b54a47052902bb770a9#rd',
                        },
                        {
                            'title' : '水果------丰满多汁 酸甜可口 欢迎品尝',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdwXnNyhoCyVlt13QGACias1TVudCJk5Z00986ubY8vj2mEPH66MdtmjQ/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=4&sn=50058344fcb3a3e5bf124989cc234a22#rd',
                        },
                        {
                            'title' : '糖水------品类多 味道好 餐后好零食',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jd35ickgBqo7bkvZInbyQGYgibTYrbv8GPLfwv0rEM9sjk8icdmCGtEvdKw/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=5&sn=aa464773b8e95a719df1d48ee5d693e4#rd',
                        },
                    ]
                res = self.render_string( 'weixin_reply_news.html',
                        p_fromUser = toUser,
                        p_toUser = fromUser,
                        p_createTime = int( time.time() ),
                        p_items = items,
                    )
                self.write( res )
                return
            if find( patt_kz, content ) :
                items = [
                        {
                            'title' : '波罗的海私人海景客栈',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaia0d0x0Th1xLB0Q4GfbAVtGcuF5k0NcxvVQLTMDibdR2Ap2QOM5U2ObUE12ibibRUgf7ZqU9SZyQjcag/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290407&idx=1&sn=cc7edb83784abfef3ccd1562f0a08c2a#rd',
                        },
                        {
                            'title' : '海驿国际青年旅舍',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaia0d0x0Th1xLB0Q4GfbAVtGyG7cY8D2hFZAr0ZqwRmK8gpe9S1e682WziaFhodPRIoARiaRR6VfTxPw/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290407&idx=2&sn=a29ea00c124b7b8216286fb4b55cb111#rd',
                        },
                        {
                            'title' : '第四城度假客栈',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaia0d0x0Th1xLB0Q4GfbAVtGt8kY5PB6Q19QkOTibjPCqA2BJJg7ZNicKckJ0tQ88Jpymv5SysWiakACw/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290407&idx=3&sn=37c006b582fb0e3847f25bad0b00dd33#rd',
                        },
                        {
                            'title' : '海澐间客栈',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaia0d0x0Th1xLB0Q4GfbAVtGwhsuH3iaic6b83f9moVILKzP4knHnoMbhATZsAKrPWlQ7OHiaElu31ia4Q/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290407&idx=4&sn=ca6dda83ab4bf4b5526240d171aa7c7f#rd',
                        },
                    ]
                res = self.render_string( 'weixin_reply_news.html',
                        p_fromUser = toUser,
                        p_toUser = fromUser,
                        p_createTime = int( time.time() ),
                        p_items = items,
                    )
                self.write( res )
                return
            if find( patt_jd, content ) :
                items = [
                        {
                            'title' : '五彩滩------最适合看日出的地方',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdib7biaYSTQGx7MHKg8Lgu2S7koCnOcYshoMPEKpPYgnjnWCMkjhg0eyg/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290271&idx=1&sn=1d3be3f8fb81a584f5a5cfb675227a97#rd',
                        },
                        {
                            'title' : '滴水丹屏------绝壁滴水如连珠垂挂',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jd8Kax8MvJYtL03LPMzDTnb6crvIIsDBmTqTN33CDy2ooQMtp3ldr4Kg/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290271&idx=2&sn=d5b362360abfc6c41298e0b0df0d1981#rd',
                        },
                        {
                            'title' : '斜阳岛------可观太阳斜照此岛全景',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdRxC4O3krduviaiasKdoWFgL2Zggj5oM6Yiaic5fAQA8tHLPNIwn7mickkXQ/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290271&idx=3&sn=fe7458e0a96279d26ca071077aebf9a2#rd',
                        },
                        {
                            'title' : '石螺口------适合看日落的好地方',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jd5xCqdb9BzECR8HMxQCR92p7qegurDOEm2UAibUM3rrnE3zWK3Y8QNVQ/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290271&idx=4&sn=8e5f902ef68403d9bba53b231e4685ae#rd',
                        },
                    ]
                res = self.render_string( 'weixin_reply_news.html',
                        p_fromUser = toUser,
                        p_toUser = fromUser,
                        p_createTime = int( time.time() ),
                        p_items = items,
                    )
                self.write( res )
                return
            if find( patt_xc, content ) :
                items = [
                        {
                            'title' : '涠洲岛之旅————行程篇',
                            'description' : '',
                            'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnlbLaIzvAAMXapMxf0NuHR2zEqzw44pAeWGOesd4iaIHRA19hFfg0d0g/0',
                            'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200279456&idx=1&sn=22040a2c5e3bf0e0c7429957e3325455#rd',
                        },
                    ]
                res = self.render_string( 'weixin_reply_news.html',
                        p_fromUser = toUser,
                        p_toUser = fromUser,
                        p_createTime = int( time.time() ),
                        p_items = items,
                    )
                self.write( res )
                return
            if find( patt_jt, content ) :
                items = [
                            {
                                'title' : '涠洲岛之旅————交通篇',
                                'description' : '',
                                'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnueqtvjdaoIO4kh1wIzA8xW07g7qShyp6Ba6MRwXxz7iapsialGDVWUNA/0',
                                'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200327738&idx=1&sn=fb5e0d38ce30aa5017dc20121d0eb61f#rd',
                            },
                    ]
                res = self.render_string( 'weixin_reply_news.html',
                        p_fromUser = toUser,
                        p_toUser = fromUser,
                        p_createTime = int( time.time() ),
                        p_items = items,
                    )
                self.write( res )
                return
            reply = '鹦鹉学舌 :'+content
            res = self.render_string( 'weixin_reply_text.html',
                    p_fromUser = toUser,
                    p_toUser = fromUser,
                    p_createTime = int( time.time() ),
                    p_reply = reply,
                )
            self.write( res )
            return
        if msgType == 'event' :
            event = xml.find('Event').text
            if event == 'subscribe' :
                reply = data_weixin.weixin_subscribe_text
                res = self.render_string( 'weixin_reply_text.html',
                        p_fromUser = toUser,
                        p_toUser = fromUser,
                        p_createTime = int( time.time() ),
                        p_reply = reply,
                    )
                self.write( res )
                return
            if event == 'unsubscribe' :
                self.write( '' )
                return
            if event == 'CLICK' :
                event_key = xml.find('EventKey').text
                if event_key == 'V_001_CLICK_ABOUT' :
                    items = [
                            {
                                'title' : '涠洲岛手绘,以及石墨设计团队',
                                'description' : '这是一个古老的小岛,和年轻的团队',
                                'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaia0d0x0Th1xLB0Q4GfbAVtG5ejnMYsAA7wibBJuzSRLsiaG0rfeM2YodGSnY3o8QuNoTAqbj1EPQL8A/0',
                                'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200289683&idx=1&sn=f012c2138fefbc4665b94f929e546520#rd',
                            },
                        ]
                    res = self.render_string( 'weixin_reply_news.html',
                            p_fromUser = toUser,
                            p_toUser = fromUser,
                            p_createTime = int( time.time() ),
                            p_items = items,
                        )
                    self.write( res )
                    return
                if event_key == 'V_001_CLICK_TIPS' :
                    items = [
                            {
                                'title' : '涠洲岛之旅————海鲜篇',
                                'description' : '',
                                'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdfhYsvJ2FoZS68Dpbb2qsJfQh03KgbWLZUHK8kGZF414tPmibB3XVAwA/0',
                                'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200321645&idx=1&sn=dbde8e6bf25ff6f0b2a00f9edc1fca78#rd',
                            },
                            {
                                'title' : '涠洲岛之旅————风景篇',
                                'description' : '',
                                'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnTfvMPOEibnAickxYowMemHI1WYpfC1XobD4zUou0rzQohrobGRcMzh4g/0',
                                'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200327693&idx=1&sn=6a4c42b2b8ffb4f5f4aba591c346252a#rd',
                            },
                            {
                                'title' : '涠洲岛之旅————住宿篇',
                                'description' : '',
                                'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdebCtQibMYPppoBMnYOnAHjgjpHGPf76nTa2lx5QhOhjX5Ke4unDIfmw/0',
                                'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200325138&idx=1&sn=78bef3cd5850f8e38033c9fc7737de8b#rd',
                            },
                            {
                                'title' : '涠洲岛之旅————交通篇',
                                'description' : '',
                                'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnueqtvjdaoIO4kh1wIzA8xW07g7qShyp6Ba6MRwXxz7iapsialGDVWUNA/0',
                                'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200327738&idx=1&sn=fb5e0d38ce30aa5017dc20121d0eb61f#rd',
                            },
                            {
                                'title' : '涠洲岛之旅————行程篇',
                                'description' : '',
                                'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnlbLaIzvAAMXapMxf0NuHR2zEqzw44pAeWGOesd4iaIHRA19hFfg0d0g/0',
                                'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200279456&idx=1&sn=22040a2c5e3bf0e0c7429957e3325455#rd',
                            },
                        ]
                    res = self.render_string( 'weixin_reply_news.html',
                            p_fromUser = toUser,
                            p_toUser = fromUser,
                            p_createTime = int( time.time() ),
                            p_items = items,
                        )
                    self.write( res )
                    return
        self.write( '' )
        return
Back to Top