2024-08-31 07:35:50 +08:00
|
|
|
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
|
|
|
|
|
2024-08-31 07:38:30 +08:00
|
|
|
apiService = Blueprint('site',__name__)
|
2024-08-31 07:35:50 +08:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
|
2024-08-31 07:38:30 +08:00
|
|
|
@apiService.route('/api/mywords') # HTTPie usage: http -A bearer -a secret-token http://127.0.0.1:5000/api/mywords
|
2024-08-31 07:35:50 +08:00
|
|
|
@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)
|
|
|
|
|