-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_foods.py
More file actions
39 lines (31 loc) · 974 Bytes
/
export_foods.py
File metadata and controls
39 lines (31 loc) · 974 Bytes
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
import requests
import json
import os
from dotenv import load_dotenv
load_dotenv()
# Konfiguration
MEALIE_URL = os.getenv("MEALIE_URL")
API_KEY = os.getenv("API_KEY")
EXPORT_FILE = "mealie_foods_export.json"
headers = {
"Authorization": f"Bearer {API_KEY}",
"accept": "application/json"
}
all_foods = []
page = 1
per_page = 50 # oder mehr, je nach API-Limit
while True:
params = {"page": page, "perPage": per_page}
response = requests.get(f"{MEALIE_URL}/api/foods", headers=headers, params=params)
response.raise_for_status()
data = response.json()
items = data.get("items") or data.get("data") or data # je nach API-Version
if not items:
break
all_foods.extend(items)
if len(items) < per_page:
break
page += 1
with open(EXPORT_FILE, "w", encoding="utf-8") as f:
json.dump(all_foods, f, indent=2, ensure_ascii=False)
print(f"Erfolgreich exportiert: {len(all_foods)} Lebensmittel in {EXPORT_FILE}")