1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
"""
Wrapper to get ROVA calendar from Rova's API
Acces to this ROVA API has been simplified since version 0.2.1 of this wrapper
Just use https://www.rova.nl/api/waste-calendar/upcoming?postalcode=1000AA&houseNumber=1&addition=&take=5
with a existing combination of postalcode, housenumber, housenumber addition
Be aware that this API has not been officially published by ROVA.
"""
from datetime import datetime
import random
import requests
__title__ = "rova"
__version__ = "0.4.1"
__author__ = "Gido Hakvoort and synoniem <synoniem@hotmail.com>"
__license__ = "MIT"
class Rova:
"""
ROVA class
"""
def __init__(self, zip_code, house_number, house_addition=""):
"""
To fetch the garbage calendar, you need to set a zip_code and house_number.
"""
self.zip_code = zip_code.replace(' ', '')
self.house_number = house_number.strip()
self.house_addition = house_addition.strip()
def is_rova_area(self):
"""
Check if ROVA collects garbage at this address
"""
url = 'https://www.rova.nl/api/waste-calendar/upcoming'
# request data from rova API and check if garbage is collected at this address
# requesting with a non-existing postalcode will result in a error message
response = requests.get(url, params={
'postalcode': self.zip_code,
'houseNumber': self.house_number,
'addition': self.house_addition,
'take': '1',
})
response.raise_for_status()
rova_response = response.text.strip()
if rova_response != '[]':
rova_response = "OK"
return rova_response == "OK"
def get_calendar_items(self, take=5):
"""
Get next pickup date for each garbage types
"""
url = 'https://www.rova.nl/api/waste-calendar/upcoming'
# request data from rova API and save response first 5 items (default)
response = requests.get(url, params={
'postalcode': self.zip_code,
'houseNumber': self.house_number,
'addition': self.house_addition,
'take': take,
})
response.raise_for_status()
rova_response = response.json()
items = []
types = []
# add next pickup date for each garbage type
for item in rova_response:
date = datetime.strptime(item["date"], "%Y-%m-%dT%H:%M:%SZ")
date = date.strftime("%Y-%m-%dT%H:%M:%S")
garbage_type = item["wasteType"]["code"].upper()
# Breaking API change, so fix for upstream
if garbage_type in {"PAP", "LOSPAP"}:
garbage_type = "PAPIER"
if garbage_type == "RST":
garbage_type = "RESTAFVAL"
items.append({
'GarbageTypeCode': garbage_type,
'Date': date
})
types.append(garbage_type)
return items
|