123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- from flask import current_app
- import requests
- from ownchatbot.user_handlers import award_chat_points
- import random
- def get_client_id(user_id):
- owncast_url = current_app.config['OWNCAST_URL']
- access_token = current_app.config['ACCESS_TOKEN']
- url = f'{owncast_url}/api/integrations/moderation/chat/user/{user_id}'
- auth_bearer = f'Bearer {access_token}'
- headers = {'Authorization': auth_bearer}
- try:
- response = requests.get(url, headers=headers)
- response = response.json()
- client_id = response['connectedClients'][0]['id']
- except requests.exceptions.RequestException as gcierror:
- current_app.logger.error(f'Couldn\'t get client id from Owncast: {gcierror.args[0]}')
- current_app.logger.debug(f'Got client id {client_id}')
- return client_id
- def live_now(): # Check if stream is live
- owncast_url = current_app.config['OWNCAST_URL']
- if owncast_url != '': # If owncast URL is set, award points
- url = f'{owncast_url}/api/status'
- try:
- response = requests.get(url)
- except requests.exceptions.RequestException as cserror:
- current_app.logger.error(f'Couldn\'t check if stream is live: {cserror.args[0]}')
- return False
- return response.json()['online']
- else:
- current_app.logger.error('Owncast url is not set. Can\'t check if stream is live.')
- def award_points(db): # Award points to users
- owncast_url = current_app.config['OWNCAST_URL']
- access_token = current_app.config['ACCESS_TOKEN']
- url = f'{owncast_url}/api/integrations/clients'
- auth_bearer = f'Bearer {access_token}'
- headers = {'Authorization': auth_bearer}
- try:
- response = requests.get(url, headers=headers)
- except requests.exceptions.RequestException as aperror:
- current_app.logger.error(f'Couldn\'t get user info: {aperror.args[0]}')
- return
- if response.status_code != 200:
- current_app.logger.error(f'Couldn\'t award points: {response.status_code}.')
- return
- unique_users = set(map(lambda user_object: user_object['user']['id'], response.json()))
- for user_id in unique_users:
- award_chat_points(db, user_id, current_app.config['POINTS_AWARD'])
- def send_chat(message): # Send message to owncast chat
- owncast_url = current_app.config['OWNCAST_URL']
- access_token = current_app.config['ACCESS_TOKEN']
- url = f'{owncast_url}/api/integrations/chat/send'
- auth_bearer = f'Bearer {access_token}'
- headers = {'Authorization': auth_bearer}
- try:
- response = requests.post(url, headers=headers, json={'body': message})
- except requests.exceptions.RequestException as scerror:
- current_app.logger.error(f'Couldn\'t send {message} to Owncast: {scerror.args[0]}')
- return
- if response.status_code != 200:
- current_app.logger.error(f'Couldn\'t send {message} to Owncast: {response.status_code}.')
- return
- return response.json()
- def send_private_chat(user_id, message):
- client_id = get_client_id(user_id)
- owncast_url = current_app.config['OWNCAST_URL']
- access_token = current_app.config['ACCESS_TOKEN']
- url = f'{owncast_url}/api/integrations/chat/system/client/{client_id}'
- auth_bearer = f'Bearer {access_token}'
- headers = {'Authorization': auth_bearer}
- try:
- response = requests.post(url, headers=headers, json={'body': message})
- except requests.exceptions.RequestException as swerror:
- current_app.logger.error(f'Couldn\'t send {message} to Owncast: {swerror.args[0]}')
- sys.exit()
- if response.status_code != 200:
- current_app.logger.error(f'Couldn\'t send {message} to Owncast: {response.status_code}.')
- sys.exit()
- response = response.json()
- def send_system_chat(message):
- owncast_url = current_app.config['OWNCAST_URL']
- access_token = current_app.config['ACCESS_TOKEN']
- url = f'{owncast_url}/api/integrations/chat/system'
- auth_bearer = f'Bearer {access_token}'
- headers = {'Authorization': auth_bearer}
- try:
- response = requests.post(url, headers=headers, json={'body': message})
- except requests.exceptions.RequestException as swerror:
- current_app.logger.error(f'Couldn\'t send {message} to Owncast: {swerror.args[0]}')
- sys.exit()
- if response.status_code != 200:
- current_app.logger.error(f'Couldn\'t send {message} to Owncast: {response.status_code}.')
- sys.exit()
- response = response.json()
|