Last active
February 18, 2020 08:45
-
-
Save ChrLipp/0c7be62d06ff96b9293cfe71311f563d to your computer and use it in GitHub Desktop.
Python script to fetch the BMV Abholtermine (https://www.bmv.at/service/muellabfuhrtermine.html). Target: include this in home assistant.
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
from urllib import request, parse | |
from bs4 import BeautifulSoup | |
from datetime import date, datetime | |
# Checks if the given string is a valid date in the future (or today). | |
def parse_date(date_string): | |
result: date = None | |
if ', ' in date_string: | |
parts = date_string.split(', ') | |
parsed_date = datetime.strptime(parts[1], '%d.%m.%Y').date() | |
if parsed_date >= date.today(): | |
result = parsed_date | |
return result | |
# Select all table columns with a date in the future. | |
def get_text_from_tds(elements): | |
result = list() | |
for element in elements: | |
parsed_date = parse_date(element.text) | |
if parsed_date is not None: | |
result.append(parsed_date) | |
return result | |
# Calls the servlet with the given parameters. | |
def get_servlet(parms): | |
# build URI on given parameters | |
servletUrl = 'https://webudb.udb.at/WasteManagementUDB/WasteManagementServlet' | |
querystring = parse.urlencode(parms) | |
# execute request | |
u = request.urlopen(servletUrl + '?' + querystring) | |
resp = u.read() | |
soup = BeautifulSoup(resp, 'html.parser') | |
return soup | |
# Entry function | |
def main(): | |
# call 1, get session id | |
params1 = { | |
'SubmitAction': 'wasteDisposalServices' | |
} | |
doc = get_servlet(params1) | |
session = doc.select('form #SessionId')[0] | |
# call 2, set city | |
paramsMap = { | |
'SubmitAction': 'citiesEvent', | |
'InFrameMode': 'TRUE', | |
'Ort': '<Ort>', | |
'Strasse': '<Strasse>', | |
'Hausnummer': '<Hausnummer>', | |
'ApplicationName': 'com.athos.kd.udb.CheckAbfuhrTermineParameterBusinessCase', | |
session['name']: session['value'] | |
} | |
doc = get_servlet(paramsMap) | |
# call 3, fetch dates | |
paramsMap['SubmitAction'] = 'forward' | |
doc = get_servlet(paramsMap) | |
restmuellSelection = doc.find("table", {"name": "RestmuellTermineLIST"}).select('td') | |
restmuell = get_text_from_tds(restmuellSelection) | |
gelberSackSelection = doc.find("table", {"name": "DSDTermineLIST"}).select('td') | |
gelberSack = get_text_from_tds(gelberSackSelection) | |
papierSelection = doc.find("table", {"name": "PapierTermineLIST"}).select('td') | |
papier = get_text_from_tds(papierSelection) | |
# output result | |
print("Nächster Restmüll: ", restmuell[0]) | |
print("Nächster gelber Sack: ", gelberSack[0]) | |
print("Nächster Papier: ", papier[0]) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment