forked from misialq/openerz-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request misialq#1 from misialq/DEV-1
DEV-1: first working version
- Loading branch information
Showing
11 changed files
with
488 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Run the tests | ||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: [3.6, 3.7, 3.8] | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v1 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Set up PYTHONPATH | ||
run: | | ||
echo "::set-env name=PYTHONPATH::$PYTHONPATH:/home/runner/work/openerz-api" | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
- name: Lint with flake8 | ||
run: | | ||
pip install flake8 | ||
# stop the build if there are Python syntax errors or undefined names | ||
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics | ||
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide | ||
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics | ||
- name: Test with pytest | ||
run: | | ||
pip install pytest testfixtures | ||
pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
bin/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
.tox/ | ||
.coverage | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
|
||
# Translations | ||
*.mo | ||
|
||
# Mr Developer | ||
.mr.developer.cfg | ||
.project | ||
.pydevproject | ||
|
||
# Rope | ||
.ropeproject | ||
|
||
# Django stuff: | ||
*.log | ||
*.pot | ||
|
||
# Sphinx documentation | ||
docs/_build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) [2020] [Michał Ziemski] | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
### OpenERZ Python API | ||
|
||
This wrapper allows you to interact with the OpenERZ API using Python code. | ||
|
||
For more information about the API itself see: [http://openerz.metaodi.ch/documentation](http://openerz.metaodi.ch/documentation) and [https://github.com/metaodi/openerz](https://github.com/metaodi/openerz) | ||
|
||
#### Usage example | ||
|
||
A ready-to-run example can also be found in the `examples` directory. | ||
|
||
``` | ||
from openerz_api.main import OpenERZConnector | ||
ZIP = 8001 | ||
WASTE = "paper" | ||
# initialize the connector given zip code and waste type | ||
connector = OpenERZConnector(zip_code=ZIP, waste_type=WASTE) | ||
# retrieve the next pick-up date within a period of choice | ||
next_pickup = connector.find_next_pickup(day_offset=15) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from openerz_api.main import OpenERZConnector | ||
|
||
ZIP = 8001 | ||
WASTE = "paper" | ||
|
||
# initialize the connector given zip code and waste type | ||
connector = OpenERZConnector(zip_code=ZIP, waste_type=WASTE) | ||
|
||
# retrieve the next pick-up date within a period of choice | ||
next_pickup = connector.find_next_pickup(day_offset=15) | ||
|
||
print(f"Next pickup date for {WASTE} in {ZIP} area: {next_pickup}") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import logging | ||
import requests | ||
from datetime import datetime, timedelta | ||
|
||
|
||
class OpenERZConnector: | ||
"""A simple connector to interact with OpenERZ API.""" | ||
|
||
def __init__(self, zip_code, waste_type): | ||
"""Initialize the API connector. | ||
Args: | ||
zip_code (int): post code of the area of interest | ||
waste_type (str): type of waste to be picked up (paper/cardboard/waste/cargotram/etram/organic/textile) | ||
""" | ||
self.zip = zip_code | ||
self.waste_type = waste_type | ||
self.start_date = datetime.now() | ||
self.end_date = None | ||
self.last_api_response = None | ||
self.logger = logging.getLogger(__name__) | ||
|
||
def update_start_date(self): | ||
"""Set the start day to today.""" | ||
|
||
self.start_date = datetime.now() | ||
|
||
def find_end_date(self, day_offset=31): | ||
"""Find the end date for the request, given an offset expressed in days. | ||
Args: | ||
day_offset (int): difference in days between start and end date of the request | ||
""" | ||
|
||
self.end_date = self.start_date + timedelta(days=day_offset) | ||
|
||
def make_api_request(self): | ||
"""Construct a request and send it to the OpenERZ API.""" | ||
|
||
headers = {"accept": "application/json"} | ||
|
||
start_date = self.start_date.strftime("%Y-%m-%d") | ||
end_date = self.end_date.strftime("%Y-%m-%d") | ||
|
||
payload = { | ||
"zip": self.zip, | ||
"start": start_date, | ||
"end": end_date, | ||
"offset": 0, | ||
"limit": 0, | ||
"lang": "en", | ||
"sort": "date", | ||
} | ||
url = f"http://openerz.metaodi.ch/api/calendar/{self.waste_type}.json" | ||
|
||
try: | ||
self.last_api_response = requests.get(url, params=payload, headers=headers) | ||
except requests.exceptions.RequestException as connection_error: | ||
self.logger.error("RequestException while making request to OpenERZ: %s", connection_error) | ||
|
||
def parse_api_response(self): | ||
"""Parse the JSON response received from the OpenERZ API and return a date of the next pickup.""" | ||
|
||
if not self.last_api_response.ok: | ||
self.logger.warning( | ||
"Last request to OpenERZ was not successful. Status code: %d", self.last_api_response.status_code, | ||
) | ||
return None | ||
|
||
response_json = self.last_api_response.json() | ||
if response_json["_metadata"]["total_count"] == 0: | ||
self.logger.warning("Request to OpenERZ returned no results.") | ||
return None | ||
result_list = response_json.get("result") | ||
first_scheduled_pickup = result_list[0] | ||
if first_scheduled_pickup["zip"] == self.zip and first_scheduled_pickup["type"] == self.waste_type: | ||
return first_scheduled_pickup["date"] | ||
self.logger.warning("Either zip or waste type does not match the ones specified in the configuration.") | ||
return None | ||
|
||
def find_next_pickup(self, day_offset=31): | ||
"""Find the next pickup date within the next X days, given zip_code and waste type | ||
Args: | ||
day_offset (int): difference in days between start and end date of the request | ||
""" | ||
|
||
self.update_start_date() | ||
self.find_end_date(day_offset=day_offset) | ||
self.make_api_request() | ||
next_pickup_date = self.parse_api_response() | ||
return next_pickup_date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requests ~= 2.23.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import setuptools | ||
|
||
with open("README.md", "r") as fh: | ||
long_description = fh.read() | ||
|
||
setuptools.setup( | ||
name="openerz-api", | ||
version="0.1.0", | ||
author="Michał Ziemski", | ||
author_email="michal@terrestryal.com", | ||
description="A Python wrapper around the OpenERZ API.", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/misialq/openerz-api", | ||
packages=setuptools.find_packages(), | ||
classifiers=[ | ||
"Programming Language :: Python :: 3", | ||
"License :: OSI Approved :: MIT License", | ||
"Operating System :: OS Independent", | ||
], | ||
install_requires=["requests",], | ||
python_requires=">=3.6", | ||
) |
Binary file not shown.
Oops, something went wrong.