-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathns_api_EDA.py
More file actions
72 lines (64 loc) · 2.04 KB
/
ns_api_EDA.py
File metadata and controls
72 lines (64 loc) · 2.04 KB
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
# %%
import requests
import os
from dotenv import load_dotenv, find_dotenv
import pandas as pd
from datetime import date
# %%
_ = load_dotenv(find_dotenv())
# %%
hdr = {
# Request headers
"Cache-Control": "no-cache",
"Ocp-Apim-Subscription-Key": os.environ.get("NS_APP_PRIMARY"),
}
url = "https://gateway.apiportal.ns.nl/reisinformatie-api/api/v3/disruptions?isActive=false"
response = requests.get(url, headers=hdr)
# %%
df = pd.DataFrame(
[
(x["id"], x["title"], x["start"], x["end"], x["timespans"][0]["cause"]["label"])
for x in response.json()
],
columns=["id", "title", "start", "end", "cause"],
).assign(
**{
"start": lambda x: pd.to_datetime(x["start"]),
"end": lambda x: pd.to_datetime(x["end"]),
"duration_minutes": lambda x: (x["end"] - x["start"]).dt.total_seconds() / 60,
}
)
# %%
amount_disruptions_NS = (
df.loc[lambda x: x["start"].dt.date == date.today(), :]
.loc[lambda x: x["end"].dt.date == date.today(), "duration_minutes"]
.sum()
)
# %%
def get_amount_disruptions_NS():
hdr = {
# Request headers
"Cache-Control": "no-cache",
"Ocp-Apim-Subscription-Key": os.environ.get("NS_APP_PRIMARY"),
}
url = "https://gateway.apiportal.ns.nl/reisinformatie-api/api/v3/disruptions?isActive=false"
response = requests.get(url, headers=hdr)
df = pd.DataFrame(
[
(x["id"], x["title"], x["start"], x["end"], x["timespans"][0]["cause"]["label"])
for x in response.json()
],
columns=["id", "title", "start", "end", "cause"],
).assign(
**{
"start": lambda x: pd.to_datetime(x["start"]),
"end": lambda x: pd.to_datetime(x["end"]),
"duration_minutes": lambda x: (x["end"] - x["start"]).dt.total_seconds() / 60,
}
)
amount_disruptions_NS = (
df.loc[lambda x: x["start"].dt.date == date.today(), :]
.loc[lambda x: x["end"].dt.date == date.today(), "duration_minutes"]
.sum()
)
return round(amount_disruptions_NS, 2)