nagbot.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import requests
  2. import json
  3. import sys
  4. import time
  5. import config
  6. import logging
  7. import requests
  8. logging.basicConfig(filename=config.logfile, level=logging.INFO)
  9. def get_now(): # Create a timestamp for logging
  10. now = str(time.strftime("%Y/%m/%d %H:%M:%S"))
  11. return now
  12. def live_now(): # Check if the stream is live
  13. response = requests.get(f'{config.owncast_url}/api/status')
  14. response = response.json()
  15. return response['online']
  16. def send_msg(msg):
  17. if live_now() is True:
  18. new_status = {'body': msg}
  19. headers = {'Authorization': f'Bearer {config.auth_bearer}'}
  20. response = requests.post(f'{config.owncast_url}/api/integrations/chat/send', data=json.dumps(new_status), headers=headers)
  21. return response.json()
  22. else:
  23. logging.info(f'{get_now()} - Owncast stream is not live, so not sending.')
  24. def main():
  25. for nag in config.nags:
  26. logging.info(f'{get_now()} - Sending \"{nag}\"...')
  27. send_msg(nag)
  28. time.sleep(config.interval) # Wait between messages
  29. try:
  30. while True:
  31. main()
  32. except KeyboardInterrupt:
  33. logging.info(f'{get_now()} - Ctrl-alt-delete caught. Exiting program...')
  34. print('Ctrl-alt-delete caught. Exiting program...')
  35. sys.exit()
  36. except Exception as error:
  37. logging.info(f'{get_now()} - \n{error}\n')
  38. sys.exit()