添加一个web API,显示当前用户的所有生词,以及添加某生词的总次数

鲁枭晨 2022-06-13 17:16:58 +08:00
parent e48008550a
commit 868fed795d
2 changed files with 46 additions and 0 deletions

44
app/api_bp.py Normal file
View File

@ -0,0 +1,44 @@
import json
from flask import Blueprint
from wordfreqCMD import *
from Article import get_today_article, load_freq_history
import pickle_idea
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)
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):
# 获取session里的用户名
result = []
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
s = pickle_idea2.load_record(user_freq_record)
wordlist = helper(s,result)
results = "{"
i=0
for word in wordlist:
if i!=len(wordlist)-1:
results +='"' + word +'":' + str(len(s[word])) +","
i=i+1
else:
results += '"' + word + '":' + str(len(s[word]))
results+="}"
return results

View File

@ -11,12 +11,14 @@ from Article import *
import Yaml
from user_service import userService
from account_service import accountService
from api_bp import api_blue
app = Flask(__name__)
app.secret_key = 'lunch.time!'
# 将蓝图注册到Lab app
app.register_blueprint(userService)
app.register_blueprint(accountService)
app.register_blueprint(api_blue)
path_prefix = '/var/www/wordfreq/wordfreq/'
path_prefix = './' # comment this line in deployment