-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
127 lines (101 loc) · 3.58 KB
/
models.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""Models for the Radio Browser API."""
from __future__ import annotations
from datetime import datetime
from typing import Optional, cast
import pycountry
from awesomeversion import AwesomeVersion
from pydantic import BaseModel, Field, validator
class Stats(BaseModel):
"""Object holding the Radio Browser stats."""
supported_version: int
software_version: AwesomeVersion
status: str
stations: int
stations_broken: int
tags: int
clicks_last_hour: int
clicks_last_day: int
languages: int
countries: int
class Station(BaseModel):
"""Object information for a station from the Radio Browser."""
bitrate: int
change_uuid: str = Field(..., alias="changeuuid")
click_count: int = Field(..., alias="clickcount")
click_timestamp: Optional[datetime] = Field(..., alias="clicktimestamp_iso8601")
click_trend: int = Field(..., alias="clicktrend")
codec: str
country_code: str = Field(..., alias="countrycode")
favicon: str
latitude: Optional[float] = Field(..., alias="geo_lat")
longitude: Optional[float] = Field(..., alias="geo_long")
has_extended_info: bool
hls: bool
homepage: str
iso_3166_2: Optional[str]
language: list[str]
language_codes: list[str] = Field(..., alias="languagecodes")
lastchange_time: Optional[datetime] = Field(..., alias="lastchangetime_iso8601")
lastcheckok: bool
last_check_ok_time: Optional[datetime] = Field(..., alias="lastcheckoktime_iso8601")
last_check_time: Optional[datetime] = Field(..., alias="lastchecktime_iso8601")
last_local_check_time: Optional[datetime] = Field(
..., alias="lastlocalchecktime_iso8601"
)
name: str
ssl_error: int
state: str
uuid: str = Field(..., alias="stationuuid")
tags: list[str]
url_resolved: str
url: str
votes: int
@validator("tags", "language", "language_codes", pre=True)
@classmethod
def _split(cls, value: str) -> list[str]: # noqa: F841
"""Split comma separated string into a list of strings.
Arguments:
value: Comma separated string.
Returns:
List of strings.
"""
return [item.strip() for item in value.split(",")]
@property
def country(self) -> str | None:
"""Return country name of this station.
Returns:
Country name or None if no country code is set.
"""
if resolved_country := pycountry.countries.get(alpha_2=self.country_code):
return cast(str, resolved_country.name)
return None
class Country(BaseModel):
"""Object information for a Counbtry from the Radio Browser."""
code: str
name: str
station_count: str = Field(..., alias="stationcount")
@property
def favicon(self) -> str:
"""Return the favicon URL for the country.
Returns:
URL to the favicon.
"""
return f"https://flagcdn.com/256x192/{self.code.lower()}.png"
class Language(BaseModel):
"""Object information for a Language from the Radio Browser."""
code: Optional[str] = Field(..., alias="iso_639")
name: str
station_count: str = Field(..., alias="stationcount")
@property
def favicon(self) -> str | None:
"""Return the favicon URL for the language.
Returns:
URL to the favicon.
"""
if self.code:
return f"https://flagcdn.com/256x192/{self.code.lower()}.png"
return None
class Tag(BaseModel):
"""Object information for a Tag from the Radio Browser."""
name: str
station_count: str = Field(..., alias="stationcount")