playlists.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from mopidy.models import Playlist, Track
  2. from mopidy.backend import PlaylistProvider
  3. from .mb_client import get_similar_artists, get_genre_tracks
  4. class SmartPlaylistsProvider(PlaylistProvider):
  5. def as_list(self):
  6. playlists = []
  7. # Genres
  8. for genre in ["rock", "jazz", "hiphop", "blues"]:
  9. playlists.append(
  10. Playlist(uri=f"smart:genre:{genre}", name=f"Genre: {genre.title()}")
  11. )
  12. # Example artist radios
  13. playlists.append(
  14. Playlist(uri="smart:artist:radio:MBID123", name="Artist Radio: Example")
  15. )
  16. return playlists
  17. def lookup(self, uri):
  18. parts = uri.split(":")
  19. typ = parts[1]
  20. if typ == "genre":
  21. genre = parts[2]
  22. tracks = get_genre_tracks(genre)
  23. elif typ == "artist" and parts[2] == "radio":
  24. mbid = parts[3]
  25. tracks = get_similar_artists(mbid)
  26. else:
  27. return None
  28. return Playlist(uri=uri, name="Generated", tracks=tracks)