summaryrefslogtreecommitdiffstats
path: root/europastats/attractions.py
diff options
context:
space:
mode:
Diffstat (limited to 'europastats/attractions.py')
-rw-r--r--europastats/attractions.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/europastats/attractions.py b/europastats/attractions.py
new file mode 100644
index 0000000..9a6c087
--- /dev/null
+++ b/europastats/attractions.py
@@ -0,0 +1,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()