From 4c3d2cc7341fc13410873ca1cae11fe0f2d5eacf Mon Sep 17 00:00:00 2001 From: Lili Date: Wed, 14 Sep 2022 14:05:10 +0200 Subject: [PATCH] improvement: no points giving when stream is off --- tlapbot/__init__.py | 6 +++--- tlapbot/owncast_helpers.py | 40 +++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/tlapbot/__init__.py b/tlapbot/__init__.py index 3823598..5ba8b11 100644 --- a/tlapbot/__init__.py +++ b/tlapbot/__init__.py @@ -2,7 +2,7 @@ import os from flask import Flask from apscheduler.schedulers.background import BackgroundScheduler from tlapbot.db import get_db -from tlapbot.owncast_helpers import give_points_to_chat +from tlapbot.owncast_helpers import is_stream_live, give_points_to_chat def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) @@ -24,8 +24,8 @@ def create_app(test_config=None): def proxy_job(): with app.app_context(): - #TODO: make this not give points when stream is offline lol - give_points_to_chat(get_db()) + if is_stream_live(): + give_points_to_chat(get_db()) points_giver = BackgroundScheduler() points_giver.add_job(proxy_job, 'interval', seconds=app.config['POINTS_CYCLE_TIME']) # change to 10 minutes out of testing diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index ff3fcbe..b684b3f 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -2,6 +2,31 @@ from flask import current_app import requests from sqlite3 import Error + +# # # requests stuff # # # +def is_stream_live(): + url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/status' + r = requests.post(url) + print(r.json()["online"]) + return r.json()["online"] + +def give_points_to_chat(db): + url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/clients' + headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']} + r = requests.post(url, headers=headers) + for user_object in r.json(): + give_points_to_user(db, + user_object["user"]["id"], + current_app.config['POINTS_AMOUNT_GIVEN']) + +def send_chat(message): + url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/chat/send' + headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']} + r = requests.post(url, headers=headers, json={"body": message}) + return r.json() + + +# # # db stuff # # # def read_users_points(db, user_id): try: cursor = db.execute( @@ -37,15 +62,6 @@ def use_points(db, user_id, points): print("From user:", user_id, " amount of points:", points) return False -def give_points_to_chat(db): - url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/clients' - headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']} - r = requests.post(url, headers=headers) - for user_object in r.json(): - give_points_to_user(db, - user_object["user"]["id"], - current_app.config['POINTS_AMOUNT_GIVEN']) - def user_exists(db, user_id): try: cursor = db.execute( @@ -87,11 +103,7 @@ def change_display_name(db, user_id, new_name): print("Error occured changing display name:", e.args[0]) print("To user:", user_id, new_name) -def send_chat(message): - url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/chat/send' - headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']} - r = requests.post(url, headers=headers, json={"body": message}) - return r.json() + def add_to_redeem_queue(db, user_id, redeem_name): try: