diff options
Diffstat (limited to 'pandora/models/search.py')
-rw-r--r-- | pandora/models/search.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/pandora/models/search.py b/pandora/models/search.py new file mode 100644 index 0000000..89def8e --- /dev/null +++ b/pandora/models/search.py | |||
@@ -0,0 +1,93 @@ | |||
1 | from ._base import Field, PandoraModel | ||
2 | |||
3 | |||
4 | class SearchResultItem(PandoraModel): | ||
5 | |||
6 | score = Field("score") | ||
7 | token = Field("musicToken") | ||
8 | |||
9 | @property | ||
10 | def is_song(self): | ||
11 | return isinstance(self, SongSearchResultItem) | ||
12 | |||
13 | @property | ||
14 | def is_artist(self): | ||
15 | return isinstance(self, ArtistSearchResultItem) and \ | ||
16 | self.token.startswith("R") | ||
17 | |||
18 | @property | ||
19 | def is_composer(self): | ||
20 | return isinstance(self, ArtistSearchResultItem) and \ | ||
21 | self.token.startswith("C") | ||
22 | |||
23 | @property | ||
24 | def is_genre_station(self): | ||
25 | return isinstance(self, GenreStationSearchResultItem) | ||
26 | |||
27 | def create_station(self): # pragma: no cover | ||
28 | raise NotImplementedError | ||
29 | |||
30 | @classmethod | ||
31 | def from_json(cls, api_client, data): | ||
32 | if data["musicToken"].startswith("S"): | ||
33 | return SongSearchResultItem.from_json(api_client, data) | ||
34 | elif data["musicToken"].startswith(("R", "C")): | ||
35 | return ArtistSearchResultItem.from_json(api_client, data) | ||
36 | elif data["musicToken"].startswith("G"): | ||
37 | return GenreStationSearchResultItem.from_json(api_client, data) | ||
38 | else: | ||
39 | raise NotImplementedError("Unknown result token type '{}'" | ||
40 | .format(data["musicToken"])) | ||
41 | |||
42 | |||
43 | class ArtistSearchResultItem(SearchResultItem): | ||
44 | |||
45 | score = Field("score") | ||
46 | token = Field("musicToken") | ||
47 | artist = Field("artistName") | ||
48 | likely_match = Field("likelyMatch", default=False) | ||
49 | |||
50 | def create_station(self): | ||
51 | self._api_client.create_station(artist_token=self.token) | ||
52 | |||
53 | @classmethod | ||
54 | def from_json(cls, api_client, data): | ||
55 | return super(SearchResultItem, cls).from_json(api_client, data) | ||
56 | |||
57 | |||
58 | class SongSearchResultItem(SearchResultItem): | ||
59 | |||
60 | score = Field("score") | ||
61 | token = Field("musicToken") | ||
62 | artist = Field("artistName") | ||
63 | song_name = Field("songName") | ||
64 | |||
65 | def create_station(self): | ||
66 | self._api_client.create_station(track_token=self.token) | ||
67 | |||
68 | @classmethod | ||
69 | def from_json(cls, api_client, data): | ||
70 | return super(SearchResultItem, cls).from_json(api_client, data) | ||
71 | |||
72 | |||
73 | class GenreStationSearchResultItem(SearchResultItem): | ||
74 | |||
75 | score = Field("score") | ||
76 | token = Field("musicToken") | ||
77 | station_name = Field("stationName") | ||
78 | |||
79 | def create_station(self): | ||
80 | self._api_client.create_station(search_token=self.token) | ||
81 | |||
82 | @classmethod | ||
83 | def from_json(cls, api_client, data): | ||
84 | return super(SearchResultItem, cls).from_json(api_client, data) | ||
85 | |||
86 | |||
87 | class SearchResult(PandoraModel): | ||
88 | |||
89 | nearest_matches_available = Field("nearMatchesAvailable") | ||
90 | explanation = Field("explanation") | ||
91 | songs = Field("songs", model=SongSearchResultItem) | ||
92 | artists = Field("artists", model=ArtistSearchResultItem) | ||
93 | genre_stations = Field("genreStations", model=GenreStationSearchResultItem) | ||