Esempio n. 1
0
    def del_tracks(self, tracks, own, max_elements_per_request=50):
        """Delete tracks from the playlist.

        In order to limit the length of the resulting URL for a very large
        number of tracks, split the request into multiple.

        Parameters
        ----------
        tracks: list: Track or int (playlist track id)
            Tracks to be deleted. Note that the id used here, is Track.playlist_track_id, returned via Playlist.get_tracks
        own: User
            Deleting tracks requires a logged in User
        max_elements_per_request: int
            Split the request into multiple. Each with at most this many tracks
        """
        if isinstance(tracks[0], int):
            track_ids = tracks
        else:
            track_ids = [t.playlist_track_id for t in tracks]

        for c in self._split_into_chunks(track_ids, max_elements_per_request):
            api.request(
                "playlist/deleteTracks",
                playlist_id=self.id,
                comma_encoding=False,
                playlist_track_ids=",".join(map(str, c)),
                user_auth_token=own.auth_token,
            )
Esempio n. 2
0
    def favorites_add(self, obj):
        """Add artist/album/track to user's favorites.

        Parameters
        ----------
        obj: Artist/Album/Track
            Object to be added to the favorites

        Returns
        -------
        bool
            Successfully added to favorites
        """
        if isinstance(obj, Artist):
            status = api.request(
                "favorite/create",
                artist_ids=obj.id,
                user_auth_token=self.auth_token,
            )
        elif isinstance(obj, Album):
            status = api.request(
                "favorite/create",
                album_ids=obj.id,
                user_auth_token=self.auth_token,
            )
        elif isinstance(obj, Track):
            status = api.request(
                "favorite/create",
                track_ids=obj.id,
                user_auth_token=self.auth_token,
            )
        else:
            raise TypeError("obj must be Artist, Album or Track")

        return status.get("status") == "success"
Esempio n. 3
0
    def add_tracks(self, tracks, own, max_elements_per_request=50):
        """Add tracks to the playlist.

        In order to limit the length of the resulting URL for a very large
        number of tracks, split the request into multiple.

        Parameters
        ----------
        tracks: list: Track
            Tracks to be added
        own: User
            Adding tracks requires a logged in User
        max_elements_per_request: int
            Split the request into multiple. Each with at most this many tracks
        """
        track_ids = [t.id for t in tracks]

        for c in self._split_into_chunks(track_ids, max_elements_per_request):
            api.request(
                "playlist/addTracks",
                playlist_id=self.id,
                comma_encoding=False,
                track_ids=",".join(map(str, c)),
                user_auth_token=own.auth_token,
            )
Esempio n. 4
0
    def playlist_create(self,
                        name,
                        description=None,
                        is_public=0,
                        is_collaborative=0):
        """Create a new playlist.

        Parameters
        ----------
        name: str
            Name for the new playlist
        description: str
            Description for the playlist
        is_public: bool
            Flag to make the playlist public.
        is_collaborative: bool
            Flag to make the playlist collaborative.
        """
        playlist = api.request(
            "playlist/create",
            name=name,
            description=description,
            is_public=is_public,
            is_collaborative=is_collaborative,
            user_auth_token=self.auth_token,
        )

        return Playlist(playlist)
Esempio n. 5
0
    def search(cls, query, limit=50, offset=0, raw=False):
        """Search for a track.

        Parameters
        ----------
        query: str
            Search query
        limit: int
            Number of elements returned per request
        offset: int
            Offset from which to obtain limit elements
        raw: bool
            results will be returned as json if True

        Returns
        -------
        list of Track
            Resulting tracks for the search query
        """
        tracks = api.request("track/search",
                             query=query,
                             offset=offset,
                             limit=limit)

        if raw:
            return tracks

        return [cls(t) for t in tracks["tracks"]["items"]]
Esempio n. 6
0
    def get_tracks(self, limit=50, offset=0):
        """Tracks of the playlist.

        Parameters
        ---------
        limit: int
            Number of elements returned per request
        offset: int
            Offset from which to obtain limit elements

        Returns
        -------
        lst
            List of Tracks
        """
        token = self._user.auth_token if self._user else None

        playlist = api.request(
            "playlist/get",
            playlist_id=self.id,
            extra="tracks",
            limit=limit,
            user_auth_token=token,
            offset=offset,
        )

        return [Track(t) for t in playlist["tracks"]["items"]]
Esempio n. 7
0
    def get_file_url(self, track_id, format_id=None, intent=None):
        """Get the file url for a track.

        Parameters
        ----------
        track_id: int
            Track-ID to get the url for
        format_id: int
            Format ID following qobuz specifications:
             5: MP3 320
             6: FLAC Lossless
             7: FLAC Hi-Res 24 bit =< 96kHz,
            27: FLAC Hi-Res 24 bit >96 kHz & =< 192 kHz
        intent: str
            How the application will use the file URL
            Either 'stream', 'import', or 'download'.

        Returns
        -------
        str
            URL to the appropriate file
        """
        resp = api.request(
            "track/getFileUrl",
            signed=True,
            track_id=track_id,
            format_id=format_id,
            intent=intent,
            user_auth_token=self.auth_token,
        )

        return resp.get("url")
Esempio n. 8
0
    def from_id(cls, playlist_id, user=None):
        token = user.auth_token if user is not None else None

        playlist = api.request("playlist/get",
                               playlist_id=playlist_id,
                               user_auth_token=token)

        return cls(playlist, user=user)
Esempio n. 9
0
    def playlists_get(self, filter="owner", limit=50, offset=0):
        result = api.request(
            "playlist/getUserPlaylists",
            filter=filter,
            limit=limit,
            offset=offset,
            user_auth_token=self.auth_token,
        )

        return [Playlist(p, user=self) for p in result["playlists"]["items"]]
Esempio n. 10
0
    def reset_password(username):
        """Request the resetting of the current password.

        Parameters
        ----------
        username: str
            Username to be sent a email with instructions.

        Returns
        -------
        bool
            Successfully requested
        """
        resp = api.request("user/resetPassword", username=username)

        return resp.get("status") == "success"
Esempio n. 11
0
    def __init__(self, username, password, device_manufacturer_id=None):
        self.username = username
        if device_manufacturer_id is None:
            device_manufacturer_id = uuid.uuid4()

        login_resp = api.request(
            "user/login",
            username=username,
            password=self._hash_password(password),
            device_manufacturer_id=device_manufacturer_id,
        )

        self.auth_token = login_resp["user_auth_token"]
        self.id = login_resp["user"]["id"]
        self.credential_id = login_resp["user"]["credential"]["id"]
        self.device_id = login_resp["user"]["device"]["id"]
Esempio n. 12
0
    def favorites_get(self, fav_type=None, limit=50, offset=0, raw=False):
        """Get all favorites for the user.

        Parameters
        ----------
        fav_type: str
            Favorite type: 'artists', 'albums' or 'tracks'
        limit: int
            Number of elements returned per request
        offset: int
            Offset from which to obtain limit elements
        raw: bool
            results will be returned as json if True

        Returns
        -------
        list
            List containing Artist/Album/Track objects
        """
        favorites = api.request(
            "favorite/getUserFavorites",
            type=fav_type,
            limit=limit,
            offset=offset,
            user_auth_token=self.auth_token,
        )

        if raw:
            return favorites

        if fav_type == "artists":
            return [Artist(f) for f in favorites["artists"]["items"]]
        if fav_type == "albums":
            return [Album(f) for f in favorites["albums"]["items"]]
        if fav_type == "tracks":
            return [Track(f) for f in favorites["tracks"]["items"]]
        else:
            all_favorites = [Artist(f) for f in favorites["artists"]["items"]]
            all_favorites.append(
                Album(f) for f in favorites["albums"]["items"])
            all_favorites.append(
                Track(f) for f in favorites["tracks"]["items"])
            return all_favorites
Esempio n. 13
0
    def playlist_delete(self, playlist):
        """Delete a playlist.

        Parameters
        ----------
        playlist: Playlist
            Playlist to be deleted

        Returns
        -------
        bool
            Successfully deleted playlist
        """
        status = api.request(
            "playlist/delete",
            playlist_id=playlist.id,
            user_auth_token=self.auth_token,
        )

        return status.get("status") == "success"
Esempio n. 14
0
    def favorites_add(self, **kwargs):
        """Add artists/albums/tracks to user's favorites.

        kwargs
        ----------
        artists : Artist, int, str or list of these
        albums : Album, int or str or list of these
        tracks : Track, int, str or list of these

        Returns
        -------
        bool
            Successfully added to favorites
        """

        for params in self._get_params_splitted(kwargs):
            status = api.request("favorite/create", **params)
            if status.get("status") != "success":
                return False
        return True
Esempio n. 15
0
    def favorites_status(self, obj):
        """Get status whether obj is in the favorites.

        Parameters
        ----------
        obj: Artist/Album/Track
            Object to be added to the favorites

        Returns
        -------
        bool
            Successfully deleted from favorites
        """
        status = api.request(
            "favorite/status",
            item=obj.id,
            type=obj.type,
            user_auth_token=self.auth_token,
        )

        return status.get("status") == "true"
Esempio n. 16
0
    def search(cls, query, limit=50, offset=0):
        """Search for a playlist.

        Parameters
        ----------
        query: str
            Search query
        limit: int
            Number of elements returned per request
        offset: int
            Offset from which to obtain limit elements

        Returns
        -------
        list of Playlist
            Resulting playlists for the search query
        """
        playlists = api.request("playlist/search",
                                query=query,
                                limit=limit,
                                offset=offset)

        return [cls(p) for p in playlists["playlists"]["items"]]
Esempio n. 17
0
    def favorites_del(self, **kwargs):
        """Delete artists/albums/tracks from favorites.

        Parameters
        ----------
        artists : Artist, int, str or list of these
        albums : Album, int or str or list of these
        tracks : Track, int, str or list of these

        Returns
        -------
        bool
            Successfully deleted from favorites
        """

        for params in self._get_params_splitted(kwargs):
            status = api.request(
                "favorite/delete",
                **params,
            )
            if status.get("status") != "success":
                return False
        return True
Esempio n. 18
0
    def search(cls, query, limit=50, offset=0):
        """Search for a track.

        Parameters
        ----------
        query: str
            Search query
        limit: int
            Number of elements returned per request
        offset: int
            Offset from which to obtain limit elements

        Returns
        -------
        list of Track
            Resulting tracks for the search query
        """
        tracks = api.request("track/search",
                             query=query,
                             offset=offset,
                             limit=limit)

        return [cls(t) for t in tracks["tracks"]["items"]]
Esempio n. 19
0
    def playlist_delete(self, playlist):
        """Delete a playlist.

        Parameters
        ----------
        playlist: Playlist or int (playlist id)
            Playlist to be deleted

        Returns
        -------
        bool
            Successfully deleted playlist
        """
        if isinstance(playlist, int):
            id = playlist
        else:
            id = playlist.id
        status = api.request(
            "playlist/delete",
            playlist_id=id,
            user_auth_token=self.auth_token,
        )

        return status.get("status") == "success"
Esempio n. 20
0
 def from_id(cls, id):
     return cls(api.request("track/get", track_id=id))