diff options
author | John Cass <john.cass77@gmail.com> | 2016-06-07 07:23:57 +0200 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2016-06-06 22:23:57 -0700 |
commit | 79af8c5a61de0feb54bd14c17fb81dd742c04e2c (patch) | |
tree | b95360a631e9565920081e061230d7c388c27375 /pandora | |
parent | b414e2c38b8bc13bafed500b656aa68be0db820c (diff) | |
download | pydora-79af8c5a61de0feb54bd14c17fb81dd742c04e2c.tar.bz2 pydora-79af8c5a61de0feb54bd14c17fb81dd742c04e2c.tar.xz pydora-79af8c5a61de0feb54bd14c17fb81dd742c04e2c.zip |
Add support for searching genre stations. (#45)
Add support for searching genre stations.
Diffstat (limited to 'pandora')
-rw-r--r-- | pandora/client.py | 14 | ||||
-rw-r--r-- | pandora/models/pandora.py | 83 |
2 files changed, 82 insertions, 15 deletions
diff --git a/pandora/client.py b/pandora/client.py index 7c9791e..48077cb 100644 --- a/pandora/client.py +++ b/pandora/client.py | |||
@@ -174,12 +174,18 @@ class APIClient(BaseAPIClient): | |||
174 | return self("bookmark.deleteArtistBookmark", | 174 | return self("bookmark.deleteArtistBookmark", |
175 | bookmarkToken=bookmark_token) | 175 | bookmarkToken=bookmark_token) |
176 | 176 | ||
177 | def search(self, search_text): | 177 | def search(self, search_text, |
178 | include_near_matches=False, | ||
179 | include_genre_stations=False): | ||
178 | from .models.pandora import SearchResult | 180 | from .models.pandora import SearchResult |
179 | 181 | ||
180 | return SearchResult.from_json(self, | 182 | return SearchResult.from_json( |
181 | self("music.search", | 183 | self, |
182 | searchText=search_text)) | 184 | self("music.search", |
185 | searchText=search_text, | ||
186 | includeNearMatches=include_near_matches, | ||
187 | includeGenreStations=include_genre_stations) | ||
188 | ) | ||
183 | 189 | ||
184 | def add_feedback(self, track_token, positive): | 190 | def add_feedback(self, track_token, positive): |
185 | return self("station.addFeedback", | 191 | return self("station.addFeedback", |
diff --git a/pandora/models/pandora.py b/pandora/models/pandora.py index f2fd36e..8fa8e21 100644 --- a/pandora/models/pandora.py +++ b/pandora/models/pandora.py | |||
@@ -284,37 +284,98 @@ class BookmarkList(PandoraModel): | |||
284 | 284 | ||
285 | class SearchResultItem(PandoraModel): | 285 | class SearchResultItem(PandoraModel): |
286 | 286 | ||
287 | artist = Field("artistName") | ||
288 | song_name = Field("songName") | ||
289 | score = Field("score") | 287 | score = Field("score") |
290 | likely_match = Field("likelyMatch", default=False) | ||
291 | token = Field("musicToken") | 288 | token = Field("musicToken") |
292 | 289 | ||
293 | @property | 290 | @property |
294 | def is_song(self): | 291 | def is_song(self): |
295 | return self.token.startswith('S') | 292 | return isinstance(self, SongSearchResultItem) |
296 | 293 | ||
297 | @property | 294 | @property |
298 | def is_artist(self): | 295 | def is_artist(self): |
299 | return self.token.startswith('R') | 296 | return isinstance(self, ArtistSearchResultItem) and \ |
297 | self.token.startswith("R") | ||
300 | 298 | ||
301 | @property | 299 | @property |
302 | def is_composer(self): | 300 | def is_composer(self): |
303 | return self.token.startswith('C') | 301 | return isinstance(self, ArtistSearchResultItem) and \ |
302 | self.token.startswith("C") | ||
303 | |||
304 | @property | ||
305 | def is_genre_station(self): | ||
306 | return isinstance(self, GenreStationSearchResultItem) | ||
304 | 307 | ||
305 | def create_station(self): | 308 | def create_station(self): |
306 | if self.is_song: | 309 | raise NotImplementedError |
307 | self._api_client.create_station(track_token=self.token) | 310 | |
311 | @classmethod | ||
312 | def from_json(cls, api_client, data): | ||
313 | if data["musicToken"].startswith("S"): | ||
314 | return SongSearchResultItem.from_json(api_client, data) | ||
315 | |||
316 | elif data["musicToken"].startswith(("R", "C")): | ||
317 | return ArtistSearchResultItem.from_json(api_client, data) | ||
318 | |||
319 | elif data["musicToken"].startswith("G"): | ||
320 | return GenreStationSearchResultItem.from_json(api_client, data) | ||
308 | else: | 321 | else: |
309 | self._api_client.create_station(artist_token=self.token) | 322 | raise NotImplementedError("Unknown result token type '{}'" |
323 | .format(data["musicToken"])) | ||
324 | |||
325 | |||
326 | class ArtistSearchResultItem(SearchResultItem): | ||
327 | |||
328 | score = Field("score") | ||
329 | token = Field("musicToken") | ||
330 | artist = Field("artistName") | ||
331 | likely_match = Field("likelyMatch", default=False) | ||
332 | |||
333 | def create_station(self): | ||
334 | self._api_client.create_station(artist_token=self.token) | ||
335 | |||
336 | @classmethod | ||
337 | def from_json(cls, api_client, data): | ||
338 | return super(SearchResultItem, cls).from_json(api_client, data) | ||
339 | |||
340 | |||
341 | class SongSearchResultItem(SearchResultItem): | ||
342 | |||
343 | score = Field("score") | ||
344 | token = Field("musicToken") | ||
345 | artist = Field("artistName") | ||
346 | song_name = Field("songName") | ||
347 | |||
348 | def create_station(self): | ||
349 | self._api_client.create_station(track_token=self.token) | ||
350 | |||
351 | @classmethod | ||
352 | def from_json(cls, api_client, data): | ||
353 | return super(SearchResultItem, cls).from_json(api_client, data) | ||
354 | |||
355 | |||
356 | class GenreStationSearchResultItem(SearchResultItem): | ||
357 | |||
358 | score = Field("score") | ||
359 | token = Field("musicToken") | ||
360 | station_name = Field("stationName") | ||
361 | |||
362 | def create_station(self): | ||
363 | self._api_client.create_station(search_token=self.token) | ||
364 | |||
365 | @classmethod | ||
366 | def from_json(cls, api_client, data): | ||
367 | return super(SearchResultItem, cls).from_json(api_client, data) | ||
310 | 368 | ||
311 | 369 | ||
312 | class SearchResult(PandoraModel): | 370 | class SearchResult(PandoraModel): |
313 | 371 | ||
314 | nearest_matches_available = Field("nearMatchesAvailable") | 372 | nearest_matches_available = Field("nearMatchesAvailable") |
315 | explanation = Field("explanation") | 373 | explanation = Field("explanation") |
316 | songs = Field("songs", formatter=SearchResultItem.from_json_list) | 374 | songs = Field("songs", formatter=SongSearchResultItem.from_json_list) |
317 | artists = Field("artists", formatter=SearchResultItem.from_json_list) | 375 | artists = Field("artists", formatter=ArtistSearchResultItem.from_json_list) |
376 | genre_stations = Field( | ||
377 | "genreStations", | ||
378 | formatter=GenreStationSearchResultItem.from_json_list) | ||
318 | 379 | ||
319 | 380 | ||
320 | class GenreStationList(PandoraDictListModel): | 381 | class GenreStationList(PandoraDictListModel): |