1
|
|
|
#!/usr/bin/env python
|
2
|
|
|
# encoding: utf-8
|
3
|
|
|
from __future__ import absolute_import, unicode_literals
|
4
|
|
|
|
5
|
|
|
import os
|
6
|
|
|
from flask import Flask, request, render_template
|
7
|
|
|
from flask_wechatpy import Wechat, wechat_required
|
8
|
|
|
from wechatpy.replies import create_reply, EmptyReply, TextReply
|
9
|
|
|
from utils import assemble_schedule, assemble_score
|
10
|
|
|
from school import School
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
app = Flask(__name__)
|
14
|
|
|
app.config['WECHAT_APPID'] = os.getenv('WECHAT_APPID', '')
|
15
|
|
|
app.config['WECHAT_SECRET'] = os.getenv('WECHAT_SECRET', '')
|
16
|
|
|
app.config['WECHAT_TOKEN'] = os.getenv('WECHAT_TOKEN', '')
|
17
|
|
|
app.config['DEBUG'] = True
|
18
|
|
|
|
19
|
|
|
user = os.getenv('GDST_STUDENT_ACCOUNT', '')
|
20
|
|
|
password = os.getenv('GDST_STUDENT_PASSWD', '')
|
21
|
|
|
|
22
|
|
|
wechat = Wechat(app)
|
23
|
|
|
school = School(user, password)
|
24
|
|
|
|
25
|
|
|
|
26
|
|
|
@app.route('/test_mp/msg/callback', methods=['GET', 'POST'])
|
27
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
28
|
|
|
@wechat_required
|
29
|
|
|
def wechat_handler():
|
30
|
|
|
msg = request.wechat_msg
|
31
|
|
|
reply = EmptyReply()
|
32
|
|
|
if msg.type == 'text':
|
33
|
|
|
|
34
|
|
|
if msg.content == '课表':
|
35
|
|
|
schedule_year = '2015-2016'
|
36
|
|
|
schedule_term = '2'
|
37
|
|
|
schedule_data = school.schedule(schedule_year, schedule_term)
|
38
|
|
|
if type(schedule_data) == dict and schedule_data.get('error'):
|
39
|
|
|
reply = TextReply(content=schedule_data['error'], message=msg)
|
40
|
|
|
else:
|
41
|
|
|
data = assemble_schedule(schedule_data)
|
42
|
|
|
reply = create_reply(data, message=msg)
|
43
|
|
|
|
44
|
|
|
elif msg.content == '成绩':
|
45
|
|
|
score_year = '2015-2016'
|
46
|
|
|
score_term = '2'
|
47
|
|
|
score_data = school.score(score_year, score_term)
|
48
|
|
|
if type(score_data) == dict and score_data.get('error'):
|
49
|
|
|
reply = TextReply(content=schedule_data['error'], message=msg)
|
50
|
|
|
else:
|
51
|
|
|
data = assemble_score(score_year, score_term, score_data)
|
52
|
|
|
reply = create_reply(data, message=msg)
|
53
|
|
|
|
54
|
|
|
elif msg.content == '绑定':
|
55
|
|
|
content = "<a>点击绑定</a>"
|
56
|
|
|
reply = TextReply(content=content, message=msg)
|
57
|
|
|
|
58
|
|
|
else:
|
59
|
|
|
reply = TextReply(content=msg.content, message=msg)
|
60
|
|
|
|
61
|
|
|
return reply
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
@app.route('/get_schedule')
|
65
|
|
|
def get_schedule_page():
|
66
|
|
|
schedule_data = school.schedule
|
67
|
|
|
return render_template(
|
68
|
|
|
"schedule.html",
|
69
|
|
|
schedule_type=0, weeks=1,
|
70
|
|
|
schedule_info=schedule_data['schedule'])
|
71
|
|
|
|
72
|
|
|
|
73
|
|
|
@app.route('/access_token')
|
74
|
|
|
def access_token():
|
75
|
|
|
return "access token: {}".format(wechat.access_token)
|
76
|
|
|
|
77
|
|
|
|
78
|
|
|
if __name__ == '__main__':
|
79
|
|
|
app.run()
|
80
|
|
|
|