owncast-clipper/helpers/owncast_api.py
2025-04-26 23:45:24 -05:00

58 lines
1.7 KiB
Python

# Standard imports
import json
import sys
# Package Imports
import requests
# Local Imports
import config
def get_owncast_title():
"""
Gets the current owncast stream title, if any, via API call
Returns:
str: The current stream title, if one is set; otherwise None.
"""
try:
response = requests.get(f'{config.OWNCAST_HOST}/api/status')
if response.status_code != 200:
print(
f'Received a {response.status_code} response from Owncast while getting the title: {response}', file=sys.stderr)
return None
data = response.json()
except json.JSONDecodeError as e:
print(
f'Unable to parse response JSON from Owncast: {e}: {response}', file=sys.stderr)
return None
return data.get('streamTitle')
# TODO this should probably return whether or not it was successful.
def send_owncast_message(message: str):
"""
Sends a chat message to the owncast server via self-contained API call.
Args:
message (str): The message to be sent to owncast as the Clip Bot user
Returns:
None
"""
try:
url = f'{config.OWNCAST_HOST}/api/integrations/chat/send'
headers = {'Authorization': f'Bearer {config.OWNCAST_AUTH}'}
response = requests.post(
url=url, headers=headers, json={'body': message})
if response.status_code != 200:
print(
f'Received a {response.status_code} response from Owncast while trying to send a message: {response}', file=sys.stderr)
except requests.exceptions.RequestException as e:
print(
f'Error occurred while sending a chat message: {e}', file=sys.stderr)
return