diff --git a/tlapbot/__init__.py b/tlapbot/__init__.py index 5a550bb..870500e 100644 --- a/tlapbot/__init__.py +++ b/tlapbot/__init__.py @@ -2,9 +2,12 @@ import os import logging from flask import Flask from apscheduler.schedulers.background import BackgroundScheduler +from datetime import datetime from tlapbot.db import get_db from tlapbot.owncast_requests import is_stream_live, give_points_to_chat from tlapbot.redeems import remove_inactive_redeems +from tlapbot.helpers import (get_last_online_time, delete_last_online_time, + save_last_online_time) def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) @@ -56,9 +59,14 @@ def create_app(test_config=None): def proxy_job(): with app.app_context(): if is_stream_live(): + if get_last_online_time: + delete_last_online_time() app.logger.info("Stream is LIVE. Giving points to chat.") give_points_to_chat(get_db()) else: + if not get_last_online_time: + # TODO: error state + save_last_online_time(get_db(), datetime.now(), False) app.logger.info("Stream is NOT LIVE. (Not giving points to chat.)") # start scheduler that will give points to users diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index c727e25..8d5b041 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -95,6 +95,37 @@ def add_user_to_database(db, user_id, display_name): current_app.logger.error(f"To user id: {user_id}, with display name: {display_name}") +def save_last_online_time(db, timestamp, from_owncast): + try: + db.execute( + "INSERT OVERWRITE last_online_time(id, last_online_time, from_owncast)", + (1, timestamp, from_owncast) + ) + db.commit() + except Error as e: + current_app.logger.error(f"Error occured saving last online time: {e.args[0]}") + current_app.logger.error(f"Timestamp: {timestamp}, from_owncast: {from_owncast}") + + +def get_last_online_time(db): + try: + cursor = db.execute( + "SELECT last_online_time FROM last_online_time WHERE id = 1" + ) + last_online_time = cursor.fetchone() + return last_online_time + except Error as e: + current_app.logger.error(f"Error occured reading last online time: {e.args[0]}") + + +def delete_last_online_time(db): + try: + db.execute("DELETE FROM last_online_time") + db.commit() + except Error as e: + current_app.logger.error(f"Error occured deleting last online time: {e.args[0]}") + + def change_display_name(db, user_id, new_name): try: cursor = db.execute( diff --git a/tlapbot/owncast_webhooks.py b/tlapbot/owncast_webhooks.py index aaaab05..c443614 100644 --- a/tlapbot/owncast_webhooks.py +++ b/tlapbot/owncast_webhooks.py @@ -1,11 +1,13 @@ from flask import Flask, request, json, Blueprint, current_app -from tlapbot.db import get_db +from datetime import datetime +from tlapbot.db import get_db, refresh_counters, clear_redeem_queue from tlapbot.owncast_requests import send_chat from tlapbot.owncast_helpers import (add_user_to_database, change_display_name, - read_users_points, remove_duplicate_usernames) + read_users_points, remove_duplicate_usernames, get_last_online_time, delete_last_online_time) from tlapbot.help_message import send_help from tlapbot.redeems_handler import handle_redeem +# might need datetime timestamp bp = Blueprint('owncast_webhooks', __name__) @@ -15,6 +17,22 @@ def owncast_webhook(): data = request.json db = get_db() + if data["type"] == "STREAM_STARTED": + # TODO: make this a function, import here and in init + delete_last_online_time(db) + last_online = get_last_online_time(db) + if last_online and current_app.config['AUTO_REFRESH']: + time_difference = datetime.now() - last_online + if time_difference.seconds//60 > current_app.config['RECONNECT_TIME']: + if refresh_counters() and clear_redeem_queue(): + current_app.logger.debug(f'Counters refreshed, redeem queue cleared.') + else: + current_app.logger.error( + f'Error occured when automatically clearing queue and resetting counters.' + ) + elif data["type"] == "STREAM_STOPPED": + save_last_online_time(db, datetime.now(), True) + # Make sure user is in db before doing anything else. if data["type"] in ["CHAT", "NAME_CHANGED", "USER_JOINED"]: user_id = data["eventData"]["user"]["id"] diff --git a/tlapbot/schema.sql b/tlapbot/schema.sql index 4eb4dab..0ec944b 100644 --- a/tlapbot/schema.sql +++ b/tlapbot/schema.sql @@ -29,4 +29,9 @@ CREATE TABLE redeem_queue ( redeemer_id TEXT NOT NULL, note TEXT, FOREIGN KEY (redeemer_id) REFERENCES points (id) +); + +CREATE TABLE online_time( + id INTEGER PRIMARY KEY, + online_time TIMESTAMP NOT NULL ); \ No newline at end of file