From 0359fde175cb83ff391210b4702e8ca08e8feb98 Mon Sep 17 00:00:00 2001 From: Jerad Meisner Date: Sat, 10 Feb 2018 09:30:55 -0800 Subject: [PATCH] initial commit --- .gitignore | 8 ++++++ LICENSE | 21 +++++++++++++++ README.md | 1 + pysabnzbd/_init_.py | 1 + pysabnzbd/sabnzbd_api.py | 58 ++++++++++++++++++++++++++++++++++++++++ setup.cfg | 2 ++ setup.py | 17 ++++++++++++ 7 files changed, 108 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 pysabnzbd/_init_.py create mode 100644 pysabnzbd/sabnzbd_api.py create mode 100644 setup.cfg create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fbb41cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +__pycache__/ +dist/ +MANIFEST +pyxeoma.egg-info/ + +# intellij +.idea/ +*.iml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c71ce90 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Jerad Meisner + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a73c792 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Python wrapper for SABnzbd API diff --git a/pysabnzbd/_init_.py b/pysabnzbd/_init_.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/pysabnzbd/_init_.py @@ -0,0 +1 @@ + diff --git a/pysabnzbd/sabnzbd_api.py b/pysabnzbd/sabnzbd_api.py new file mode 100644 index 0000000..a30b6dc --- /dev/null +++ b/pysabnzbd/sabnzbd_api.py @@ -0,0 +1,58 @@ +import asyncio +import aiohttp + + +class SabnzbdApi(object): + def __init__(self, base_url, api_key): + if base_url.endswith('/'): + base_url = base_url[:-1] + self.base_url = base_url + self.api_key = api_key + self.queue = {} + + @asyncio.coroutine + def refresh_queue(self): + try: + api_args = { + 'apikey': self.api_key, + 'mode': 'queue', + 'start': '0', + 'limit': '10', + 'output': 'json' + } + + url = '{}/{}'.format(self.base_url, 'api') + with aiohttp.ClientSession() as session: + resp = yield from session.get(url, params=api_args) + json_resp = yield from resp.json() + self.queue = json_resp.get('queue') + except aiohttp.ClientError: + raise SabnzbdApiException( + "Failed to communicate with SABnzbd API.") + + @asyncio.coroutine + def check_available(self): + try: + api_args = { + 'apikey': self.api_key, + 'mode': 'qstatus', + 'output': 'json' + } + + url = '{}/{}'.format(self.base_url, 'api') + with aiohttp.ClientSession() as session: + resp = yield from session.get(url, params=api_args) + json_obj = yield from resp.json() + if not json_obj.get('status', True): + raise SabnzbdApiException( + json_obj.get('error', + 'Failed to communicate with SABnzbd API.')) + except aiohttp.ClientError: + raise SabnzbdApiException( + "Failed to communicate with SABnzbd API.") + + return True + + +class SabnzbdApiException(Exception): + pass diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..b88034e --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[metadata] +description-file = README.md diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a6a2f09 --- /dev/null +++ b/setup.py @@ -0,0 +1,17 @@ +from setuptools import setup + +setup( + name='pysabnzbd', + packages=['pysabnzbd'], + version='0.0.1', + description='Python wrapper for SABnzbd API', + author='Jerad Meisner', + author_email='jerad.meisner@gmail.com', + url='https://github.com/jeradM/pysabnzbd', + download_url='https://github.com/jeradM/pysabnzbd/archive/0.0.1.tar.gz', + keywords=['SABnzbd'], + install_requires=[ + 'aiohttp' + ], + classifiers=[] +)