1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import requests
- import json
- import sys
- import time
- import config
- import logging
- import requests
- logging.basicConfig(filename=config.logfile, level=logging.INFO)
- def get_now(): # Create a timestamp for logging
- now = str(time.strftime("%Y/%m/%d %H:%M:%S"))
- return now
- def live_now(): # Check if the stream is live
- response = requests.get(f'{config.owncast_url}/api/status')
- response = response.json()
- return response['online']
- def send_msg(msg):
- if live_now() is True:
- logging.info(f'{get_now()} - Sending \"{nag}\"...')
- new_status = {'body': msg}
- headers = {'Authorization': f'Bearer {config.auth_bearer}'}
- response = requests.post(f'{config.owncast_url}/api/integrations/chat/send', data=json.dumps(new_status), headers=headers)
- return response.json()
- else:
- logging.info(f'{get_now()} - Owncast stream is not live, so not sending messages.')
- def main():
- logging.info(f'{get_now()} - Starting nagbot...')
- for nag in config.nags:
- send_msg(nag)
- time.sleep(config.interval) # Wait between messages
- try:
- while True:
- main()
- except KeyboardInterrupt:
- logging.info(f'{get_now()} - Ctrl-alt-delete caught. Exiting program...')
- print('Ctrl-alt-delete caught. Exiting program...')
- sys.exit()
- except Exception as error:
- logging.info(f'{get_now()} - \n{error}\n')
- sys.exit()
|