|
@@ -3,19 +3,36 @@ 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):
|
|
|
- new_status = {'body': msg}
|
|
|
- headers = {'Authorization': f'Bearer {config.auth_bearer}'}
|
|
|
- response = requests.post(f'{config.api_url}/api/integrations/chat/send', data=json.dumps(new_status),
|
|
|
- headers=headers)
|
|
|
- return response.json()
|
|
|
+ if live_now() is True:
|
|
|
+ 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.')
|
|
|
|
|
|
|
|
|
def main():
|
|
|
for nag in config.nags:
|
|
|
- print(f'Sending \"{nag}\"...')
|
|
|
+ logging.info(f'{get_now()} - Sending \"{nag}\"...')
|
|
|
send_msg(nag)
|
|
|
time.sleep(config.interval) # Wait between messages
|
|
|
|
|
@@ -24,8 +41,9 @@ try:
|
|
|
while True:
|
|
|
main()
|
|
|
except KeyboardInterrupt:
|
|
|
- print('\nExiting program...')
|
|
|
+ logging.info(f'{get_now()} - Ctrl-alt-delete caught. Exiting program...')
|
|
|
+ print('Ctrl-alt-delete caught. Exiting program...')
|
|
|
sys.exit()
|
|
|
except Exception as error:
|
|
|
- print(f'\n{error}\n')
|
|
|
+ logging.info(f'{get_now()} - \n{error}\n')
|
|
|
sys.exit()
|