-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
192 lines (171 loc) · 6.28 KB
/
app.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
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
from dao.fund import Fund
from dao.user import User
# from scheduler import MyScheduler
from bots.wxworkbot import WxWorkBot
from bots.telebot import TeleBot
from utils import DB
import services
import asyncio
from flask import Flask, request, render_template, session
import json
import logging
app = Flask(__name__)
logger = logging.getLogger('flask.app')
loop = asyncio.get_event_loop()
db = DB('sqlite3', './data/my_fund.db')
@app.route('/', methods=['GET', 'POST'])
def home():
return render_template('home.html')
@app.route('/publish_fund_image/<int:user_id>', methods=['POST'])
def publish_user_funds(user_id):
user = None
try:
user = next(db.select_data('select * from users where id = %d' % (user_id)))
except StopIteration:
return {'status' : 'error', 'message' : '没有id为%d的用户' % (user_id)}
else:
if user == None:
return {'status' : 'error', 'message' : '没有该用户' % (user_id)}
funds = db.select_data('select * from funds where user_id = %d;' % (user_id))
bot = services.get_bot(user.get('bot_id'), user.get('chat_id'))
for fund in funds:
f = Fund(fund.get('id'))
res = services.send_fund_image(bot, f)
return res.text
@app.route('/publish_fund_image/<name>', methods=['POST'])
def publish_fund_image_by_name(name):
user = None
try:
user = next(db.select_data('select * from users where name = "%s"' % (name)))
except StopIteration:
return '没有用户名为%s的用户' % (name)
else:
if user == None:
return '没有用户名为%s的用户' % (name)
funds = db.select_data('select * from funds where user_id = %d;' % (user.get('id')))
bot = services.get_bot(user.get('bot_id'), user.get('chat_id'))
for fund in funds:
f = Fund(fund.get('id'))
res = services.send_fund_image(bot, f)
return res.text
@app.route('/publish_fund_image/async/<name>', methods=['POST'])
def async_publish_fund_image_by_name(name):
user = None
try:
user = next(db.select_data('select * from users where name = "%s"' % (name)))
except StopIteration:
return '没有用户名为%s的用户' % (name)
else:
if user == None:
return '没有用户名为%s的用户' % (name)
funds = db.select_data('select * from funds where user_id = %d;' % (user.get('id')))
bot = services.get_bot(user.get('bot_id'), user.get('chat_id'))
tasks = [services.async_send_image(bot, Fund(f.get('id'))) for f in funds]
group = asyncio.gather(*tasks, loop=loop)
res = loop.run_until_complete(group)
return str(res)
@app.route('/publish_fund_image/async', methods=['POST'])
def publish_fund_image_async():
'''
向所有的bot发送基金曲线
通过库中查询后提交
因为基金图片要请求网络,所以通过基金来遍历
'''
res = db.select_data('select f.id, f.user_id, u.name, u.bot_id, u.chat_id from funds f, users u where f.user_id = u.id;')
'''
构造bot字典:{
bot_id-chat_id : Bot对象
}
'''
funds = generate_fund_userlist(res)
bots = dict()
tasks = []
print(funds)
for fund_id, user_list in funds.items():
f = Fund(fund_id)
for user in user_list:
'''
查找bots字典
'''
bid, cid = user.bot_id, user.chat_id
bot_key = '%s-%s' % (bid, cid)
bot = bots.get(bot_key)
if bot == None:
bot = bots[bot_key] = services.get_bot(bid, cid)
tasks.append(services.async_send_image(bot, f))
group = asyncio.gather(*tasks, loop=loop)
res = loop.run_until_complete(group)
return str(res)
@app.route('/publish_fund_image', methods=['POST'])
def publish_fund_image():
'''
向所有的bot发送基金曲线
通过库中查询后提交
因为基金图片要请求网络,所以通过基金来遍历
'''
res = db.select_data('select f.id, f.user_id, u.name, u.bot_id, u.chat_id from funds f, users u where f.user_id = u.id;')
'''
构造bot字典:{
bot_id-chat_id : Bot对象
}
'''
funds = generate_fund_userlist(res)
bots = dict()
print(funds)
for fund_id, user_list in funds.items():
f = Fund(fund_id)
for user in user_list:
'''
查找bots字典
'''
bid, cid = user.bot_id, user.chat_id
bot_key = '%s-%s' % (bid, cid)
bot = bots.get(bot_key)
if bot == None:
bot = bots[bot_key] = services.get_bot(bid, cid)
services.send_fund_image(bot, f)
return '成功'
@app.route('/users', methods=['GET'])
def get_users():
users = list(db.select_data('select * from users'))
return str([(u.get('id'), u.get('name')) for u in users])
@app.route('/users/<int:user_id>/funds', methods=['GET'])
def get_user_funds(user_id):
res = list(db.select_data('select * from funds where user_id=%d;' % (user_id)))
return str([f.get('id') for f in res])
@app.route('/users/<int:user_id>/funds', methods=['POST'])
def add_user_funds(user_id):
'TODO'
data = json.loads(request.get_data(as_text=True))
return json.dumps([db.insert_data('funds', d) for d in data])
@app.route('/users/<int:user_id>/funds/<fund_id>', methods=['POST'])
def add_user_fund(user_id, fund_id):
return db.insert_data('funds', {'id': fund_id, 'user_id': user_id})
@app.route('/users/<int:user_id>/funds/<fund_id>', methods=['DELETE'])
def del_user_fund(user_id, fund_id):
return db.delete_data('funds', {'id': fund_id, 'user_id': user_id})
@app.route('/users/<int:user_id>/funds', methods=['DELETE'])
def del_user_funds(user_id):
data = json.loads(request.get_data(as_text=True))
return [db.delete_data('funds', d) for d in data]
def generate_fund_userlist(data):
'''
将基金表数据整合为:{
基金ID1 : [{用户对象}],
基金ID2 : [{用户对象}]
}
'''
fund_user = {}
for r in data:
fid = r.get('id')
r['id'] = r.get('user_id')
if fund_user.get(fid) != None:
fund_user[fid].append(User(r))
else:
fund_user[fid] = [User(r)]
return fund_user
def __init__():
db.execute_sql('./db.sql')
if __name__ == "__main__":
__init__()
app.run(host='0.0.0.0', port=5000)