split out requests to owncast_requests.py

This commit is contained in:
Lili (Tlapka) 2023-03-20 17:16:17 +01:00
parent d6297c537f
commit 7235340a39
6 changed files with 39 additions and 35 deletions

View File

@ -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)

View File

@ -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():

View File

@ -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."""

View File

@ -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()

View File

@ -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

View File

@ -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)