コード例 #1
0
    public async Task <Result <SpotifyAlbum> > FindReleaseAsync(string searchQuery)
    {
        try
        {
            var client = this.clientProviderService.GetSpotifyClient();

            this.serviceLogger.LogTrace($"FindReleaseAsync: Find release request, query = {searchQuery}");

            var searchResult = await client.Search.Item(new SearchRequest(SearchRequest.Types.Album, searchQuery));

            if (searchResult.Albums.Items == null)
            {
                this.serviceLogger.LogError("FindReleaseAsync: Bad spotify response");
                return(Result.Fail("Invalid response from Spotify"));
            }

            switch (searchResult.Albums.Items.Count)
            {
            case 0:
                this.serviceLogger.LogWarn($"FindReleaseAsync: No matches found on Spotify for {searchQuery}");
                return(Result.Fail("No matches found on Spotify"));

            case > 2:
                this.serviceLogger.LogWarn($"FindReleaseAsync: Too many matches (> 2) found on Spotify for {searchQuery}");
                return(Result.Fail("Too many matches (> 2) found on Spotify"));

            default:
            {
                // TODO: Just use first match for now
                var item = SpotifyApiConverters.Convert(searchResult.Albums.Items[0]);
                this.serviceLogger.LogTrace($"FindReleaseAsync: Match found {item.ReleaseName} ({item.ReleaseDate:yyyy/MM/dd})");

                return(Result.Ok(item));
            }
            }
        }
        catch (Exception ex)
        {
            return(Result.Fail(ex.Message));
        }
    }
コード例 #2
0
    public async Task <SpotifyPlaylist> CreatePlaylistAsync(string playlistName, IReadOnlyList <SpotifyTrack> tracks, IProgress <int> progress)
    {
        var client = this.clientProviderService.GetSpotifyClient();

        this.serviceLogger.LogInfo($"CreatePlaylistAsync: Create playlist request; Playlist name = {playlistName}");

        var user = await client.UserProfile.Current();

        var request = new PlaylistCreateRequest(playlistName)
        {
            Description = PLAYLIST_MARKET
        };
        var playlist = await client.Playlists.Create(user.Id, request);

        this.serviceLogger.LogInfo($"CreatePlaylistAsync: Playlist {playlistName} was created successfully");

        if (playlist.Id == null)
        {
            return(null);
        }

        var chunks = tracks.Select(t => t.Uri).ChunkBy(100).ToList();

        this.serviceLogger.LogInfo($"CreatePlaylistAsync: Adding tracks to playlist {playlistName}...");
        foreach ((var chunk, int progressPercent) in chunks.ProgressForEach())
        {
            progress.Report(progressPercent);
            await client.Playlists.AddItems(playlist.Id, new PlaylistAddItemsRequest(chunk));
        }

        this.serviceLogger.LogInfo($"CreatePlaylistAsync: {tracks.Count} track(s) was successfully to playlist {playlistName}");

        var updatedPlaylist = await client.Playlists.Get(playlist.Id);

        return(SpotifyApiConverters.Convert(updatedPlaylist));
    }