1
0
Fork 0
EnglishPal/app/api_bp.py

44 lines
1.2 KiB
Python
Raw Normal View History

import json
2023-05-06 19:08:45 +08:00
2023-05-16 14:46:57 +08:00
from flask import Blueprint, session
2023-05-06 19:08:45 +08:00
import pickle_idea2
path_prefix = '/var/www/wordfreq/wordfreq/'
path_prefix = './' # comment this line in deployment
# 创建api蓝图
api_blue = Blueprint('api', __name__, url_prefix='/api')
def helper(res, result):
for item in res:
if type(res[str(item)]) == 'dict':
helper(res[str(item)], result)
2023-05-06 19:08:45 +08:00
if type(res[str(item)]) == 'list':
for i in range(len(res[str(item)])):
helper(res[str(item)][i], result)
result.append(str(item))
return result
@api_blue.route('/json/<username>', methods=['GET'])
def api_bp(username):
2023-05-16 14:46:57 +08:00
# 获取session里的用户名,必须携带token
token = session.get("token")
if token == "70620F32A9DC965FCCF0447B674AA161":
result = []
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
s = pickle_idea2.load_record(user_freq_record)
wordlist = helper(s, result)
print(json.dumps(s))
results = {}
for word in wordlist:
results[word] = len(s[word])
return results
else:
print("无效的token")