From 69c220cda3d8c0a95327630f5752dad36cb82261 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Tue, 17 Jul 2018 12:34:04 +0200 Subject: Squashed commit --- jobs/tankerkoenig.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 jobs/tankerkoenig.py (limited to 'jobs/tankerkoenig.py') diff --git a/jobs/tankerkoenig.py b/jobs/tankerkoenig.py new file mode 100755 index 0000000..e78853d --- /dev/null +++ b/jobs/tankerkoenig.py @@ -0,0 +1,36 @@ +import codecs +import json +import logging +import typing +from collections import namedtuple +from urllib import request + +Data = namedtuple('Data', ['name', 'id', 'type', 'price']) + + +URL = "https://creativecommons.tankerkoenig.de/json/list.php?lat={lat}&lng={lng}&rad={rad}&sort=dist&type=all&apikey={api_key}" + + +def execute(api_key : str, lat: float, lng : float, rad: float) -> typing.Iterable[Data]: + url = URL.format(api_key=api_key, rad=rad, lat=lat, lng=lng) + r = request.Request(url) + try: + with request.urlopen(r) as f: + f2 = codecs.getreader('utf-8')(f) + data = json.load(f2) + if not data['status'] == 'ok': + raise Exception("Error %s", data['message']) + for station in data['stations']: + name = "{} - {} - {}".format(station['place'], station['brand'], station['name']) + if not station['isOpen'] == True: + continue + + if "diesel" in station: + yield Data(name, station['id'], 'Diesel', station['diesel']) + if "e5" in station: + yield Data(name, station['id'], 'SP95-E5', station['e5']) + if "e10" in station: + yield Data(name, station['id'], 'SP95-E10', station['e10']) + except Exception as e: + logging.error("Failed for: %f %f %f", lat, lng, rad) + raise e -- cgit v1.2.1