Esempio n. 1
0
        public async Task <bool> UpdateTrack(Track track, string forceQueryTitle = null)
        {
            var api = await GetSpotifyWebAPI();

            if (api == null)
            {
                OpenAuthenticationDialog();
                return(false);
            }

            var playback = await api.GetPlaybackAsync();

            if (playback.HasError() || playback.Item == null)
            {
                api.Dispose();

                // open spotify authentication page if user is disconnected
                // user might be connected with a different account that the one that granted rights
                if (!_connectionDialogOpened)
                {
                    OpenAuthenticationDialog();
                }

                // fallback in case getting the playback did not work
                Settings.Default.MediaTagsAPI = (int)MediaTagsAPI.LastFM;
                Settings.Default.Save();

                FrmEspionSpotify.Instance.UpdateMediaTagsAPIToggle(MediaTagsAPI.LastFM);

                var lastFmApiResult = await _lastFmApi.UpdateTrack(track);

                return(lastFmApiResult);
            }

            MapSpotifyTrackToTrack(track, playback.Item);

            if (playback.Item.Album?.Id == null)
            {
                api.Dispose();
                return(false);
            }

            var album = await api.GetAlbumAsync(playback.Item.Album.Id);

            if (album.HasError())
            {
                api.Dispose();
                return(false);
            }

            MapSpotifyAlbumToTrack(track, album);

            api.Dispose();
            return(true);
        }
Esempio n. 2
0
        public async Task <bool> UpdateTrack(Track track)
        {
            var api = await GetSpotifyWebAPI();

            if (api == null)
            {
                return(false);
            }

            var playback = await api.GetPlaybackAsync();

            if (playback.HasError() || playback.Item == null)
            {
                // fallback in case getting the playback did not work
                // user might be connected with a different account that the one that granted rights
                Settings.Default.MediaTagsAPI = (int)MediaTagsAPI.LastFM;
                Settings.Default.Save();
                return(await _lastFmApi.UpdateTrack(track));
            }

            MapSpotifyTrackToTrack(track, playback.Item);

            if (playback.Item.Album?.Id == null)
            {
                return(false);
            }

            var album = await api.GetAlbumAsync(playback.Item.Album.Id);

            if (album.HasError())
            {
                return(false);
            }

            MapSpotifyAlbumToTrack(track, album);

            return(true);
        }
Esempio n. 3
0
        public async Task <bool> UpdateTrack(Track track)
        {
            var api = await GetSpotifyWebAPI();

            if (api == null)
            {
                return(false);
            }

            var playback = api.GetPlayback();

            if (playback.HasError() || playback.Item == null)
            {
                // fallback in case getting the playback did not work
                // user might be connected with a different account that the one that granted rights
                return(await _lastFmApi.UpdateTrack(track));
            }

            track.Title         = playback.Item.Name;
            track.AlbumPosition = playback.Item.TrackNumber;
            track.Performers    = playback.Item.Artists?.Select(a => a.Name).ToArray();
            track.Disc          = (uint)playback.Item.DiscNumber;

            if (playback?.Item?.Album?.Id == null)
            {
                return(false);
            }

            var album = api.GetAlbum(playback.Item.Album.Id);

            if (album.HasError())
            {
                return(false);
            }

            track.AlbumArtists = album.Artists.Select(a => a.Name).ToArray();
            track.Album        = album.Name;
            track.Genres       = album.Genres.ToArray();
            if (uint.TryParse(album.ReleaseDate?.Substring(0, 4), out var year))
            {
                track.Year = year;
            }

            if (album.Images.Count > 0)
            {
                var sorted = album.Images.OrderByDescending(i => i.Width).ToList();

                if (sorted.Count > 0)
                {
                    track.ArtExtraLargeUrl = sorted[0].Url;
                }
                if (sorted.Count > 1)
                {
                    track.ArtLargeUrl = sorted[1].Url;
                }
                if (sorted.Count > 2)
                {
                    track.ArtMediumUrl = sorted[2].Url;
                }
                if (sorted.Count > 3)
                {
                    track.ArtSmallUrl = sorted[3].Url;
                }
                if (sorted.Count > 4)
                {
                    track.ArtExtraLargeUrl = sorted[4].Url;
                }
            }

            return(true);
        }