diff --git a/push_msg.py b/push_msg.py index f3329ff..4f9f0b5 100755 --- a/push_msg.py +++ b/push_msg.py @@ -1,13 +1,12 @@ #!/usr/bin/env python3 import requests -import json import time import logging try: from settings import * except: - print("Please provide a settings.py file") + print('Please provide a settings.py file') exit(0) @@ -21,11 +20,11 @@ format='%(asctime)s %(levelname)8s - %(message)s' ) except FileNotFoundError: - print("ERROR: Invalid log file path specified in settings.py") + print('ERROR: Invalid log file path specified in settings.py') exit(0) except NameError: # log_file is an optional, setting and may not be present in older config files - print("INFO: No logfile specified, logging is disabled") + print('INFO: No logfile specified, logging is disabled') pass @@ -47,26 +46,28 @@ def get_notifications(): """Retrieve notifications from nextcloud""" try: # retrieve json data from the notifications endpoint - r = requests.get(url, headers=headers, auth=(user, pw)) + full_url = '%s/ocs/v2.php/apps/notifications/api/v2/notifications' % url + r = requests.get(full_url, headers=headers, auth=(user, pw)) # load the json data m = (r.json()) if r.status_code < 300: # only handle success status codes - return m["ocs"]["data"] + return m['ocs']['data'] else: logging.error('failed to retrieve notifications - %s', r.text) except requests.exceptions.RequestException as err: - logging.info("failed to connect to nextcloud - %s", repr(err)) + logging.info('failed to connect to nextcloud - %s', repr(err)) except (ValueError, KeyError) as err: - logging.error("failed to parse notifications - %s", repr(err)) + logging.error('failed to parse notifications - %s', repr(err)) return [] def push_notification(notification_id, date, title, msg, priority): """Send the notification to the gotify server.""" try: + full_urlpush = '%s/message' % urlpush response = requests.post( - urlpush, + full_urlpush, headers=headerspush, data={ 'id': notification_id, @@ -76,28 +77,29 @@ def push_notification(notification_id, date, title, msg, priority): 'priority': priority} ) except requests.exceptions.RequestException as e: - logging.error("push to gotify server failed - %s", repr(e)) + logging.error('push to gotify server failed - %s', repr(e)) return False if response.status_code < 300: return True else: - logging.error("push to gotify server failed with HTTP status %s - %s", response.status_code, response.text) + logging.error('push to gotify server failed with HTTP status %s - %s', + response.status_code, response.text) return False # start infinite loop for listening -if __name__ == "__main__": +if __name__ == '__main__': while True: new_notification_list = get_notifications() # Iterate over the notifications for n in new_notification_list: try: - n_id = n["notification_id"] # id - title = n["subject"] - date = n["datetime"] - msg = n["message"] or " " + n_id = n['notification_id'] # id + title = n['subject'] + date = n['datetime'] + msg = n['message'] or ' ' except (KeyError, AttributeError): # invalid or unsupported notification format logging.warning('Invalid notification object - %s', n) diff --git a/settings.py.sample b/settings.py.sample index 02f5c9b..2c6be6a 100755 --- a/settings.py.sample +++ b/settings.py.sample @@ -1,27 +1,28 @@ #!/usr/bin/python3 +from os import getenv ########################################## # Nextcloud settings -url='https://example.com/ocs/v2.php/apps/notifications/api/v2/notifications' -user='username' -pw='secretpw' +url = getenv('NEXTCLOUD_URL', 'https://nextcloudexample.com') +user = getenv('NEXTCLOUD_USER', 'username') +pw = getenv('NEXTCLOUD_PASSWORD', 'secretpw') ########################################## # Gotify settings -urlpush='https://example.com/message' -token='TOKEN' +urlpush = getenv('GOTIFY_URL', 'https://gotifyexample.com') +token = getenv('GOTIFY_TOKEN', 'TOKEN') ########################################## # General settings # Frequency for checking new notifications -delay=30 +delay = int(getenv('NOTIFICATION_DELAY', 30)) # Priority for the notification sent through this channel # If this is set below 10, there won't be any sound when receiving a message -notification_priority='10' +notification_priority = getenv('NOTIFICATION_PRIORITY', '10') # Optional log file -log_file = 'logs/gotify-nc.log' +log_file = getenv('LOG_PATH', 'logs/gotify-nc.log')