1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- from flask import current_app
- import requests
- from ownchatbot.user_handlers import award_chat_points
- def live_now(): # Check if stream is live
- owncast_url = current_app.config['OWNCAST_URL']
- 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']
- 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()
|