Last active
October 18, 2020 22:05
-
-
Save mendel129/eb6486ce78b55e35904f753e845856ab to your computer and use it in GitHub Desktop.
python wrapper to get data from Belgium's waste pickup schedule via https://recycleapp.be
This file contains hidden or 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
import requests | |
from datetime import datetime, date, timedelta | |
import re | |
CONF_CONSUMER = "recycleapp.be" | |
CONF_APPAPI="https://recycleapp.be/api/app/v1/" | |
# CONF_DAYSINFUTURE = 14 | |
CONF_DAYSINFUTURE = 31 | |
CONF_LANG='nl' | |
CONF_USERAGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0" | |
#this is a random test address - no association to me whatsoever | |
CONF_STREET= "August Van de Wielelei" | |
CONF_STREETNR= "253" | |
CONF_POSTALC= "2100" | |
CONF_CITY= "Deurne" | |
#get the stupid token | |
#set fake user agent in header | |
headers = {"User-Agent": CONF_USERAGENT} | |
javascriptlocation=re.search(r'/static/js/main.*.chunk.js', str(requests.get("https://www.recycleapp.be", headers=headers, timeout=10).content)) | |
tokendata=re.search(r'var n="\w+"', str((requests.get("https://www.recycleapp.be"+javascriptlocation.group(0), headers=headers, timeout=10)).content)) | |
token=tokendata.group(0)[7:len(tokendata.group(0))-1] | |
headers = {"x-secret": token, "x-consumer": CONF_CONSUMER, "User-Agent": CONF_USERAGENT} | |
request = requests.get(CONF_APPAPI+"access-token", headers=headers, timeout=10) | |
token=request.json()['accessToken'] | |
#find city id | |
headers = {"Authorization": token, "x-consumer": CONF_CONSUMER, "User-Agent": CONF_USERAGENT} | |
zips = requests.get(CONF_APPAPI+"zipcodes?q="+CONF_POSTALC, headers=headers, timeout=10) | |
data=zips.json()['items'][0] | |
cityid="" | |
for name in data['names']: | |
if name[CONF_LANG] == CONF_CITY: | |
cityid=data['id'] | |
#find street id | |
params = {"q":CONF_STREET, "zipcodes":cityid} | |
street = requests.post(CONF_APPAPI+"streets", params=params, headers=headers, timeout=10) | |
data=street.json()['items'][0] | |
streetid="" | |
#todo, iterate over multiple found streets and check if in correct city | |
if data['names'][CONF_LANG] == CONF_STREET: | |
streetid=data['id'] | |
#finally get the data | |
today=datetime.today().strftime("%Y-%m-%d") | |
future=str((date.today() + timedelta(days=CONF_DAYSINFUTURE)).strftime("%Y-%m-%d")) | |
headers = {"Authorization": token, "x-consumer": CONF_CONSUMER, "User-Agent": CONF_USERAGENT} | |
finalurl=CONF_APPAPI+'collections/?zipcodeId='+cityid+'&streetId='+streetid+'&houseNumber='+CONF_STREETNR+'&fromDate='+today+'&untilDate='+future+'&size=100' | |
request = requests.get(finalurl, headers=headers, timeout=10) | |
data=request.json() | |
items = data['items'] | |
for item in items: | |
if item['type'] == 'collection': | |
fractionnl=item['fraction']['name'][CONF_LANG] | |
timestamp=item['timestamp'] | |
print("future pickup "+fractionnl+" at "+ datetime.strftime(datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ'), '%Y-%m-%d')) | |
#elfis some logic to parse special events |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment