owncast_com.py 5.0 KB

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