66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
from fastapi.responses import FileResponse
|
|
|
|
from ..schemas import (
|
|
SourceListResponse,
|
|
SourceDetailResponse,
|
|
SourceParam,
|
|
)
|
|
from ..core.scheduler import SOURCE_DEFS
|
|
|
|
router = APIRouter(prefix="/sources", tags=["sources"])
|
|
|
|
SOURCES_JSON = (
|
|
Path(__file__).resolve().parent.parent.parent.parent
|
|
/ "waste_lib"
|
|
/ "custom_components"
|
|
/ "waste_collection_schedule"
|
|
/ "sources.json"
|
|
)
|
|
|
|
|
|
@router.get("", response_model=SourceListResponse)
|
|
def list_sources():
|
|
return SourceListResponse(
|
|
count=len(SOURCE_DEFS),
|
|
sources=[{"id": k, "name": v["name"]} for k, v in SOURCE_DEFS.items()],
|
|
)
|
|
|
|
|
|
@router.get("/germany")
|
|
def list_germany_sources():
|
|
"""
|
|
Return all 727 German municipalities from sources.json.
|
|
Includes title, module type, and default_params (if any).
|
|
"""
|
|
with open(SOURCES_JSON) as f:
|
|
data = json.load(f)
|
|
return {"country": "Germany", "count": len(data["Germany"]), "sources": data["Germany"]}
|
|
|
|
|
|
@router.get("/germany/search")
|
|
def search_germany_sources(q: str):
|
|
"""
|
|
Search Germany municipalities by name (case-insensitive).
|
|
"""
|
|
with open(SOURCES_JSON) as f:
|
|
data = json.load(f)
|
|
q = q.lower()
|
|
results = [s for s in data["Germany"] if q in s["title"].lower()]
|
|
return {"query": q, "count": len(results), "sources": results[:20]}
|
|
|
|
|
|
@router.get("/{source_id}", response_model=SourceDetailResponse)
|
|
def get_source(source_id: str):
|
|
if source_id not in SOURCE_DEFS:
|
|
raise HTTPException(status_code=404, detail=f"Source '{source_id}' not found")
|
|
s = SOURCE_DEFS[source_id]
|
|
return SourceDetailResponse(
|
|
id=source_id,
|
|
name=s["name"],
|
|
params=[SourceParam(**p) for p in s["params"]],
|
|
)
|