Debian Codegraph @

beetsplug/tidal/api_types.py

raw
1 from __future__ import annotations
2
3 from typing import Generic, Literal, TypedDict, TypeVar
4
5 from typing_extensions import NotRequired
6
7
8 class ResourceIdentifier(TypedDict):
9 id: str
10 type: str
11
12
13 class RelationshipLinks(TypedDict):
14 self: NotRequired[str]
15 next: NotRequired[str]
16
17
18 class RelationshipData(TypedDict):
19 data: list[ResourceIdentifier]
20 links: RelationshipLinks
21
22
23 class SingleRelationshipData(TypedDict):
24 data: ResourceIdentifier
25 links: NotRequired[RelationshipLinks]
26
27
28 class ExternalLink(TypedDict):
29 href: str
30 meta: str
31
32
33 class Copyright(TypedDict):
34 text: str
35
36
37 class ArtistAttributes(TypedDict):
38 name: str
39 popularity: float # 0.0 - 1.0, required
40 handle: NotRequired[str]
41 ownerType: NotRequired[Literal["LABEL", "USER", "MIXED"]]
42 spotlighted: NotRequired[bool]
43
44
45 class MediaAttributes(TypedDict):
46 """Describe media metadata exposed by the TIDAL API.
47
48 Combines the core values needed to identify and present a media item with
49 optional publishing, attribution, and sharing details when TIDAL provides
50 them.
51
52 While this type is not part of the original TIDAL json:api schema itself, we
53 introduced it to simplify our type definitions and allow for easier reuse of
54 shared attributes.
55 """
56
57 duration: str # ISO 8601
58 title: str
59 explicit: bool
60 mediaTags: list[str]
61 popularity: float
62
63 accessType: NotRequired[Literal["PUBLIC", "UNLISTED", "PRIVATE"]]
64 copyright: NotRequired[Copyright]
65 createdAt: NotRequired[str] # ISO 8601 datetime
66 externalLinks: NotRequired[list[ExternalLink]]
67 version: NotRequired[str]
68
69
70 class AlbumAttributes(MediaAttributes):
71 """Represent album-specific metadata returned by the TIDAL API.
72
73 Extends shared media fields with release packaging details that describe how
74 an album is published and how many items or volumes it contains.
75 """
76
77 # see "Albums_Attributes"
78 # in https://tidal-music.github.io/tidal-api-reference/tidal-api-oas.json
79
80 # Required
81 albumType: Literal["ALBUM", "EP", "SINGLE"]
82 barcodeId: str
83 numberOfItems: int
84 numberOfVolumes: int
85
86 # Optional
87 releaseDate: NotRequired[str] # ISO date YYYY-MM-DD
88
89
90 class TrackAttributes(MediaAttributes):
91 """Represent track-specific metadata returned by the TIDAL API.
92
93 Adds the musical and catalog information needed to describe an individual
94 recording, while allowing enrichment fields when TIDAL includes them.
95 """
96
97 # see "Tracks_Attributes"
98 # in https://tidal-music.github.io/tidal-api-reference/tidal-api-oas.json
99
100 # Required
101 isrc: str
102 key: Literal[
103 "UNKNOWN",
104 "C",
105 "CSharp",
106 "D",
107 "Eb",
108 "E",
109 "F",
110 "FSharp",
111 "G",
112 "Ab",
113 "A",
114 "Bb",
115 "B",
116 ]
117 keyScale: Literal[
118 "UNKNOWN",
119 "MAJOR",
120 "MINOR",
121 "AEOLIAN",
122 "BLUES",
123 "DORIAN",
124 "HARMONIC_MINOR",
125 "LOCRIAN",
126 "LYDIAN",
127 "MIXOLYDIAN",
128 "PENTATONIC_MAJOR",
129 "PHRYGIAN",
130 "MELODIC_MINOR",
131 "PENTATONIC_MINOR",
132 ]
133
134 # Optional
135 bpm: NotRequired[float]
136 spotlighted: NotRequired[bool]
137 toneTags: NotRequired[list[str]]
138
139
140 class SearchAttributes(TypedDict):
141 didYouMean: NotRequired[str]
142 trackingId: str
143
144
145 class TidalArtist(TypedDict):
146 id: str
147 type: Literal["artists"]
148 attributes: ArtistAttributes
149
150
151 class TidalAlbum(TypedDict):
152 id: str
153 type: Literal["albums"]
154 attributes: AlbumAttributes
155 relationships: dict[str, RelationshipData]
156
157
158 class TidalTrack(TypedDict):
159 id: str
160 type: Literal["tracks"]
161 attributes: TrackAttributes
162 relationships: dict[str, RelationshipData]
163
164
165 class FileMeta(TypedDict):
166 width: int
167 height: int
168
169
170 class ArtworkFile(TypedDict):
171 href: str
172 meta: FileMeta
173
174
175 class ArtworkAttributes(TypedDict):
176 mediaType: Literal["IMAGE"]
177 files: list[ArtworkFile]
178 visualMetadata: NotRequired[dict[str, str]]
179
180
181 class TidalArtwork(TypedDict):
182 id: str
183 type: Literal["artworks"]
184 attributes: ArtworkAttributes
185
186
187 class TidalSearch(TypedDict):
188 id: str
189 type: Literal["searchResults"]
190 attributes: SearchAttributes
191 relationships: dict[str, RelationshipData]
192
193
194 T = TypeVar("T")
195
196
197 class Document(TypedDict, Generic[T]):
198 data: T
199 included: NotRequired[
200 list[TidalArtist | TidalAlbum | TidalTrack | TidalArtwork]
201 ]
202 links: NotRequired[dict[str, str]]
203
204
205 AlbumDocument = Document[list[TidalAlbum]]
206 TrackDocument = Document[list[TidalTrack]]
207 SearchDocument = Document[list[TidalSearch]]