-
Notifications
You must be signed in to change notification settings - Fork 0
/
livetzc.py
166 lines (144 loc) · 4.76 KB
/
livetzc.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
#! /usr/bin/env python
# -*- coding:utf8 -*-
import time
from flask import Flask, g, request, make_response
import hashlib, urllib2, urllib, re, cookielib
import xml.etree.ElementTree as ET
import sae.storage
from datetime import date
import requests
import json
import config
import dateCounts,lrcSearch,renrenStatus,bus,movies, schoolCalendar,courses,music
import libraryBorrowed,librarySearch
tips = config.tips
error_msg = config.error_msg
app = Flask(__name__)
app.debug = True
@app.route('/', methods=['GET', 'POST'])
def wechat_auth():
if request.method == 'GET':
token = 'Add your token here'
query = request.args
signature = query.get('signature', '')
timestamp = query.get('timestamp', '')
nonce = query.get('nonce', '')
echostr = query.get('echostr', '')
s = [timestamp, nonce, token]
s.sort()
s = ''.join(s)
if ( hashlib.sha1(s).hexdigest() == signature ):
return make_response(echostr)
# Get the infomations from the recv_xml.
reply = config.reply
xiaoli_reply = config.xiaoli_reply
bus_reply = ''' '''
xml_recv = ET.fromstring(request.data)
ToUserName = xml_recv.find("ToUserName").text
FromUserName = xml_recv.find("FromUserName").text
Content = ''
try:
Content = xml_recv.find("Content").text
except:
pass
msgtype_test = ''
event_test = ''
try:
msgtype_test = xml_recv.find('MsgType').text
event_test = xml_recv.find('Event').text
except:
pass
if msgtype_test == 'event' and event_test == 'subscribe':
# 返回帮助信息
Content = tips
elif Content.lower().find('xiaoli') >= 0:
# 返回图文模式
# 校历
response = make_response(xiaoli_reply % (
FromUserName, ToUserName, str(int(time.time())), 'http://tzclife-pic.stor.sinaapp.com/xiaoli2015.jpg' ))
response.content_type = 'application/xml'
return response
elif Content.lower().find('help') >= 0:
# 返回帮助信息
Content = tips
elif Content.lower().find('bir') >= 0:
# 返回天数
Content = dateCounts.DateCounts(Content)
elif Content.lower().find('time') >= 0 :
# 查询校园时间表
Content = config.TimeTableSummer
elif Content.lower().find('movie') >= 0 :
# 查询学校电影信息
Content = config.movies
elif Content.lower().find('bus') >= 0 :
# 查询公交信息
Content = config.bus
elif Content.lower().find('status') >= 0 :
# 向人人网LiveTzc发布状态
Content = renrenStatus.RenrenStatus(Content)
elif Content.find('@') >= 0:
# 查询歌词 用的是百度音乐
Content = lrcSearch.LrcSearchRequests(Content)
elif Content.find('dt') >= 0:
# 查询上海公交卡还剩多少钱
sptccUrl = 'http://220.248.75.36/handapp/PGcardAmtServlet?arg1='
success = Content.split()
if len(success) == 2:
page = urllib2.urlopen(sptccUrl + success[1]).read()
page_f = page.find("'")
page_e = page.find("'", page_f + 1)
Content = '交通卡还剩下 ' + page[page_f + 1:page_e] + ' 元TAT'
else:
Content = 'SPTCC error...'
elif Content.find('car') >= 0:
# 查询违章信息
args = Content.split()
chepai = args[1]
cjh = args[2]
post_url = 'http://bsdt.jhga.gov.cn/egov/jdcwfcxAction.action'
post_data = dict(CPHM='浙G' + chepai, CJH=cjh, CPLB='02', fn='jdcwfcx', OPENID='oDQPZt6mijPGQMBv7cnAOOUBK8g8')
html = requests.post(post_url, post_data)
try:
datas = json.loads(html.content)
carid = len(datas)
infos = ''
for x in range(carid):
infos = infos + '\n\nwww.jhga.gov.cn/ajax/jxj_showimg.aspx?id='+str(datas[x]['ID'])
Content = html.content[:1500]+'\n'+'一共'+str(carid)+'个违章\n\n'+ infos[:460]
except:
Content = html.content[:2000]
elif Content.lower().find('cjhlist') >= 0:
# 违章列表
Content = config.cars
elif Content.lower().find('sptcc') >= 0:
# 查询上海公交卡还剩多少钱
sptccUrl = 'http://220.248.75.36/handapp/PGcardAmtServlet?arg1=49925366539'
page = urllib2.urlopen(sptccUrl).read()
page_f = page.find("'")
page_e = page.find("'", page_f + 1)
Content = '交通卡还剩下 ' + page[page_f + 1:page_e] + ' 元TAT'
elif Content.lower().find('lib') >= 0 :
# 图书馆相关信息查询
success = Content.split()
book_url = 'http://www2.lib.tzc.edu.cn/cgi-bin/IlaswebBib'
for i in range(len(success)):
success[i] = success[i].encode('utf8')
if len(success) == 2:
book_name = success[1]
Content = librarySearch.LibrarySearch(book_name)
elif len(success) == 3:
try:
Content = libraryBorrowed(success[1],success[2])
except:
Content = '图书馆服务器故障'
else:
Content = 'lib和书名之间要有个空格哦!!!'
elif len(Content.split()) == 2:
# 返回课表信息
Content = courses.Courses(Content)
else:
Content = error_msg
# 格式化
response = make_response(reply % (FromUserName, ToUserName, str(int(time.time())), Content ))
response.content_type = 'application/xml'
return response