Simplify wordCMD.py and rename it to api_service.py

SPM2023S-QianJunQi
Lan Hui 2024-08-31 07:35:50 +08:00
parent 8f31b030ea
commit ceb5f14ee9
3 changed files with 33 additions and 40 deletions

31
app/api_service.py Normal file
View File

@ -0,0 +1,31 @@
from flask import *
from flask_httpauth import HTTPTokenAuth
from Article import load_freq_history
path_prefix = '/var/www/wordfreq/wordfreq/'
path_prefix = './' # comment this line in deployment
show_bp = Blueprint('site',__name__)
auth = HTTPTokenAuth(scheme='Bearer')
tokens = {
"token": "token",
"secret-token": "lanhui" # token, username
}
@auth.verify_token
def verify_token(token):
if token in tokens:
return tokens[token]
@show_bp.route('/api/mywords') # HTTPie usage: http -A bearer -a secret-token http://127.0.0.1:5000/api/mywords
@auth.login_required
def show():
username = auth.current_user()
word_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
d = load_freq_history(word_freq_record)
return jsonify(d)

View File

@ -10,9 +10,10 @@ import Yaml
from user_service import userService from user_service import userService
from account_service import accountService from account_service import accountService
from admin_service import adminService, ADMIN_NAME from admin_service import adminService, ADMIN_NAME
from api_service import show_bp
import os import os
from translate import * from translate import *
from wordCMD import show_bp
app = Flask(__name__) app = Flask(__name__)
app.secret_key = os.urandom(32) app.secret_key = os.urandom(32)

View File

@ -1,39 +0,0 @@
from flask import *
from flask_httpauth import HTTPTokenAuth
from Article import load_freq_history
from wordfreqCMD import sort_in_descending_order
import pickle_idea
auth = HTTPTokenAuth(scheme='Bearer')
path_prefix = '/var/www/wordfreq/wordfreq/'
path_prefix = './' # comment this line in deployment
show_bp = Blueprint(
'site',
__name__,
)
tokens = {
"token": "token"
}
@auth.verify_token
def verify_token(token):
if token in tokens:
return tokens[token]
@show_bp.route('/show/<name>/') # set route for show page <name> means the var name to search
@auth.login_required
def show(name):
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (name)
d = load_freq_history(user_freq_record)
freqlst = sort_in_descending_order(pickle_idea.dict2lst(d))
words_freq = [] # 存储单词表的数组,格式为 单词-词频
for i in range(len(freqlst)):
words_freq.append(str(freqlst[i][0]) + "-" + str(len(freqlst[i][1])))
t = {}
t[name] = words_freq
return jsonify(t)