public async Task <SpotifyPlaylist> CreatePlaylist(string name, List <LastFmTrack> tracks)
        {
            var notFound  = 0;
            var processed = 0;
            var toPost    = new List <SpotifyTrack>();

            _addedTracks = new List <SpotifyTrack>();
            var token = await _localStore.GetItemAsync <string>(Constants.SpotifyTokenKey);

            var user = await _localStore.GetItemAsync <SpotifyUser>(Constants.SpotifyUserKey);

            _client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
            var playlist = new CreateSpotifyPlaylist
            {
                Name = name
            };
            var result = await _client.PostAsJsonAsync($"{_baseUrl}/users/{user.Id}/playlists", playlist);

            if (!result.IsSuccessStatusCode)
            {
                RequestRefresh?.Invoke(this, new MessageEventArgs {
                    Messages = new List <string> {
                        "Could not create playlist."
                    }
                });
                return(null);
            }
            var created = JsonSerializer.Deserialize <SpotifyPlaylist>(await result.Content.ReadAsStringAsync());

            foreach (var lfmTrack in tracks)
            {
                var track = await SearchTrack(lfmTrack.Artist.Name, lfmTrack.Name);

                if (track.NotFound)
                {
                    notFound++;
                }
                else
                {
                    toPost.Add(track);
                    _addedTracks.Add(track);
                }
                processed++;
                if (toPost.Count == 10)
                {
                    var query = string.Join(',', toPost.Select(_ => _.Uri?.ToString()));
                    await _client.PostAsync($"{_baseUrl}/playlists/{created.Id}/tracks?uris={query}", new StringContent(""));

                    toPost.Clear();
                    RequestRefresh?.Invoke(this, new MessageEventArgs
                    {
                        Messages = new List <string> {
                            $"Added {processed} of {tracks.Count}. {notFound} not found."
                        },
                        Type = UIUpdateType.Processing
                    });
                }
            }
            if (toPost.Any())
            {
                var query = string.Join(',', toPost.Select(_ => _.Uri?.ToString()));
                await _client.PostAsync($"{_baseUrl}/playlists/{created.Id}/tracks?uris={query}", new StringContent(""));
            }
            CreationDone?.Invoke(this, new MessageEventArgs
            {
                Type     = UIUpdateType.Done,
                Messages = new List <string>
                {
                    $"Your playlist {name} was created.",
                    $"{tracks.Count - notFound} added.",
                    $"{notFound} tracks could not be found on Spotify.",
                }
            });
            return(created);
        }
Beispiel #2
0
 public void FireCreationDone()
 {
     CreationDone.Fire(this, EventArgs.Empty);
 }
Beispiel #3
0
 public void OnCreationDone(TEntity entity)
 {
     CreationDone?.Invoke(entity);
     OperationDone?.Invoke(entity, Operation.Create);
 }