From 7235340a39f48c47738449b2c8bb2e3b32e91f7e Mon Sep 17 00:00:00 2001 From: Lili Date: Mon, 20 Mar 2023 17:16:17 +0100 Subject: [PATCH] split out requests to owncast_requests.py --- tlapbot/__init__.py | 4 ++-- tlapbot/help_message.py | 2 +- tlapbot/owncast_helpers.py | 30 ------------------------------ tlapbot/owncast_requests.py | 32 ++++++++++++++++++++++++++++++++ tlapbot/owncast_webhooks.py | 3 ++- tlapbot/redeems_handler.py | 3 ++- 6 files changed, 39 insertions(+), 35 deletions(-) create mode 100644 tlapbot/owncast_requests.py diff --git a/tlapbot/__init__.py b/tlapbot/__init__.py index 14eabd8..efd8c99 100644 --- a/tlapbot/__init__.py +++ b/tlapbot/__init__.py @@ -3,8 +3,8 @@ import logging from flask import Flask from apscheduler.schedulers.background import BackgroundScheduler from tlapbot.db import get_db -from tlapbot.owncast_helpers import (is_stream_live, give_points_to_chat, - remove_inactive_redeems) +from tlapbot.owncast_requests import is_stream_live, give_points_to_chat +from tlapbot.owncast_helpers import remove_inactive_redeems def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) diff --git a/tlapbot/help_message.py b/tlapbot/help_message.py index f9a7d89..017a3ce 100644 --- a/tlapbot/help_message.py +++ b/tlapbot/help_message.py @@ -1,5 +1,5 @@ from flask import current_app -from tlapbot.owncast_helpers import send_chat +from tlapbot.owncast_requests import send_chat def send_help(): diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index 732ca57..e498d17 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -1,37 +1,7 @@ from flask import current_app -import requests from sqlite3 import Error from re import sub -# # # requests stuff # # # -def is_stream_live(): - url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/status' - try: - r = requests.get(url) - except requests.exceptions.RequestException as e: - current_app.logger.error(f"Error occured checking if stream is live: {e.args[0]}") - return False - 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.get(url, headers=headers) - unique_users = set(map(lambda user_object: user_object["user"]["id"], r.json())) - for user_id in unique_users: - give_points_to_user(db, - 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): """Errors out if user doesn't exist.""" diff --git a/tlapbot/owncast_requests.py b/tlapbot/owncast_requests.py new file mode 100644 index 0000000..bc71916 --- /dev/null +++ b/tlapbot/owncast_requests.py @@ -0,0 +1,32 @@ +import requests +from flask import current_app +from tlapbot.owncast_helpers import give_points_to_user + + +def is_stream_live(): + url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/status' + try: + r = requests.get(url) + except requests.exceptions.RequestException as e: + current_app.logger.error(f"Error occured checking if stream is live: {e.args[0]}") + return False + 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.get(url, headers=headers) + unique_users = set(map(lambda user_object: user_object["user"]["id"], r.json())) + for user_id in unique_users: + give_points_to_user(db, + 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() + diff --git a/tlapbot/owncast_webhooks.py b/tlapbot/owncast_webhooks.py index 77b22da..aaaab05 100644 --- a/tlapbot/owncast_webhooks.py +++ b/tlapbot/owncast_webhooks.py @@ -1,7 +1,8 @@ from flask import Flask, request, json, Blueprint, current_app from tlapbot.db import get_db +from tlapbot.owncast_requests import send_chat from tlapbot.owncast_helpers import (add_user_to_database, change_display_name, - send_chat, read_users_points, remove_duplicate_usernames) + read_users_points, remove_duplicate_usernames) from tlapbot.help_message import send_help from tlapbot.redeems_handler import handle_redeem diff --git a/tlapbot/redeems_handler.py b/tlapbot/redeems_handler.py index 3b4ee01..3c9d2d7 100644 --- a/tlapbot/redeems_handler.py +++ b/tlapbot/redeems_handler.py @@ -1,7 +1,8 @@ from flask import current_app from tlapbot.db import get_db +from tlapbot.owncast_requests import send_chat from tlapbot.owncast_helpers import (use_points, add_to_redeem_queue, - add_to_counter, read_users_points, send_chat, remove_emoji, + add_to_counter, read_users_points, remove_emoji, add_to_milestone, check_apply_milestone_completion, milestone_complete)