owncast_com.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. client_id = response['connectedClients'][0]['id']
  15. except requests.exceptions.RequestException as gcierror:
  16. current_app.logger.error(f'Couldn\'t get client id from Owncast: {gcierror.args[0]}')
  17. current_app.logger.debug(f'Got client id {client_id}')
  18. return client_id
  19. def live_now(): # Check if stream is live
  20. owncast_url = current_app.config['OWNCAST_URL']
  21. if owncast_url != '': # If owncast URL is set, award points
  22. url = f'{owncast_url}/api/status'
  23. try:
  24. response = requests.get(url)
  25. except requests.exceptions.RequestException as cserror:
  26. current_app.logger.error(f'Couldn\'t check if stream is live: {cserror.args[0]}')
  27. return False
  28. return response.json()['online']
  29. else:
  30. current_app.logger.error('Owncast url is not set. Can\'t check if stream is live.')
  31. def award_points(db): # Award points to users
  32. owncast_url = current_app.config['OWNCAST_URL']
  33. access_token = current_app.config['ACCESS_TOKEN']
  34. url = f'{owncast_url}/api/integrations/clients'
  35. auth_bearer = f'Bearer {access_token}'
  36. headers = {'Authorization': auth_bearer}
  37. try:
  38. response = requests.get(url, headers=headers)
  39. except requests.exceptions.RequestException as aperror:
  40. current_app.logger.error(f'Couldn\'t get user info: {aperror.args[0]}')
  41. return
  42. if response.status_code != 200:
  43. current_app.logger.error(f'Couldn\'t award points: {response.status_code}.')
  44. return
  45. unique_users = set(map(lambda user_object: user_object['user']['id'], response.json()))
  46. for user_id in unique_users:
  47. award_chat_points(db, user_id, current_app.config['POINTS_AWARD'])
  48. def send_chat(message): # Send message to owncast chat
  49. owncast_url = current_app.config['OWNCAST_URL']
  50. access_token = current_app.config['ACCESS_TOKEN']
  51. url = f'{owncast_url}/api/integrations/chat/send'
  52. auth_bearer = f'Bearer {access_token}'
  53. headers = {'Authorization': auth_bearer}
  54. try:
  55. response = requests.post(url, headers=headers, json={'body': message})
  56. except requests.exceptions.RequestException as scerror:
  57. current_app.logger.error(f'Couldn\'t send {message} to Owncast: {scerror.args[0]}')
  58. return
  59. if response.status_code != 200:
  60. current_app.logger.error(f'Couldn\'t send {message} to Owncast: {response.status_code}.')
  61. return
  62. return response.json()
  63. def send_private_chat(user_id, message):
  64. client_id = get_client_id(user_id)
  65. owncast_url = current_app.config['OWNCAST_URL']
  66. access_token = current_app.config['ACCESS_TOKEN']
  67. url = f'{owncast_url}/api/integrations/chat/system/client/{client_id}'
  68. auth_bearer = f'Bearer {access_token}'
  69. headers = {'Authorization': auth_bearer}
  70. try:
  71. response = requests.post(url, headers=headers, json={'body': message})
  72. except requests.exceptions.RequestException as swerror:
  73. current_app.logger.error(f'Couldn\'t send {message} to Owncast: {swerror.args[0]}')
  74. sys.exit()
  75. if response.status_code != 200:
  76. current_app.logger.error(f'Couldn\'t send {message} to Owncast: {response.status_code}.')
  77. sys.exit()
  78. response = response.json()
  79. def send_system_chat(message):
  80. owncast_url = current_app.config['OWNCAST_URL']
  81. access_token = current_app.config['ACCESS_TOKEN']
  82. url = f'{owncast_url}/api/integrations/chat/system'
  83. auth_bearer = f'Bearer {access_token}'
  84. headers = {'Authorization': auth_bearer}
  85. try:
  86. response = requests.post(url, headers=headers, json={'body': message})
  87. except requests.exceptions.RequestException as swerror:
  88. current_app.logger.error(f'Couldn\'t send {message} to Owncast: {swerror.args[0]}')
  89. sys.exit()
  90. if response.status_code != 200:
  91. current_app.logger.error(f'Couldn\'t send {message} to Owncast: {response.status_code}.')
  92. sys.exit()
  93. response = response.json()