Browse Source

schedule

pull/1/head
namasikanam 3 years ago
parent
commit
6bd93e1e6b
3 changed files with 51 additions and 3 deletions
  1. +18
    -2
      app.py
  2. +1
    -0
      requirements.txt
  3. +32
    -1
      templates/story.html

+ 18
- 2
app.py View File

@ -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 = '''<html>
<head>
@ -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)

+ 1
- 0
requirements.txt View File

@ -1,3 +1,4 @@
APScheduler~=3.8.1
Flask~=2.0.2
Flask_Limiter~=1.4
Flask_SQLAlchemy~=2.5.1

+ 32
- 1
templates/story.html View File

@ -45,11 +45,21 @@
<p class="story-text" title="{{p.author}}, {{p.create_at}}, {{p.like_num}}">{{p.text}}</p>
{% endfor %}
</div>
<div style="font-size:90%;">
<ul>
<li>我们将每六小时划分为一个时段,每个时段结束后,该时段获得赞同票数最高(且不小于 10)的条目将入选,成为这个角色最终故事的一部分。</li>
<li>具体时段为:6:00-12:00, 12:00-18:00,18:00-24:00,0:00-6:00</li>
<li>当前时段剩余时间:<span id="timeleft" data-period="{{period}}"></span></li>
<li>参与创作需要回答几个简单的问题以验证学生身份,或者通过<a href="https://thu.closed.social/">闭社清华站</a>账号登录。</li>
<li>如需删除条目,请联系工作人员(微信:ordinary_thuer)。</li>
</ul>
</div>
</div>
<div class="part2" id="part2">
<div class="paragraph-list twin front qbox part-box" style="border: 0">
<h1 style="margin: -8px -5px 20px">备选后续</h1>
<h1 style="margin: -8px -5px 20px">备选条目</h1>
<span style="margin:8px" class="sort-by">
{% if sort_by == 'like' %}
<a href="?sort_by=time#part2">按时间</a> | <b>按赞数</b>
@ -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);
</script>
</html>

Loading…
Cancel
Save