Beispiel #1
0
    def post(self, request, *args, **kwargs):
        """
        This method pull all playlist tracks from provider for given playlist.
        """
        serialized = TrackPostSerializer(data=self.request.data)
        serialized.is_valid(raise_exception=True)
        valid_data = serialized.validated_data

        source, playlist_id = valid_data['source'], valid_data['playlist_id']

        adapter = Provider.get_provider(provider=source,
                                        token=valid_data['source_token'],
                                        user=request.account)

        if source == Provider.SPOTIFY and playlist_id == "spotify_saved_tracks":
            tracks: List[list] = adapter.saved_tracks(playlist_id=playlist_id)
        else:
            tracks: List[list] = adapter.playlist_tracks(
                playlist_id=playlist_id)

        if not any(tracks):
            raise AllTracksAlreadyProcessed(
                "All tracks on this playlist already processed.")

        # We get list of lists. Due to this condition need to flatten that list.
        merged = list(itertools.chain(*tracks))
        serialized = TrackSerializer(merged, many=True)
        return Response(serialized.data, status=200)
Beispiel #2
0
    def post(self, request, *args, **kwargs):
        """
        This method pull all playlists from provider.
        """
        serialized = PlaylistPostSerializer(data=self.request.data)
        serialized.is_valid(raise_exception=True)
        valid_data = serialized.validated_data

        source = valid_data['source']
        adapter = Provider.get_provider(provider=source,
                                        token=valid_data['source_token'],
                                        user=request.account)

        playlists: list = adapter.playlists()
        if source == Provider.SPOTIFY:
            try:
                Playlist.objects.get(remote_id=adapter.saved_tracks_id,
                                     user=request.account)
            except Playlist.DoesNotExist:
                saved_tracks_playlist = Playlist(
                    name="saved_tracks",
                    status="private",
                    remote_id=adapter.saved_tracks_id,
                    content=None,
                    provider=Provider.SPOTIFY,
                    user=request.account)
                saved_tracks_playlist.save()
                playlists.append(saved_tracks_playlist)

        if not any(playlists):
            raise AllPlaylistsAlreadyProcessed(
                "All playlist on this account already processed.")

        serialized = PulledPlaylistSerializer({"playlists": playlists})
        return Response(serialized.data, status=200)
Beispiel #3
0
def transfer_tracks_task(source_slug, source_token, end_slug, end_token, user):
    adapter = Provider.get_provider(provider=source_slug,
                                    token=source_token,
                                    user=user)

    # Get all playlists of user and get tracks of those playlists.
    playlists = Playlist.objects.filter(user=user, provider=source_slug)

    for playlist in playlists:
        playlist_remote_id = playlist.remote_id
        try:
            if playlist_remote_id == "spotify_saved_tracks":
                adapter.saved_tracks(playlist_id=playlist_remote_id)
            else:
                adapter.playlist_tracks(playlist_id=playlist_remote_id)
        except ProviderResponseError as pre:
            logger.warning(pre)
            TransferError.objects.create(
                request_data={"playlist_id": playlist_remote_id},
                error=pre,
                source=source_slug,
                end=end_slug,
                type=TransferError.PLAYLIST,
                user=user)
            continue

    # Search inserted tracks and try to upload them.
    adapter = Provider.get_provider(provider=end_slug,
                                    token=end_token,
                                    user=user)
    created_playlists = CreatedPlaylist.objects.filter(user=user,
                                                       provider=end_slug)

    for created_playlist in created_playlists:
        playlist = Playlist.objects.prefetch_related("playlisttrack_set").get(
            name=created_playlist.name, provider=source_slug)
        tracks = playlist.playlisttrack_set.filter(is_transferred=False)
        for track in tracks:
            try:
                response = adapter.search(track.name)
                adapter.add_track_to_playlist(
                    playlist_id=created_playlist.remote_id,
                    track_id=response['id'])
            except ProviderResponseError as pre:
                logger.warning(pre)
                # In search part it create search error.
                continue
Beispiel #4
0
def transfer_playlists_task(source_slug, source_token, end_slug, end_token,
                            user):
    adapter = Provider.get_provider(provider=source_slug,
                                    token=source_token,
                                    user=user)
    # Create all playlists in database
    adapter.playlists()
    if source_slug == Provider.SPOTIFY:
        try:
            Playlist.objects.get(remote_id=adapter.saved_tracks_id, user=user)
        except Playlist.DoesNotExist:
            saved_tracks_playlist = Playlist(name="saved_tracks",
                                             status="private",
                                             remote_id=adapter.saved_tracks_id,
                                             content=None,
                                             provider=Provider.SPOTIFY,
                                             user=user)
            saved_tracks_playlist.save()

    # Pull all created playlists from db and send request to end point to create.
    adapter = Provider.get_provider(provider=end_slug,
                                    token=end_token,
                                    user=user)

    playlists = Playlist.objects.filter(user=user,
                                        provider=source_slug,
                                        is_transferred=False)

    for playlist in playlists:
        playlist_data = {
            "playlist_name": playlist.name,
            "privacy_status": playlist.status,
        }
        try:
            adapter.create_playlist(**playlist_data)
            playlist.is_transferred = True
            playlist.save()
        except ProviderResponseError as pre:
            logger.warning(pre)
            TransferError.objects.create(request_data=playlist_data,
                                         error=pre,
                                         source=source_slug,
                                         end=end_slug,
                                         type=TransferError.PLAYLIST,
                                         user=user)
            continue
Beispiel #5
0
    def post(self, request, *args, **kwargs):
        serialized = SearchSerializer(data=self.request.data)
        serialized.is_valid(raise_exception=True)
        valid_data = serialized.validated_data

        adapter = Provider.get_provider(provider=valid_data['source'],
                                        token=valid_data['source_token'],
                                        user=request.account)
        response = adapter.search(valid_data['track_name'])

        return Response(response, status=200)
Beispiel #6
0
    def post(self, request, *args, **kwargs):
        serialized = self.serializer_class(data=self.request.data)
        serialized.is_valid(raise_exception=True)
        valid_data = serialized.validated_data

        adapter = Provider.get_provider(provider=valid_data['end'],
                                        token=valid_data['end_token'],
                                        user=request.account)
        adapter.add_track_to_playlist(valid_data['playlist_id'],
                                      valid_data['track_id'])

        return Response(status=201)
Beispiel #7
0
    def post(self, request, *args, **kwargs):
        serialized = CreatePlaylistSerializer(data=self.request.data)
        serialized.is_valid(raise_exception=True)
        valid_data = serialized.validated_data

        adapter = Provider.get_provider(provider=valid_data["end"],
                                        token=valid_data['end_token'],
                                        user=request.account)

        playlist = adapter.create_playlist(valid_data)

        return Response(playlist, status=200)