owncast_com.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. if response.json()['online']:
  31. return True
  32. else:
  33. return False
  34. except requests.exceptions.RequestException as cserror:
  35. current_app.logger.error(f'Couldn\'t check if stream is live: {cserror.args[0]}')
  36. return False
  37. else:
  38. current_app.logger.error('Owncast url is not set. Can\'t check if stream is live.')
  39. return False
  40. def award_points(db): # Award points to users
  41. owncast_url = current_app.config['OWNCAST_URL']
  42. access_token = current_app.config['ACCESS_TOKEN']
  43. url = f'{owncast_url}/api/integrations/clients'
  44. auth_bearer = f'Bearer {access_token}'
  45. headers = {'Authorization': auth_bearer}
  46. try:
  47. response = requests.get(url, headers=headers)
  48. except requests.exceptions.RequestException as aperror:
  49. current_app.logger.error(f'Couldn\'t get user info: {aperror.args[0]}')
  50. return
  51. if response.status_code != 200:
  52. current_app.logger.error(f'Couldn\'t award points: {response.status_code}.')
  53. return
  54. unique_users = set(map(lambda user_object: user_object['user']['id'], response.json()))
  55. for user_id in unique_users:
  56. award_chat_points(db, user_id, current_app.config['POINTS_AWARD'])
  57. def send_chat(message): # Send message to owncast chat
  58. owncast_url = current_app.config['OWNCAST_URL']
  59. access_token = current_app.config['ACCESS_TOKEN']
  60. url = f'{owncast_url}/api/integrations/chat/send'
  61. auth_bearer = f'Bearer {access_token}'
  62. headers = {'Authorization': auth_bearer}
  63. try:
  64. response = requests.post(url, headers=headers, json={'body': message})
  65. except requests.exceptions.RequestException as scerror:
  66. current_app.logger.error(f'Couldn\'t send \"{message}\" to Owncast: {scerror.args[0]}')
  67. return
  68. if response.status_code != 200:
  69. current_app.logger.error(f'Couldn\'t send \"{message}\" to Owncast: {response.status_code}.')
  70. return
  71. return response.json()
  72. def send_private_chat(user_id, message):
  73. client_id = get_client_id(user_id)
  74. if client_id or client_id == 0: # If no client ID is found, don't try to send
  75. owncast_url = current_app.config['OWNCAST_URL']
  76. access_token = current_app.config['ACCESS_TOKEN']
  77. url = f'{owncast_url}/api/integrations/chat/system/client/{client_id}'
  78. auth_bearer = f'Bearer {access_token}'
  79. headers = {'Authorization': auth_bearer}
  80. try:
  81. response = requests.post(url, headers=headers, json={'body': message})
  82. except requests.exceptions.RequestException as swerror:
  83. current_app.logger.error(f'Couldn\'t send \"{message}\" to Owncast: {swerror.args[0]}')
  84. except Exception as swerror:
  85. current_app.logger.error(f'Couldn\'t send \"{message}\" to Owncast: {swerror.args[0]}')
  86. if response.status_code != 200:
  87. current_app.logger.error(f'Couldn\'t send \"{message}\" to Owncast: {response.status_code}.')
  88. else:
  89. current_app.logger.info(f'Couldn\'t send \"{message}\" to Owncast. Not a connected client.')
  90. def send_system_chat(message):
  91. owncast_url = current_app.config['OWNCAST_URL']
  92. access_token = current_app.config['ACCESS_TOKEN']
  93. url = f'{owncast_url}/api/integrations/chat/system'
  94. auth_bearer = f'Bearer {access_token}'
  95. headers = {'Authorization': auth_bearer}
  96. try:
  97. response = requests.post(url, headers=headers, json={'body': message})
  98. except requests.exceptions.RequestException as swerror:
  99. current_app.logger.error(f'Couldn\'t send {message} to Owncast: {swerror.args[0]}')
  100. sys.exit()
  101. if response.status_code != 200:
  102. current_app.logger.error(f'Couldn\'t send {message} to Owncast: {response.status_code}.')
  103. sys.exit()
  104. response = response.json()