diff options
Diffstat (limited to 'beinc_pull.py')
| -rwxr-xr-x | beinc_pull.py | 42 |
1 files changed, 18 insertions, 24 deletions
diff --git a/beinc_pull.py b/beinc_pull.py index 118a08a..6ce9c2f 100755 --- a/beinc_pull.py +++ b/beinc_pull.py | |||
| @@ -1,8 +1,7 @@ | |||
| 1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- | ||
| 3 | 2 | ||
| 4 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v4.3 | 3 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) |
| 5 | # Copyright (C) 2013-2022 Simeon Simeonov | 4 | # Copyright (C) 2013-2024 Simeon Simeonov |
| 6 | 5 | ||
| 7 | # This program is free software: you can redistribute it and/or modify | 6 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by | 7 | # it under the terms of the GNU General Public License as published by |
| @@ -17,14 +16,15 @@ | |||
| 17 | # You should have received a copy of the GNU General Public License | 16 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | """A simple client that pulls notifications from a BEINC server""" | 18 | """A simple client that pulls notifications from a BEINC server""" |
| 19 | |||
| 20 | import argparse | 20 | import argparse |
| 21 | import errno | 21 | import errno |
| 22 | import getpass | 22 | import getpass |
| 23 | import io | 23 | import io |
| 24 | import json | 24 | import json |
| 25 | import os | 25 | import os |
| 26 | import pathlib | ||
| 26 | import sched | 27 | import sched |
| 27 | import socket | ||
| 28 | import ssl | 28 | import ssl |
| 29 | import sys | 29 | import sys |
| 30 | import time | 30 | import time |
| @@ -38,7 +38,7 @@ except ImportError: | |||
| 38 | 38 | ||
| 39 | 39 | ||
| 40 | __author__ = 'Simeon Simeonov' | 40 | __author__ = 'Simeon Simeonov' |
| 41 | __version__ = '4.3' | 41 | __version__ = '4.4' |
| 42 | __license__ = 'GPL3' | 42 | __license__ = 'GPL3' |
| 43 | 43 | ||
| 44 | 44 | ||
| @@ -63,14 +63,14 @@ def fetch_password(args_password): | |||
| 63 | except KeyboardInterrupt: | 63 | except KeyboardInterrupt: |
| 64 | eprint(os.linesep + 'Prompt terminated') | 64 | eprint(os.linesep + 'Prompt terminated') |
| 65 | sys.exit(errno.EACCES) | 65 | sys.exit(errno.EACCES) |
| 66 | elif os.path.isfile(args_password): | 66 | elif pathlib.Path(args_password).is_file(): |
| 67 | try: | 67 | try: |
| 68 | with io.open(args_password, 'r', encoding='utf-8') as fp: | 68 | with io.open(args_password, 'r', encoding='utf-8') as fp: |
| 69 | passwd = fp.readline() | 69 | passwd = fp.readline() |
| 70 | if passwd.strip(): | 70 | if passwd.strip(): |
| 71 | return passwd.strip() | 71 | return passwd.strip() |
| 72 | except Exception as e: | 72 | except Exception as exp: |
| 73 | eprint(f'Unable to open password file: {e}') | 73 | eprint(f'Unable to open password file: {exp}') |
| 74 | sys.exit(1) | 74 | sys.exit(1) |
| 75 | return args_password | 75 | return args_password |
| 76 | 76 | ||
| @@ -98,8 +98,7 @@ def display_notification(args, title, message): | |||
| 98 | if not pynotify.init('BEINC Notify'): | 98 | if not pynotify.init('BEINC Notify'): |
| 99 | raise Exception('There was a problem with libnotify') | 99 | raise Exception('There was a problem with libnotify') |
| 100 | notification_obj = pynotify.Notification( | 100 | notification_obj = pynotify.Notification( |
| 101 | summary=title, | 101 | summary=title, message=message |
| 102 | message=message, | ||
| 103 | ) | 102 | ) |
| 104 | notification_obj.timeout = 1000 * args.osd_timeout | 103 | notification_obj.timeout = 1000 * args.osd_timeout |
| 105 | notification_obj.set_category('im.received') | 104 | notification_obj.set_category('im.received') |
| @@ -139,25 +138,23 @@ def pull_notifications(scheduler, args): | |||
| 139 | ) | 138 | ) |
| 140 | response_dict = json.loads(response.read().decode('utf-8')) | 139 | response_dict = json.loads(response.read().decode('utf-8')) |
| 141 | if response.code != 200: | 140 | if response.code != 200: |
| 142 | raise socket.error(response_dict.get('message', '')) | 141 | raise OSError(response_dict.get('message', '')) |
| 143 | for entry in response_dict['data']['messages']: | 142 | for entry in response_dict['data']['messages']: |
| 144 | display_notification( | 143 | display_notification( |
| 145 | args, | 144 | args, entry.get('title', ''), entry.get('message', '') |
| 146 | entry.get('title', ''), | ||
| 147 | entry.get('message', ''), | ||
| 148 | ) | 145 | ) |
| 149 | response.close() | 146 | response.close() |
| 150 | scheduler.enter( | 147 | scheduler.enter( |
| 151 | args.frequency, 1, pull_notifications, (scheduler, args) | 148 | args.frequency, 1, pull_notifications, (scheduler, args) |
| 152 | ) | 149 | ) |
| 153 | except ssl.SSLError as e: | 150 | except ssl.SSLError as err: |
| 154 | eprint(f'BEINC SSL/TLS error: {e}') | 151 | eprint(f'BEINC SSL/TLS error: {err}') |
| 155 | sys.exit(errno.EPERM) | 152 | sys.exit(errno.EPERM) |
| 156 | except socket.error as e: | 153 | except OSError as err: |
| 157 | eprint(f'BEINC connection error: {e}') | 154 | eprint(f'BEINC connection error: {err}') |
| 158 | sys.exit(errno.EPERM) | 155 | sys.exit(errno.EPERM) |
| 159 | except Exception as e: | 156 | except Exception as exp: |
| 160 | eprint(f'BEINC generic client error: {e}') | 157 | eprint(f'BEINC generic client error: {exp}') |
| 161 | sys.exit(errno.EPERM) | 158 | sys.exit(errno.EPERM) |
| 162 | 159 | ||
| 163 | 160 | ||
| @@ -167,10 +164,7 @@ def main(inargs=None): | |||
| 167 | description='The following options are available' | 164 | description='The following options are available' |
| 168 | ) | 165 | ) |
| 169 | parser.add_argument( | 166 | parser.add_argument( |
| 170 | 'url', | 167 | 'url', metavar='URL', type=str, help='BEINC server destination URL' |
| 171 | metavar='URL', | ||
| 172 | type=str, | ||
| 173 | help='BEINC server destination URL', | ||
| 174 | ) | 168 | ) |
| 175 | parser.add_argument( | 169 | parser.add_argument( |
| 176 | '-c', | 170 | '-c', |
