123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import requests
- import json
- import sys
- import time
- import config
- import logging
- import requests
- logging.basicConfig(filename=config.logfile, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
- 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():
- logging.info(f'Sending \"{msg}\"...')
- 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'Owncast stream is not live, so not sending messages.')
- def main():
- logging.info(f'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'Ctrl-alt-delete caught. Exiting program...')
- print('Ctrl-alt-delete caught. Exiting program...')
- sys.exit()
- except Exception as error:
- logging.info(f'\n{error}\n')
- sys.exit()
|