PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app_shimosheji/weixin.py

https://github.com/rongoman/www-moai-toki
Python | 329 lines | 301 code | 19 blank | 9 comment | 18 complexity | 06e08d3f87dfcd3f403f7842a3c4af29 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import xml.etree.ElementTree as ET
  4. import hashlib
  5. import time
  6. import log
  7. import re
  8. import data_weixin
  9. from session_mc import BaseHandler
  10. import sys
  11. reload(sys)
  12. sys.setdefaultencoding('utf8')
  13. patt_ms = [
  14. re.compile('美食'),
  15. re.compile('海鲜'),
  16. re.compile('小吃'),
  17. re.compile('吃'),
  18. ]
  19. patt_kz = [
  20. re.compile('客栈'),
  21. re.compile('酒店'),
  22. re.compile('旅馆'),
  23. re.compile('住'),
  24. ]
  25. patt_jd = [
  26. re.compile('景点'),
  27. re.compile('玩'),
  28. ]
  29. patt_xc = [
  30. re.compile('行程'),
  31. re.compile('行'),
  32. ]
  33. patt_jt = [
  34. re.compile('交通'),
  35. ]
  36. def find( patt, p_str ) :
  37. res = None
  38. for one in patt :
  39. res = res or one.search( p_str )
  40. return res
  41. class CgiHandler( BaseHandler ) :
  42. def get( self ) :
  43. #获取输入参数
  44. signature = self.get_argument( 'signature', default=None )
  45. timestamp = self.get_argument( 'timestamp', default=None )
  46. nonce = self.get_argument( 'nonce', default=None )
  47. echostr = self.get_argument( 'echostr', default=None )
  48. if not signature or not timestamp or not nonce or not echostr :
  49. return
  50. #自己的token
  51. token = data_weixin.weixin_token
  52. #字典序排序
  53. list=[token,timestamp,nonce]
  54. list.sort()
  55. sha1=hashlib.sha1()
  56. map(sha1.update,list)
  57. #sha1加密算法
  58. hashcode=sha1.hexdigest()
  59. #如果是来自微信的请求则回复echostr
  60. if hashcode == signature :
  61. self.write( echostr )
  62. return
  63. else :
  64. return
  65. def post( self ) :
  66. # 从request中获取请求文本
  67. rawStr = self.request.body
  68. # 将文本进行解析,得到请求的数据
  69. log.i('weixin call : %s',rawStr)
  70. xml = ET.fromstring(rawStr)
  71. fromUser = xml.find('FromUserName').text
  72. toUser = xml.find('ToUserName').text
  73. createTime = xml.find('CreateTime').text
  74. msgType = xml.find('MsgType').text
  75. if msgType == 'text':
  76. content = str( xml.find('Content').text )
  77. if find( patt_ms, content ) :
  78. items = [
  79. {
  80. 'title' : '海鲜------品种丰富 个头肥大 新鲜生猛',
  81. 'description' : '',
  82. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdcgS5xEZia3yTv4ZaKKESZrU3qmgVpNcicTbYGv44va7U9tnJdrLXvTsQ/0',
  83. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=1&sn=bb77bfaec43128afb38b865bb2ceedb9#rd',
  84. },
  85. {
  86. 'title' : '特色菜------天然无污染的食材',
  87. 'description' : '',
  88. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdOcnyOZQoWmxEYo1KocjEtWwOvs5c36nY2Gfgoicbl5YUFCaLdEGmesQ/0',
  89. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=2&sn=d6d0fa551ac22085be8304ab384acaee#rd',
  90. },
  91. {
  92. 'title' : '风味小吃------让人流连忘返的风味美食',
  93. 'description' : '',
  94. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jd0JWDb9qmBxSHIuwPe0YVDibooqkWN3EpzPFxTTAQxKzpFmzgyBPyjcw/0',
  95. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=3&sn=eef0724571ea1b54a47052902bb770a9#rd',
  96. },
  97. {
  98. 'title' : '水果------丰满多汁 酸甜可口 欢迎品尝',
  99. 'description' : '',
  100. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdwXnNyhoCyVlt13QGACias1TVudCJk5Z00986ubY8vj2mEPH66MdtmjQ/0',
  101. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=4&sn=50058344fcb3a3e5bf124989cc234a22#rd',
  102. },
  103. {
  104. 'title' : '糖水------品类多 味道好 餐后好零食',
  105. 'description' : '',
  106. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jd35ickgBqo7bkvZInbyQGYgibTYrbv8GPLfwv0rEM9sjk8icdmCGtEvdKw/0',
  107. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=5&sn=aa464773b8e95a719df1d48ee5d693e4#rd',
  108. },
  109. ]
  110. res = self.render_string( 'weixin_reply_news.html',
  111. p_fromUser = toUser,
  112. p_toUser = fromUser,
  113. p_createTime = int( time.time() ),
  114. p_items = items,
  115. )
  116. self.write( res )
  117. return
  118. if find( patt_kz, content ) :
  119. items = [
  120. {
  121. 'title' : '波罗的海私人海景客栈',
  122. 'description' : '',
  123. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaia0d0x0Th1xLB0Q4GfbAVtGcuF5k0NcxvVQLTMDibdR2Ap2QOM5U2ObUE12ibibRUgf7ZqU9SZyQjcag/0',
  124. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290407&idx=1&sn=cc7edb83784abfef3ccd1562f0a08c2a#rd',
  125. },
  126. {
  127. 'title' : '海驿国际青年旅舍',
  128. 'description' : '',
  129. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaia0d0x0Th1xLB0Q4GfbAVtGyG7cY8D2hFZAr0ZqwRmK8gpe9S1e682WziaFhodPRIoARiaRR6VfTxPw/0',
  130. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290407&idx=2&sn=a29ea00c124b7b8216286fb4b55cb111#rd',
  131. },
  132. {
  133. 'title' : '第四城度假客栈',
  134. 'description' : '',
  135. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaia0d0x0Th1xLB0Q4GfbAVtGt8kY5PB6Q19QkOTibjPCqA2BJJg7ZNicKckJ0tQ88Jpymv5SysWiakACw/0',
  136. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290407&idx=3&sn=37c006b582fb0e3847f25bad0b00dd33#rd',
  137. },
  138. {
  139. 'title' : '海澐间客栈',
  140. 'description' : '',
  141. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaia0d0x0Th1xLB0Q4GfbAVtGwhsuH3iaic6b83f9moVILKzP4knHnoMbhATZsAKrPWlQ7OHiaElu31ia4Q/0',
  142. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290407&idx=4&sn=ca6dda83ab4bf4b5526240d171aa7c7f#rd',
  143. },
  144. ]
  145. res = self.render_string( 'weixin_reply_news.html',
  146. p_fromUser = toUser,
  147. p_toUser = fromUser,
  148. p_createTime = int( time.time() ),
  149. p_items = items,
  150. )
  151. self.write( res )
  152. return
  153. if find( patt_jd, content ) :
  154. items = [
  155. {
  156. 'title' : '五彩滩------最适合看日出的地方',
  157. 'description' : '',
  158. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdib7biaYSTQGx7MHKg8Lgu2S7koCnOcYshoMPEKpPYgnjnWCMkjhg0eyg/0',
  159. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290271&idx=1&sn=1d3be3f8fb81a584f5a5cfb675227a97#rd',
  160. },
  161. {
  162. 'title' : '滴水丹屏------绝壁滴水如连珠垂挂',
  163. 'description' : '',
  164. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jd8Kax8MvJYtL03LPMzDTnb6crvIIsDBmTqTN33CDy2ooQMtp3ldr4Kg/0',
  165. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290271&idx=2&sn=d5b362360abfc6c41298e0b0df0d1981#rd',
  166. },
  167. {
  168. 'title' : '斜阳岛------可观太阳斜照此岛全景',
  169. 'description' : '',
  170. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdRxC4O3krduviaiasKdoWFgL2Zggj5oM6Yiaic5fAQA8tHLPNIwn7mickkXQ/0',
  171. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290271&idx=3&sn=fe7458e0a96279d26ca071077aebf9a2#rd',
  172. },
  173. {
  174. 'title' : '石螺口------适合看日落的好地方',
  175. 'description' : '',
  176. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jd5xCqdb9BzECR8HMxQCR92p7qegurDOEm2UAibUM3rrnE3zWK3Y8QNVQ/0',
  177. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290271&idx=4&sn=8e5f902ef68403d9bba53b231e4685ae#rd',
  178. },
  179. ]
  180. res = self.render_string( 'weixin_reply_news.html',
  181. p_fromUser = toUser,
  182. p_toUser = fromUser,
  183. p_createTime = int( time.time() ),
  184. p_items = items,
  185. )
  186. self.write( res )
  187. return
  188. if find( patt_xc, content ) :
  189. items = [
  190. {
  191. 'title' : '涠洲岛之旅————行程篇',
  192. 'description' : '',
  193. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnlbLaIzvAAMXapMxf0NuHR2zEqzw44pAeWGOesd4iaIHRA19hFfg0d0g/0',
  194. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200279456&idx=1&sn=22040a2c5e3bf0e0c7429957e3325455#rd',
  195. },
  196. ]
  197. res = self.render_string( 'weixin_reply_news.html',
  198. p_fromUser = toUser,
  199. p_toUser = fromUser,
  200. p_createTime = int( time.time() ),
  201. p_items = items,
  202. )
  203. self.write( res )
  204. return
  205. if find( patt_jt, content ) :
  206. items = [
  207. {
  208. 'title' : '涠洲岛之旅————交通篇',
  209. 'description' : '',
  210. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnueqtvjdaoIO4kh1wIzA8xW07g7qShyp6Ba6MRwXxz7iapsialGDVWUNA/0',
  211. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200327738&idx=1&sn=fb5e0d38ce30aa5017dc20121d0eb61f#rd',
  212. },
  213. ]
  214. res = self.render_string( 'weixin_reply_news.html',
  215. p_fromUser = toUser,
  216. p_toUser = fromUser,
  217. p_createTime = int( time.time() ),
  218. p_items = items,
  219. )
  220. self.write( res )
  221. return
  222. reply = '鹦鹉学舌 :'+content
  223. res = self.render_string( 'weixin_reply_text.html',
  224. p_fromUser = toUser,
  225. p_toUser = fromUser,
  226. p_createTime = int( time.time() ),
  227. p_reply = reply,
  228. )
  229. self.write( res )
  230. return
  231. if msgType == 'event' :
  232. event = xml.find('Event').text
  233. if event == 'subscribe' :
  234. reply = data_weixin.weixin_subscribe_text
  235. res = self.render_string( 'weixin_reply_text.html',
  236. p_fromUser = toUser,
  237. p_toUser = fromUser,
  238. p_createTime = int( time.time() ),
  239. p_reply = reply,
  240. )
  241. self.write( res )
  242. return
  243. if event == 'unsubscribe' :
  244. self.write( '' )
  245. return
  246. if event == 'CLICK' :
  247. event_key = xml.find('EventKey').text
  248. if event_key == 'V_001_CLICK_ABOUT' :
  249. items = [
  250. {
  251. 'title' : '涠洲岛手绘,以及石墨设计团队',
  252. 'description' : '这是一个古老的小岛,和年轻的团队',
  253. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaia0d0x0Th1xLB0Q4GfbAVtG5ejnMYsAA7wibBJuzSRLsiaG0rfeM2YodGSnY3o8QuNoTAqbj1EPQL8A/0',
  254. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200289683&idx=1&sn=f012c2138fefbc4665b94f929e546520#rd',
  255. },
  256. ]
  257. res = self.render_string( 'weixin_reply_news.html',
  258. p_fromUser = toUser,
  259. p_toUser = fromUser,
  260. p_createTime = int( time.time() ),
  261. p_items = items,
  262. )
  263. self.write( res )
  264. return
  265. if event_key == 'V_001_CLICK_TIPS' :
  266. items = [
  267. {
  268. 'title' : '涠洲岛之旅————海鲜篇',
  269. 'description' : '',
  270. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdfhYsvJ2FoZS68Dpbb2qsJfQh03KgbWLZUHK8kGZF414tPmibB3XVAwA/0',
  271. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200321645&idx=1&sn=dbde8e6bf25ff6f0b2a00f9edc1fca78#rd',
  272. },
  273. {
  274. 'title' : '涠洲岛之旅————风景篇',
  275. 'description' : '',
  276. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnTfvMPOEibnAickxYowMemHI1WYpfC1XobD4zUou0rzQohrobGRcMzh4g/0',
  277. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200327693&idx=1&sn=6a4c42b2b8ffb4f5f4aba591c346252a#rd',
  278. },
  279. {
  280. 'title' : '涠洲岛之旅————住宿篇',
  281. 'description' : '',
  282. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdebCtQibMYPppoBMnYOnAHjgjpHGPf76nTa2lx5QhOhjX5Ke4unDIfmw/0',
  283. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200325138&idx=1&sn=78bef3cd5850f8e38033c9fc7737de8b#rd',
  284. },
  285. {
  286. 'title' : '涠洲岛之旅————交通篇',
  287. 'description' : '',
  288. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnueqtvjdaoIO4kh1wIzA8xW07g7qShyp6Ba6MRwXxz7iapsialGDVWUNA/0',
  289. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200327738&idx=1&sn=fb5e0d38ce30aa5017dc20121d0eb61f#rd',
  290. },
  291. {
  292. 'title' : '涠洲岛之旅————行程篇',
  293. 'description' : '',
  294. 'picurl' : 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnlbLaIzvAAMXapMxf0NuHR2zEqzw44pAeWGOesd4iaIHRA19hFfg0d0g/0',
  295. 'url' : 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200279456&idx=1&sn=22040a2c5e3bf0e0c7429957e3325455#rd',
  296. },
  297. ]
  298. res = self.render_string( 'weixin_reply_news.html',
  299. p_fromUser = toUser,
  300. p_toUser = fromUser,
  301. p_createTime = int( time.time() ),
  302. p_items = items,
  303. )
  304. self.write( res )
  305. return
  306. self.write( '' )
  307. return