From f978df8580a4f988b851697aa9e8a3a0e4463d44 Mon Sep 17 00:00:00 2001 From: Aevann Date: Fri, 13 Oct 2023 00:09:38 +0300 Subject: [PATCH] automate more aspects of mn + allow scheduling orgies --- files/assets/{subtitles.vtt => 1.vtt} | 0 files/classes/orgy.py | 23 +++++++++---- files/helpers/config/modaction_types.py | 2 +- files/helpers/cron.py | 36 +++----------------- files/routes/admin.py | 28 +++++++++------ files/routes/chat.py | 8 +---- files/templates/admin/admin_home.html | 2 +- files/templates/admin/orgy_control.html | 18 +++++++--- files/templates/orgy.html | 7 ++-- migrations/20231012-more-orgy-automation.sql | 3 ++ 10 files changed, 63 insertions(+), 64 deletions(-) rename files/assets/{subtitles.vtt => 1.vtt} (100%) create mode 100644 migrations/20231012-more-orgy-automation.sql diff --git a/files/assets/subtitles.vtt b/files/assets/1.vtt similarity index 100% rename from files/assets/subtitles.vtt rename to files/assets/1.vtt diff --git a/files/classes/orgy.py b/files/classes/orgy.py index a11169f1b6..0126a241ae 100644 --- a/files/classes/orgy.py +++ b/files/classes/orgy.py @@ -1,5 +1,6 @@ import time from flask import g, abort +import requests from sqlalchemy import Column, or_ from sqlalchemy.sql.sqltypes import * @@ -7,6 +8,7 @@ from sqlalchemy.sql.sqltypes import * from files.classes import Base from files.helpers.lazy import lazy +from files.helpers.config.const import * class Orgy(Base): __tablename__ = "orgies" @@ -15,7 +17,9 @@ class Orgy(Base): data = Column(String) title = Column(String) created_utc = Column(Integer) + start_utc = Column(Integer) end_utc = Column(Integer) + started = Column(Boolean, default=False) def __init__(self, *args, **kwargs): if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time()) @@ -26,8 +30,8 @@ class Orgy(Base): @property @lazy - def real_created_utc(self): - t = self.created_utc + def real_start_utc(self): + t = self.start_utc if int(time.time()) - t > 3000: t += 303 return t @@ -35,10 +39,17 @@ class Orgy(Base): def get_orgy(v): if not (v and v.allowed_in_chat): return None - orgy = g.db.query(Orgy).one_or_none() + expired_orgies = g.db.query(Orgy).filter(Orgy.end_utc != None, Orgy.end_utc < time.time()).all() + for x in expired_orgies: + g.db.delete(x) - if orgy and orgy.end_utc and orgy.end_utc < time.time(): - g.db.delete(orgy) - return None + if expired_orgies: + requests.post('http://localhost:5001/refresh_chat', headers={"Host": SITE}) + + orgy = g.db.query(Orgy).filter(Orgy.start_utc < time.time()).order_by(Orgy.start_utc).first() + if orgy and not orgy.started: + orgy.started = True + g.db.add(orgy) + requests.post('http://localhost:5001/refresh_chat', headers={"Host": SITE}) return orgy diff --git a/files/helpers/config/modaction_types.py b/files/helpers/config/modaction_types.py index 0acaa8e346..42588c09cb 100644 --- a/files/helpers/config/modaction_types.py +++ b/files/helpers/config/modaction_types.py @@ -306,7 +306,7 @@ MODACTION_TYPES = { "icon": 'fa-eye-slash', "color": 'bg-danger' }, - 'start_orgy': { + 'schedule_orgy': { "str": 'started orgy', "icon": 'fa-tv', "color": 'bg-success' diff --git a/files/helpers/cron.py b/files/helpers/cron.py index beb164dac3..182012f000 100644 --- a/files/helpers/cron.py +++ b/files/helpers/cron.py @@ -88,7 +88,7 @@ def cron_fn(every_5m, every_1d, every_fri_12, every_fri_23, every_sat_00, every_ if every_fri_12: - _create_post(f'Movie Night', f'''Our Movie Night today will show `{get_name()}`.\nThe movies will start at 8 PM EST. [Here is a timezone converter for whoever needs it.](https://dateful.com/time-zone-converter?t=8pm&tz1=EST-EDT-Eastern-Time). You can also check this [countdown timer](https://www.tickcounter.com/countdown/4435809/movie-night) instead.\nThey will be shown [here](/chat).\nThere will be a 5-minute bathroom break at the 50:00 mark.\nRerun will be Sunday 4 PM EST.''', 11) + _create_post(f'Movie Night', f'''Our Movie Night today will show `{get_names()}`.\nThe movies will start at 8 PM EST. [Here is a timezone converter for whoever needs it.](https://dateful.com/time-zone-converter?t=8pm&tz1=EST-EDT-Eastern-Time). You can also check this [countdown timer](https://www.tickcounter.com/countdown/4435809/movie-night) instead.\nThey will be shown [here](/chat).\nThere will be a 5-minute bathroom break at the 50:00 mark.\nRerun will be Sunday 4 PM EST.''', 11) g.db.commit() if every_fri_23: @@ -96,16 +96,13 @@ def cron_fn(every_5m, every_1d, every_fri_12, every_fri_23, every_sat_00, every_ g.db.commit() if every_sun_07: - _create_post(f'Movie Night Rerun', f'''Our Movie Night Rerun today will show `{get_name()}`.\nThe movies will start at 4 PM EST. [Here is a timezone converter for whoever needs it.](https://dateful.com/time-zone-converter?t=4pm&tz1=EST-EDT-Eastern-Time). You can also check this [countdown timer](https://www.tickcounter.com/countdown/4465675/movie-night-rerun) instead.\nThey will be shown [here](/chat).\nThere will be a 5-minute bathroom break at the 50:00 mark.''', 1) + _create_post(f'Movie Night Rerun', f'''Our Movie Night Rerun today will show `{get_names()}`.\nThe movies will start at 4 PM EST. [Here is a timezone converter for whoever needs it.](https://dateful.com/time-zone-converter?t=4pm&tz1=EST-EDT-Eastern-Time). You can also check this [countdown timer](https://www.tickcounter.com/countdown/4465675/movie-night-rerun) instead.\nThey will be shown [here](/chat).\nThere will be a 5-minute bathroom break at the 50:00 mark.''', 1) g.db.commit() if every_sun_19: _create_post(f'Movie Night Rerun in 60 minutes', 'It will be shown [here](/chat).\nThere will be a 5-minute bathroom break at the 50:00 mark.', 1) g.db.commit() - if every_sat_00 or every_sun_20: - _create_and_delete_orgy() - if every_sat_03 or every_sun_23: _delete_all_posts() g.db.commit() @@ -140,16 +137,13 @@ def cron_fn(every_5m, every_1d, every_fri_12, every_fri_23, every_sat_00, every_ def cron(**kwargs): cron_fn(**kwargs) -def get_file(): - return max(glob.glob('/orgies/*'), key=os.path.getctime).split('/orgies/')[1] - -def get_name(): - return get_file().split('.')[0] +def get_names(): + return ' and '.join([x[0] for x in g.db.query(Orgy.title).filter_by(type='file').order_by(Orgy.start_utc)]) def _create_post(title, body, pin_hours): _delete_all_posts() - title += f': {get_name()}' + title += f': {get_names()}' title_html = filter_emojis_only(title) body_html = sanitize(body) @@ -184,26 +178,6 @@ def _create_post(title, body, pin_hours): cache.delete_memoized(frontlist) -def _create_and_delete_orgy(): - video_info = ffmpeg.probe(f'/orgies/{get_file()}') - seconds = float(video_info['streams'][0]['duration']) - end_utc = int(time.time() + seconds) - - orgy = Orgy( - title=get_name(), - type='file', - data=f'https://videos.watchpeopledie.tv/orgies/{get_file()}', - end_utc = end_utc, - ) - g.db.add(orgy) - g.db.commit() - g.db.close() - del g.db - stdout.flush() - - requests.post('http://localhost:5001/refresh_chat', headers={"Host": SITE}) - - def _delete_all_posts(): posts = g.db.query(Post).filter_by(author_id=AUTOJANNY_ID, deleted_utc=0).all() for p in posts: diff --git a/files/routes/admin.py b/files/routes/admin.py index c738559a78..788372d3a5 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -1942,14 +1942,14 @@ def admin_reset_password(user_id, v): @app.get("/admin/orgy") @admin_level_required(PERMS['ORGIES']) def orgy_control(v): - orgy = g.db.query(Orgy).one_or_none() - return render_template("admin/orgy_control.html", v=v, orgy=orgy) + return render_template("admin/orgy_control.html", v=v, orgy=get_orgy(v)) -@app.post("/admin/start_orgy") +@app.post("/admin/schedule_orgy") @admin_level_required(PERMS['ORGIES']) -def start_orgy(v): +def schedule_orgy(v): link = request.values.get("link", "").strip() title = request.values.get("title", "").strip() + start_utc = request.values.get("start_utc", "").strip() if not link: abort(400, "A link is required!") @@ -1961,6 +1961,12 @@ def start_orgy(v): abort(400, "An orgy is already in progress") normalized_link = normalize_url(link) + + if start_utc: + start_utc = int(start_utc) + else: + start_utc = int(time.time()) + end_utc = None if bare_youtube_regex.match(normalized_link): @@ -1982,7 +1988,8 @@ def start_orgy(v): pass else: seconds = float(video_info['streams'][0]['duration']) - end_utc = int(time.time() + seconds) + if seconds > 2.0: + end_utc = int(start_utc + seconds) else: abort(400) @@ -1990,20 +1997,18 @@ def start_orgy(v): title=title, type=orgy_type, data=data, + start_utc=start_utc, end_utc=end_utc, ) g.db.add(orgy) ma = ModAction( - kind="start_orgy", + kind="schedule_orgy", user_id=v.id, _note=data, ) g.db.add(ma) - g.db.commit() - requests.post('http://localhost:5001/refresh_chat', headers={"Host": SITE}) - return redirect('/chat') @app.post("/admin/stop_orgy") @@ -2021,8 +2026,9 @@ def stop_orgy(v): ) g.db.add(ma) - g.db.delete(orgy) + orgy.end_utc = int(time.time()) + g.db.add(orgy) - requests.post('http://localhost:5001/refresh_chat', headers={"Host": SITE}) + get_orgy(v) #don't remove this, it's necessary (trust) return {"message": "Orgy stopped successfully!"} diff --git a/files/routes/chat.py b/files/routes/chat.py index 7e8daff09f..ee0ce2f17a 100644 --- a/files/routes/chat.py +++ b/files/routes/chat.py @@ -71,13 +71,7 @@ def chat(v): orgy = get_orgy(v) if orgy: - m = md5() - with open('files/assets/subtitles.vtt', "rb") as f: - data = f.read() - m.update(data) - subtitles_hash = m.hexdigest() - - return render_template("orgy.html", v=v, messages=displayed_messages, orgy=orgy, subtitles_hash=subtitles_hash) + return render_template("orgy.html", v=v, messages=displayed_messages, orgy=orgy) return render_template("chat.html", v=v, messages=displayed_messages) diff --git a/files/templates/admin/admin_home.html b/files/templates/admin/admin_home.html index a44a4c21cc..c7639b9efb 100644 --- a/files/templates/admin/admin_home.html +++ b/files/templates/admin/admin_home.html @@ -7,7 +7,7 @@ {% if v.admin_level >= PERMS['ORGIES'] %}

Orgies

{%- endif %} diff --git a/files/templates/admin/orgy_control.html b/files/templates/admin/orgy_control.html index e7075fa0dd..495d707831 100644 --- a/files/templates/admin/orgy_control.html +++ b/files/templates/admin/orgy_control.html @@ -13,7 +13,7 @@
{% if not orgy %} -
+
@@ -22,17 +22,25 @@
-
+
- +
+
+
+ +
+
+ +
+
-
- +
+
{% else %} diff --git a/files/templates/orgy.html b/files/templates/orgy.html index 7c198e8b71..305af2ec2e 100644 --- a/files/templates/orgy.html +++ b/files/templates/orgy.html @@ -24,8 +24,11 @@ {% elif orgy.type == 'file' %} -