owncast_com.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from flask import current_app
  2. import requests
  3. from ownchatbot.user_handlers import award_chat_points
  4. import random
  5. def get_client_id(user_id):
  6. owncast_url = current_app.config['OWNCAST_URL']
  7. access_token = current_app.config['ACCESS_TOKEN']
  8. url = f'{owncast_url}/api/integrations/moderation/chat/user/{user_id}'
  9. auth_bearer = f'Bearer {access_token}'
  10. headers = {'Authorization': auth_bearer}
  11. try:
  12. response = requests.get(url, headers=headers)
  13. response = response.json()
  14. if response["connectedClients"]: # Make sure this is an actual, connected client
  15. client_id = response['connectedClients'][0]['id']
  16. current_app.logger.debug(f'Got client id {client_id}')
  17. return client_id
  18. else:
  19. current_app.logger.info('Not a connected client. Can\'t get client ID.')
  20. return False
  21. except requests.exceptions.RequestException as gcierror:
  22. current_app.logger.error(f'Couldn\'t get client id from Owncast: {gcierror.args[0]}')
  23. return False
  24. def live_now(): # Check if stream is live
  25. owncast_url = current_app.config['OWNCAST_URL']
  26. if owncast_url != '': # If owncast URL is set, award points
  27. url = f'{owncast_url}/api/status'
  28. try:
  29. response = requests.get(url)
  30. except requests.exceptions.RequestException as cserror:
  31. current_app.logger.error(f'Couldn\'t check if stream is live: {cserror.args[0]}')
  32. return False
  33. return response.json()['online']
  34. else:
  35. current_app.logger.error('Owncast url is not set. Can\'t check if stream is live.')
  36. def award_points(db): # Award points to users
  37. owncast_url = current_app.config['OWNCAST_URL']
  38. access_token = current_app.config['ACCESS_TOKEN']
  39. url = f'{owncast_url}/api/integrations/clients'
  40. auth_bearer = f'Bearer {access_token}'
  41. headers = {'Authorization': auth_bearer}
  42. try:
  43. response = requests.get(url, headers=headers)
  44. except requests.exceptions.RequestException as aperror:
  45. current_app.logger.error(f'Couldn\'t get user info: {aperror.args[0]}')
  46. return
  47. if response.status_code != 200:
  48. current_app.logger.error(f'Couldn\'t award points: {response.status_code}.')
  49. return
  50. unique_users = set(map(lambda user_object: user_object['user']['id'], response.json()))
  51. for user_id in unique_users:
  52. award_chat_points(db, user_id, current_app.config['POINTS_AWARD'])
  53. def send_chat(message): # Send message to owncast chat
  54. owncast_url = current_app.config['OWNCAST_URL']
  55. access_token = current_app.config['ACCESS_TOKEN']
  56. url = f'{owncast_url}/api/integrations/chat/send'
  57. auth_bearer = f'Bearer {access_token}'
  58. headers = {'Authorization': auth_bearer}
  59. try:
  60. response = requests.post(url, headers=headers, json={'body': message})
  61. except requests.exceptions.RequestException as scerror:
  62. current_app.logger.error(f'Couldn\'t send \"{message}\" to Owncast: {scerror.args[0]}')
  63. return
  64. if response.status_code != 200:
  65. current_app.logger.error(f'Couldn\'t send \"{message}\" to Owncast: {response.status_code}.')
  66. return
  67. return response.json()
  68. def send_private_chat(user_id, message):
  69. client_id = get_client_id(user_id)
  70. if client_id or client_id == 0: # If no client ID is found, don't try to send
  71. owncast_url = current_app.config['OWNCAST_URL']
  72. access_token = current_app.config['ACCESS_TOKEN']
  73. url = f'{owncast_url}/api/integrations/chat/system/client/{client_id}'
  74. auth_bearer = f'Bearer {access_token}'
  75. headers = {'Authorization': auth_bearer}
  76. try:
  77. response = requests.post(url, headers=headers, json={'body': message})
  78. except requests.exceptions.RequestException as swerror:
  79. current_app.logger.error(f'Couldn\'t send \"{message}\" to Owncast: {swerror.args[0]}')
  80. except Exception as swerror:
  81. current_app.logger.error(f'Couldn\'t send \"{message}\" to Owncast: {swerror.args[0]}')
  82. if response.status_code != 200:
  83. current_app.logger.error(f'Couldn\'t send \"{message}\" to Owncast: {response.status_code}.')
  84. else:
  85. current_app.logger.info(f'Couldn\'t send \"{message}\" to Owncast. Not a connected client.')
  86. def send_system_chat(message):
  87. owncast_url = current_app.config['OWNCAST_URL']
  88. access_token = current_app.config['ACCESS_TOKEN']
  89. url = f'{owncast_url}/api/integrations/chat/system'
  90. auth_bearer = f'Bearer {access_token}'
  91. headers = {'Authorization': auth_bearer}
  92. try:
  93. response = requests.post(url, headers=headers, json={'body': message})
  94. except requests.exceptions.RequestException as swerror:
  95. current_app.logger.error(f'Couldn\'t send {message} to Owncast: {swerror.args[0]}')
  96. sys.exit()
  97. if response.status_code != 200:
  98. current_app.logger.error(f'Couldn\'t send {message} to Owncast: {response.status_code}.')
  99. sys.exit()
  100. response = response.json()