diff --git a/app.py b/app.py index d016298..e7b6a2d 100644 --- a/app.py +++ b/app.py @@ -7,7 +7,9 @@ from flask_sqlalchemy import SQLAlchemy from flask_limiter import Limiter from flask_limiter.util import get_remote_address from mastodon import Mastodon +from apscheduler.schedulers.background import BackgroundScheduler import random +import atexit from config import C WRONG_ANS_HTML = ''' @@ -97,13 +99,26 @@ def choose_new_next(min_like_num=10): last_paragraph_id = story.tail next_one = Paragraph.query.filter_by(parent_id=last_paragraph_id, is_hidden=False)\ .order_by(Paragraph.like_num.desc()).first() + print(next_one) if next_one and next_one.like_num >= min_like_num: + print(next_one, next_one.like_num) + story.text += next_one.text story.total_like_num += next_one.like_num story.tail = next_one.id next_one.is_chosen = True - db.session.commit() + # 更新 story! + db.session.commit() + +scheduler = BackgroundScheduler() +for d in range(5, 15): + for m in range(0, 24 * 60, C.period): + scheduler.add_job(func=choose_new_next, trigger='date', run_date=datetime(2021, 11, d, m // 60, m % 60), args=[1] if C.debug else []) +scheduler.start() + +# Shut down the scheduler when exiting the app +atexit.register(lambda: scheduler.shutdown()) def sample_question(qs, n=3): @@ -198,6 +213,7 @@ def story(story_id): cs_login_url = MAST_LOGIN_URL guest_login_url = url_for('main_bp.guest_login') verify_questions = sample_question(C.verify_questions) + period = C.period return render_template('story.html', **locals()) @@ -226,7 +242,7 @@ def create(): @bp.route('/react', methods=['POST']) @login_required -@limiter.limit("4 / minute") +@limiter.limit("40 / minute") def react(): kind = request.form.get('kind', type=int) pid = request.form.get('pid', type=int) diff --git a/requirements.txt b/requirements.txt index b0a714a..87178a5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +APScheduler~=3.8.1 Flask~=2.0.2 Flask_Limiter~=1.4 Flask_SQLAlchemy~=2.5.1 diff --git a/templates/story.html b/templates/story.html index 51a4d68..2b20e80 100644 --- a/templates/story.html +++ b/templates/story.html @@ -45,11 +45,21 @@

{{p.text}}

{% endfor %} + +
+ +
-

备选后续

+

备选条目

{% if sort_by == 'like' %} 按时间 | 按赞数 @@ -200,6 +210,27 @@ $('.user-info .btn').click(check_login); $('textarea').focus(check_login); + + function makeTimer() { + let period = $("#timeleft").data('period') * 60; + + var now = new Date(); + now = (Date.parse(now) / 1000); + + var timeLeft = period - now % period; + + var hours = Math.floor(timeLeft / 3600); + var minutes = Math.floor((timeLeft - (hours * 3600 )) / 60); + var seconds = Math.floor((timeLeft - (hours * 3600) - (minutes * 60))); + + if (hours < "10") { hours = "0" + hours; } + if (minutes < "10") { minutes = "0" + minutes; } + if (seconds < "10") { seconds = "0" + seconds; } + + $("#timeleft").html(`${hours}:${minutes}:${seconds}`); + } + + setInterval(function() { makeTimer(); }, 1000);