예제 #1
0
def to_playlist(
        sp_playlist, folders=None, username=None, bitrate=None,
        as_ref=False, as_items=False):
    if not isinstance(sp_playlist, spotify.Playlist):
        return

    if not sp_playlist.is_loaded:
        return

    if as_items:
        return list(to_track_refs(sp_playlist.tracks))

    name = sp_playlist.name

    if not as_ref:
        tracks = [
            to_track(sp_track, bitrate=bitrate)
            for sp_track in sp_playlist.tracks]
        tracks = filter(None, tracks)
        if name is None:
            # Use same starred order as the Spotify client
            tracks = list(reversed(tracks))

    if name is None:
        name = 'Starred'
    if folders is not None:
        name = '/'.join(folders + [name])
    if username is not None and sp_playlist.owner.canonical_name != username:
        name = '%s (by %s)' % (name, sp_playlist.owner.canonical_name)

    if as_ref:
        return models.Ref.playlist(uri=sp_playlist.link.uri, name=name)
    else:
        return models.Playlist(
            uri=sp_playlist.link.uri, name=name, tracks=tracks)
예제 #2
0
def to_playlist(
    web_playlist,
    username=None,
    bitrate=None,
    as_ref=False,
    as_items=False,
):
    ref = to_playlist_ref(web_playlist, username)
    if ref is None or as_ref:
        return ref

    web_tracks = web_playlist.get("tracks", {}).get("items", [])
    if as_items and not isinstance(web_tracks, list):
        return

    if as_items:
        return list(web_to_track_refs(web_tracks))

    tracks = [
        web_to_track(web_track.get("track", {}), bitrate=bitrate)
        for web_track in web_tracks
    ]
    tracks = [t for t in tracks if t]

    return models.Playlist(uri=ref.uri, name=ref.name, tracks=tracks)
예제 #3
0
def playlist(path, items=None, mtime=None):
    if items is None:
        items = []
    return models.Playlist(
        uri=path_to_uri(path),
        name=name_from_path(path),
        tracks=[models.Track(uri=item.uri, name=item.name) for item in items],
        last_modified=(int(mtime * 1000) if mtime else None))
예제 #4
0
def to_playlist(qobuz_playlist):
    if qobuz_playlist is None:
        return

    return models.Playlist(
        uri="qobuz:playlist:{}".format(qobuz_playlist.id),
        name=qobuz_playlist.name,
        tracks=qobuz_playlist.get_tracks(limit=500),
    )
예제 #5
0
def web_to_playlist(web_playlist,
                    folders=None,
                    username=None,
                    bitrate=None,
                    as_ref=False,
                    as_items=False):
    if web_playlist['type'] != 'playlist':
        return

    if 'tracks' in web_playlist:
        web_tracks = web_playlist['tracks']
        if isinstance(web_tracks, dict) and 'items' in web_tracks:
            web_tracks = [t['track'] for t in web_tracks['items']]
            web_playlist['tracks'] = web_tracks
    else:
        web_tracks = []

    if as_items:
        return list(web_to_track_refs(web_tracks))

    name = web_playlist['name']

    if not as_ref:
        tracks = [
            web_to_track(web_track, bitrate=bitrate)
            for web_track in web_tracks
        ]
        tracks = filter(None, tracks)
        if name is None:
            # Use same starred order as the Spotify client
            tracks = list(reversed(tracks))

    #if name is None:
    #name = 'Starred' # Not supported by Web API
    #if folders is not None: # Not supported by Web API
    #name = '/'.join(folders + [name])
    if username is not None and web_playlist['owner']['id'] != username:
        name = '%s (by %s)' % (name, web_playlist['owner']['id'])

    if as_ref:
        return models.Ref.playlist(uri=web_playlist['uri'], name=name)
    else:
        return models.Playlist(uri=web_playlist['uri'],
                               name=name,
                               tracks=tracks)
예제 #6
0
 def user_playlist(self, username, **kwargs):
     return models.Playlist(tracks=[],
                            name="top tracks",
                            uri="lastfm:user:{0}:{1}".format(
                                username, urllib.parse.urlencode(kwargs)))
예제 #7
0
 def _handle_user_playlist_lookup(self, username, **kwargs):
     return models.Playlist(
         tracks=self._handle_user_tracks_lookup(username, **kwargs),
         name=self._generate_name(username, **kwargs),
         uri="lastfm:user:{0}".format(self._generate_uri_tail(username, **kwargs))
     )