Home Assistant Unofficial Reference 2024.12.1
util.py
Go to the documentation of this file.
1 """Utils for Spotify."""
2 
3 from __future__ import annotations
4 
5 from spotifyaio import Image
6 import yarl
7 
8 from .const import MEDIA_PLAYER_PREFIX
9 
10 
11 def is_spotify_media_type(media_content_type: str) -> bool:
12  """Return whether the media_content_type is a valid Spotify media_id."""
13  return media_content_type.startswith(MEDIA_PLAYER_PREFIX)
14 
15 
16 def resolve_spotify_media_type(media_content_type: str) -> str:
17  """Return actual spotify media_content_type."""
18  return media_content_type.removeprefix(MEDIA_PLAYER_PREFIX)
19 
20 
21 def fetch_image_url(images: list[Image]) -> str | None:
22  """Fetch image url."""
23  if not images:
24  return None
25  return images[0].url
26 
27 
28 def spotify_uri_from_media_browser_url(media_content_id: str) -> str:
29  """Extract spotify URI from media browser URL."""
30  if media_content_id and media_content_id.startswith(MEDIA_PLAYER_PREFIX):
31  parsed_url = yarl.URL(media_content_id)
32  media_content_id = parsed_url.name
33  return media_content_id
bool is_spotify_media_type(str media_content_type)
Definition: util.py:11
str resolve_spotify_media_type(str media_content_type)
Definition: util.py:16
str|None fetch_image_url(list[Image] images)
Definition: util.py:21
str spotify_uri_from_media_browser_url(str media_content_id)
Definition: util.py:28