From 61617aaf0fc7ed1d6614b2beb21ee6a37799a727 Mon Sep 17 00:00:00 2001 From: Vitold S Date: Mon, 22 Apr 2019 01:23:43 +0300 Subject: [PATCH] Initial revision --- aiopo/_PushoverClient.py | 50 +++++++++++++++++++++++++++++++++++++ aiopo/__init__.py | 2 ++ example/01_aiopo_example.py | 36 ++++++++++++++++++++++++++ setup.py | 45 +++++++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 aiopo/_PushoverClient.py create mode 100644 aiopo/__init__.py create mode 100644 example/01_aiopo_example.py create mode 100644 setup.py diff --git a/aiopo/_PushoverClient.py b/aiopo/_PushoverClient.py new file mode 100644 index 0000000..17842c7 --- /dev/null +++ b/aiopo/_PushoverClient.py @@ -0,0 +1,50 @@ + +from logging import getLogger +from aiohttp import ClientSession + + +class PushoverClient(object): + def __init__(self, app_token, user_key): + self.__log = getLogger('aiopo') + self._url = 'https://api.pushover.net/1/messages.json' + self._app_token = app_token + self._user_key = user_key + + async def notify(self, message): + """ Push message + + Pushover API request is HTTPS POST request: + + token (required) - your application's API token + user (required) - the user/group key (not e-mail address) of your user (or you), viewable when logged into our dashboard (often referred to as USER_KEY in our documentation and code examples) + message (required) - your message + + Some optional parameters may be included: + + attachment - an image attachment to send with the message; see attachments for more information on how to upload files + device - your user's device name to send the message directly to that device, rather than all of the user's devices (multiple devices may be separated by a comma) + title - your message's title, otherwise your app's name is used + url - a supplementary URL to show with your message + url_title - a title for your supplementary URL, otherwise just the URL is shown + priority - send as -2 to generate no notification/alert, -1 to always send as a quiet notification, 1 to display as high-priority and bypass the user's quiet hours, or 2 to also require confirmation from the user + sound - the name of one of the sounds supported by device clients to override the user's default sound choice + timestamp - a Unix timestamp of your message's date and time to display to the user, rather than the time your message is received by our API + + Response: + + DEBUG:aiopo:200 + DEBUG:aiopo:{"status":1,"request":"c36a3056-4377-4d88-9543-f6e5da94c781"} + + """ + payload = { + 'token': self._app_token, + 'user': self._user_key, + 'message': message, + } + async with ClientSession() as session: + async with session.post(self._url, data=payload) as resp: + self.__log.debug("status = {status!r}".format(status=resp.status)) + if resp.status != 200: + raise RuntimeError('Pushover response code {status}'.format(status=resp.status)) + values = await resp.json() + return values diff --git a/aiopo/__init__.py b/aiopo/__init__.py new file mode 100644 index 0000000..b2fa92a --- /dev/null +++ b/aiopo/__init__.py @@ -0,0 +1,2 @@ + +from ._PushoverClient import PushoverClient diff --git a/example/01_aiopo_example.py b/example/01_aiopo_example.py new file mode 100644 index 0000000..4146249 --- /dev/null +++ b/example/01_aiopo_example.py @@ -0,0 +1,36 @@ +#!/usr/bin/python + +from logging import getLogger, basicConfig, DEBUG +from asyncio import get_event_loop +from aiopo import PushoverClient + +APP_TOKEN = '...' +USER_KEY = '...' +MESSAGE = 'Hello, world!' + +class Application(object): + def __init__(self): + self.__log = getLogger('aiopo-example') + self.loop = None + self.po = None + + async def start(self): + """ Start corotine + """ + self.po = PushoverClient(app_token=APP_TOKEN, user_key=USER_KEY) + await self.po.notify(message=MESSAGE) + + async def stop(self): + """ Stop corotine + """ + + def run(self): + self.loop = get_event_loop() + # + self.loop.create_task(self.start()) + self.loop.run_forever() + +if __name__ == "__main__": + basicConfig(filename="debug.log", level=DEBUG) + app = Application() + app.run() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..6af5f05 --- /dev/null +++ b/setup.py @@ -0,0 +1,45 @@ +#!/usr/bin/python3 + +from sys import version_info +from setuptools import setup + +if version_info < (3, 5, 3): + raise RuntimeError("aiopo requires Python 3.5.3+") + +setup( + name='aiopo', + version='1.0', + description='Async Pushover client (asyncio)', + classifiers=[ + 'Intended Audience :: Developers', + 'Programming Language :: Python', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.5', +# 'Programming Language :: Python :: 3.6', +# 'Programming Language :: Python :: 3.7', + 'Operating System :: POSIX', +# 'Operating System :: MacOS :: MacOS X', + 'Operating System :: Microsoft :: Windows', + 'Development Status :: 4 - Beta', +# 'Development Status :: 5 - Production/Stable', + 'License :: OSI Approved :: MIT License', + ], + author='Vitold Sedyshev', + author_email='vit1251@gmail.com', + maintainer=', '.join([ + 'Vitold Sedyshev ', + ]), + maintainer_email='aiopo@googlegroups.com', + url='https://github.com/vit1251/aiopo', + project_urls={ +# 'CI: Travis': '...', +# 'Coverage: codecov': '...', +# 'GitHub: issues': '', +# 'GitHub: repo': '', + }, + license='MIT', + packages=['aiopo'], + python_requires='>=3.5.3', + install_requires=['aiohttp'], + include_package_data=True, +)