Initial commit: Javis project

This commit is contained in:
Javis committed 2026-09-21 21:44:00 +02:00
commit 435c645f36
164 files changed
+35279

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
"""Async client for the Emby API."""
from __future__ import annotations
import logging
from typing import Any
import httpx
from .config import Settings
log = logging.getLogger(__name__)
class EmbyClient:
"""Thin async wrapper around Emby's REST API."""
def __init__(self, settings: Settings) -> None:
self.settings = settings
self._http = httpx.AsyncClient(
base_url=settings.base_url,
headers={
"X-Emby-Token": settings.api_key,
"Accept": "application/json",
},
timeout=settings.timeout,
)
async def aclose(self) -> None:
await self._http.aclose()
# ---- helpers ----------------------------------------------------------
async def _get(self, path: str, params: dict[str, Any] | None = None) -> Any:
r = await self._http.get(path, params=params)
r.raise_for_status()
return r.json()
async def _post(
self, path: str, json: dict[str, Any] | None = None, params: dict[str, Any] | None = None
) -> Any:
r = await self._http.post(path, json=json, params=params)
r.raise_for_status()
if r.content:
try:
return r.json()
except ValueError:
return None
return None
async def _delete(self, path: str) -> Any:
r = await self._http.delete(path)
r.raise_for_status()
if r.content:
try:
return r.json()
except ValueError:
return None
return None
# ---- API: Items (media library) ---------------------------------------
async def list_items(
self,
parent_id: str | None = None,
include_related: bool = False,
recursive: bool = True,
sort_by: str | None = "Name",
sort_order: str = "Ascending",
limit: int | None = None,
) -> dict[str, Any]:
"""List items from the media library."""
params: dict[str, Any] = {
"IncludeRelated": str(include_related).lower(),
"Recursive": str(recursive).lower(),
"SortBy": sort_by,
"SortOrder": sort_order,
}
if parent_id:
params["ParentId"] = parent_id
if limit:
params["Limit"] = limit
return await self._get("/Items", params=params)
async def get_item(self, item_id: str) -> dict[str, Any]:
"""Get details for a specific item."""
return await self._get(f"/Items/{item_id}")
# ---- API: Users -------------------------------------------------------
async def list_users(self) -> dict[str, Any]:
"""List all users on the server."""
return await self._get("/Users")
async def get_user(self, user_id: str) -> dict[str, Any]:
"""Get details for a specific user."""
return await self._get(f"/Users/{user_id}")
# ---- API: Sessions ----------------------------------------------------
async def list_sessions(self) -> list[dict[str, Any]]:
"""List active sessions (connected devices/apps)."""
return await self._get("/Sessions")
async def get_system_info(self) -> dict[str, Any]:
"""Get server system info."""
return await self._get("/System/Info")
# ---- API: Playback ----------------------------------------------------
async def play_item(
self, item_id: str, user_id: str | None = None, autoplay: bool = True
) -> Any:
"""Start playback of an item."""
params: dict[str, Any] = {"UserId": user_id or "null", "Autostart": autoplay}
return await self._post(f"/Items/{item_id}/Play", params=params)
async def get_playback_info(self, item_id: str, user_id: str | None = None) -> dict[str, Any]:
"""Get playback info for an item (transcoding options, etc.)."""
params = {}
if user_id:
params["UserId"] = user_id
return await self._get(f"/Items/{item_id}/PlaybackInfo", params=params)
async def get_playback_url(self, item_id: str) -> dict[str, Any]:
"""Get a stream URL for direct playback."""
return await self._get(f"/Items/{item_id}/StreamUrl")
# ---- API: Device Info -------------------------------------------------
async def list_devices(self) -> list[dict[str, Any]]:
"""List registered devices."""
r = await self._get("/Devices")
return r if isinstance(r, list) else []