#! /usr/bin/python3
# -*- coding: utf-8 -*-
###########################################################################
# Copyright 2019 (C) Hui Lan According to your word list, your level is %4.2f and we have chosen an article with a difficulty level of %4.2f for you. %s %s %s %s English Pal - Learn English in a smart way!'
if session.get('logged_in'):
page += ' %s
')
s += '%s' % (get_answer_part(d['question']))
return s
def appears_in_test(word, d):
if not word in d:
return ''
else:
return ','.join(d[word])
def get_time():
return datetime.now().strftime('%Y%m%d%H%M') # upper to minutes
def get_question_part(s):
s = s.strip()
result = []
flag = 0
for line in s.split('\n'):
line = line.strip()
if line == 'QUESTION':
result.append(line)
flag = 1
elif line == 'ANSWER':
flag = 0
elif flag == 1:
result.append(line)
return '\n'.join(result)
def get_answer_part(s):
s = s.strip()
result = []
flag = 0
for line in s.split('\n'):
line = line.strip()
if line == 'ANSWER':
flag = 1
elif flag == 1:
result.append(line)
# https://css-tricks.com/snippets/javascript/showhide-element/
js = '''
'''
html_code = js
html_code += '\n'
html_code += '\n'
html_code += ' \n' % ('\n'.join(result))
return html_code
@app.route("/mark", methods=['GET', 'POST'])
def mark_word():
if request.method == 'POST':
d = load_freq_history(path_prefix + 'static/frequency/frequency.p')
lst_history = pickle_idea.dict2lst(d)
lst = []
for word in request.form.getlist('marked'):
lst.append((word, 1))
d = pickle_idea.merge_frequency(lst, lst_history)
pickle_idea.save_frequency_to_pickle(d, path_prefix + 'static/frequency/frequency.p')
return redirect(url_for('mainpage'))
else:
return 'Under construction'
@app.route("/", methods=['GET', 'POST'])
def mainpage():
if request.method == 'POST': # when we submit a form
content = request.form['content']
f = WordFreq(content)
lst = f.get_freq()
page = '\n'
# save history
d = load_freq_history(path_prefix + 'static/frequency/frequency.p')
lst_history = pickle_idea.dict2lst(d)
d = pickle_idea.merge_frequency(lst, lst_history)
pickle_idea.save_frequency_to_pickle(d, path_prefix + 'static/frequency/frequency.p')
return page
elif request.method == 'GET': # when we load a html page
page = '''
%s
' % (get_random_ads()) page += '粘帖1篇文章 (English only)
' page += '' d = load_freq_history(path_prefix + 'static/frequency/frequency.p') if len(d) > 0: page += '最常见的词
' for x in sort_in_descending_order(pickle_idea.dict2lst(d)): if x[1] <= 99: break page += '%s %d\n' % (youdao_link(x[0]), x[0], x[1]) page += '' return page @app.route("/请先登录。
' user_expiry_date = session.get('expiry_date') if datetime.now().strftime('%Y%m%d') > user_expiry_date: return '账号 %s 过期。
为了提高服务质量,English Pal 收取会员费用, 每天1元。 请决定你要试用的时间长度,扫描下面支付宝二维码支付。 支付时请注明English Pal Membership Fee。 我们会于12小时内激活账号。
如果有问题,请加开发者微信 torontohui
' % (username) username = session.get('username') user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username) if request.method == 'POST': # when we submit a form content = request.form['content'] f = WordFreq(content) lst = f.get_freq() page = '勾选不认识的单词
' page += '\n' return page elif request.method == 'GET': # when we load a html page page = '' page += 'English Pal for %s 登出
' % (username) page += '阅读文章并回答问题
\n' page += '%s' % (get_today_article(user_freq_record)) page += '收集生词吧
' page += '\n' d = load_freq_history(user_freq_record) if len(d) > 0: page += '我的生词簿
' lst = pickle_idea2.dict2lst(d) lst2 = [] for t in lst: lst2.append((t[0], len(t[1]))) for x in sort_in_descending_order(lst2): word = x[0] freq = x[1] if isinstance(d[word], list): # d[word] is a list of dates if freq > 1: page += '\n' % (youdao_link(word), word, '; '.join(d[word]), freq) else: page += '\n' % (youdao_link(word), word, '; '.join(d[word]), freq) elif isinstance(d[word], int): # d[word] is a frequency. to migrate from old format. page += '%s%d\n' % (youdao_link(word), word, freq) return page ### Sign-up, login, logout ### @app.route("/signup", methods=['GET', 'POST']) def signup(): if request.method == 'GET': return render_template('signup.html') elif request.method == 'POST': username = request.form['username'] password = request.form['password'] available = check_username_availability(username) if not available: flash('用户名 %s 已经被注册。' % (username)) return render_template('signup.html') elif len(password.strip()) < 4: return '密码过于简单。' else: add_user(username, password) verified = verify_user(username, password) if verified: session['logged_in'] = True session[username] = username session['username'] = username return '恭喜,你已成功注册, 你的用户名是 %s。 请记下用户名与密码。
为了提高服务质量,我们收取会员费,每天1元。 头100名用户享8折优惠。 请决定使用时间长度后,请扫描下面支付宝二维码支付。 未及时成为会员的账号,12小时后停用。如果有问题,请加开发者微信 torontohui。
\