-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdawarich_client.py
More file actions
58 lines (50 loc) · 2.13 KB
/
Copy pathdawarich_client.py
File metadata and controls
58 lines (50 loc) · 2.13 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
import requests
import pytz
from datetime import datetime
class DawarichClient:
"""Ein Client für die Interaktion mit der dawarich API."""
def __init__(self, base_url: str, api_key: str):
self.base_url = base_url.rstrip('/')
self.api_key = api_key
self.session = requests.Session()
def get_points(self, start_time: datetime, end_time: datetime) -> list:
"""Holt Standortpunkte in einem bestimmten Zeitfenster."""
api_url = f"{self.base_url}/api/v1/points"
# Versuche verschiedene Parameter-Formate
# Format 1: Unix timestamps mit since/until
params_v1 = {
"api_key": self.api_key,
"since": int(start_time.timestamp()),
"until": int(end_time.timestamp()),
"order": "asc"
}
# Format 2: ISO-Format mit start_at/end_at (wie die Web-UI)
params_v2 = {
"api_key": self.api_key,
"start_at": start_time.strftime('%Y-%m-%dT%H:%M'),
"end_at": end_time.strftime('%Y-%m-%dT%H:%M'),
"order": "asc"
}
try:
# Versuche erst das ISO-Format (wie die Web-UI)
response = self.session.get(api_url, params=params_v2)
response.raise_for_status()
data = response.json()
if isinstance(data, list) and len(data) > 0:
return data
elif isinstance(data, dict) and "results" in data and len(data["results"]) > 0:
return data["results"]
else:
# Fallback auf since/until
response = self.session.get(api_url, params=params_v1)
response.raise_for_status()
data = response.json()
if isinstance(data, list):
return data
elif isinstance(data, dict) and "results" in data:
return data["results"]
else:
return []
except requests.RequestException as e:
print(f"Fehler bei der Abfrage von dawarich: {e}")
return []