Petrol Prices API

In Germany petrol stations have to regularly share the price of their products with a government agency. In addition public APIs were created, which give close to realtime access to the prices. Here a little example on how to fetch and use the data.

API Access

There are various providers offering access to the data. After looking through a few of them, I ended up giving tankerkönig a try. After using it for a few months, well, it’s stable and does the job. All you need is an API key and the name of the petrol stations you’d like to monitor. Each petrol station is then resolved into a UUID. Together you can do a simple GET request: Plain

https://creativecommons.tankerkoenig.de/json/prices.php?ids=' + ids + '&apikey=' + tk_apikey

With my local petrol stations.

https://creativecommons.tankerkoenig.de/json/prices.php?ids=51d4b4aa-a095-1aa0-e100-80009459e03a,875f0201-0dc7-4798-a67f-61804ec83cfe,fd99dec4-4579-4bd7-7cd4-0658fc3b5d33,b3838dc2-1d4d-4878-b949-2199f0f2f231,2af748f1-b663-44a2-a62c-72a96408d658,4c746120-5b7d-40ca-8c75-ccd55c4d0814&apikey=-

The result is:

{
    "ok": true,
    "license": "CC BY 4.0 -  https:\\/\\/creativecommons.tankerkoenig.de",
    "data": "MTS-K",
    "prices": {
        "51d4b4aa-a095-1aa0-e100-80009459e03a": {
            "status": "open",
            "e5": 1.819,
            "e10": 1.759,
            "diesel": 1.639
        },
        "2af748f1-b663-44a2-a62c-72a96408d658": {
            "status": "open",
            "e5": 1.839,
            "e10": 1.779,
            "diesel": 1.649
        },
        "4c746120-5b7d-40ca-8c75-ccd55c4d0814": {
            "status": "open",
            "e5": 1.819,
            "e10": 1.759,
            "diesel": 1.639
        },
        "b3838dc2-1d4d-4878-b949-2199f0f2f231": {
            "status": "open",
            "e5": 1.829,
            "e10": 1.769,
            "diesel": 1.659
        },
        "fd99dec4-4579-4bd7-7cd4-0658fc3b5d33": {
            "status": "open",
            "e5": 1.839,
            "e10": 1.779,
            "diesel": 1.679
        },
        "875f0201-0dc7-4798-a67f-61804ec83cfe": {
            "status": "open",
            "e5": 1.839,
            "e10": 1.779,
            "diesel": 1.659
        }
    }
}

All in All

Running an InfluxDB and Grafana , I decided to combine everything there. The script is called as cronjob and regularly fetches the data, places it in the InfluxDB and Grafana does the rest.

Dashboard


#!/usr/bin/python3

import json
import requests
from datetime import datetime
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS

proxies = {
  'http': '-',
  'https': '-',
}

token = "-"
org = "-"
bucket = "fuel"

client = InfluxDBClient(url="http://-:8086", token=token)
write_api = client.write_api(write_options=SYNCHRONOUS)

tk_apikey = '-'
ps = {'51d4b4aa-a095-1aa0-e100-80009459e03a': 'Jet Barkhausen', '875f0201-0dc7-4798-a67f-61804ec83cfe': 'Total Barkhausen', 'fd99dec4-4579-4bd7-7cd4-0658fc3b5d33': 'Jantzon Roecke', 'b3838dc2-1d4d-4878-b949-2199f0f2f231': 'Total Bad Oeyenhausen', '2af748f1-b663-44a2-a62c-72a96408d658': 'Westfalen Hausberge', '4c746120-5b7d-40ca-8c75-ccd55c4d0814': 'Oil Meissen' }

ids = ""
for pst in ps:
    ids += pst + ','
ids = ids[:-1]
url = 'https://creativecommons.tankerkoenig.de/json/prices.php?ids=' + ids + '&apikey=' + tk_apikey
r = requests.get(url, proxies=proxies)
raw = r.content
j = json.loads(raw)
ft = {'e5':'Benzin E5', 'e10':'Benzin E10', 'diesel':'Diesel'}
sequence = []

for pst in ps:
    if pst in j['prices']:
        if j['prices'][pst]['status'] == 'open':
            for ftt in ft:
                if ftt in j['prices'][pst]:
                    sequence.append('price,petrolstation=' + ps[pst].replace(' ','_') + ' ' + ftt + '=' + str(j['prices'][pst][ftt]))

write_api.write(bucket, org, sequence)