summaryrefslogtreecommitdiffstats
path: root/europastats/attractions.py
blob: 9a6c087a6a18191ff714bee85b4f3c47211cc2bc (plain)
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
#!/usr/bin/env python
"""
    takes a list of openweathermap ids and a config dict with
    "openweathermap_apikey"
"""
from datetime import datetime as dt
import sys
import json
import requests

import json
from os.path import join,dirname,abspath
name = "waitingtimes"

status_codes = [
    "Open",
    "90Plus",
    "Closed",
    "Temporariy Closed",
    "Closed due to Weather",
    "Special Tour"
]

api_url = "https://api.europapark.de/api-5.4/"

def get_db(id):
    with open(abspath(join(dirname(__name__),"europastats/data",id+".json")),'rb') as f:
        return json.load(f)

name_mapping = { a["code"]: a for a in get_db("attractions") }


def gen_token():
    from binascii import hexlify
    import hmac
    import hashlib
    import base64
    secret = "ZhQCqoZp"
    message= dt.utcnow().strftime("%Y%m%d%H")
    dig = hmac.new(secret.encode("utf-8"), msg=message.encode("utf-8"), digestmod=hashlib.sha256).digest()
    return hexlify(dig).upper().decode()

def get_live(page):
    code = gen_token()
    headers = requests.utils.default_headers()
    headers.update( { "User-Agent": "okhttp/2.7.5", })
    resp = requests.get( api_url + page,
                    params = {"mock":"false","token":code},
                    headers = headers).json()
    return resp



def get_data(mock=False):
    now = dt.utcnow()
    if mock:
        resp = get_db(name)
    else:
        resp = get_live("waitingtimes")
    for v in resp:
        data = {}
        data["status"] = status_codes[v["status"]]
        data["_name"] = "Attraktion {}".format( name_mapping[v["code"]]["nameGerman"])
        data["_id"] = "europapark-{}-{}".format(name,v["code"])
        data["waitingtime"] = v["time"]
        data["_ts"] = now.isoformat(timespec="seconds") + "Z"
        data["_db"] = name
        yield data

def main():
    kvt = get_data()
    print(json.dumps(list(kvt)))

def get_mock_data():
    return get_data(True)

if __name__ == "__main__":
    main()