Ejemplo n.º 1
0
        private async Task FillSimilarArtistsAsync()
        {
            if (this.lfmArtist != null && this.lfmArtist.SimilarArtists != null && this.lfmArtist.SimilarArtists.Count > 0)
            {
                await Task.Run(async() =>
                {
                    var localSimilarArtists = new ObservableCollection <SimilarArtistViewModel>();

                    foreach (Artist similarArtist in this.lfmArtist.SimilarArtists)
                    {
                        string artistImageUrl = string.Empty;

                        try
                        {
                            // Last.fm was so nice to break their artist image API. So we need to get images from elsewhere.
                            Artist lfmArtist = await LastfmApi.ArtistGetInfo(similarArtist.Name, true, ResourceUtils.GetString("Language_ISO639-1"));
                            artistImageUrl   = await FanartApi.GetArtistThumbnailAsync(lfmArtist.MusicBrainzId);
                        }
                        catch (Exception ex)
                        {
                            LogClient.Warning($"Could not get artist image from Fanart for artist {similarArtist.Name}. Exception: {ex}");
                        }

                        localSimilarArtists.Add(new SimilarArtistViewModel {
                            Name = similarArtist.Name, Url = similarArtist.Url, ImageUrl = artistImageUrl
                        });
                    }

                    this.SimilarArtists = localSimilarArtists;
                });
            }

            RaisePropertyChanged(nameof(this.SimilarArtists));
            RaisePropertyChanged(nameof(this.HasSimilarArtists));
        }
Ejemplo n.º 2
0
        private async Task DownloadArtworkAsync()
        {
            this.IsBusy = true;

            try
            {
                Common.Api.Lastfm.Album lfmAlbum = await LastfmApi.AlbumGetInfo((string)this.Album.AlbumArtist, (string)this.Album.AlbumTitle, false, "EN");

                byte[] artworkData = null;

                if (!string.IsNullOrEmpty(lfmAlbum.LargestImage()))
                {
                    string temporaryFilePath = await this.cacheService.DownloadFileToTemporaryCacheAsync(new Uri(lfmAlbum.LargestImage()));

                    if (!string.IsNullOrEmpty(temporaryFilePath))
                    {
                        this.UpdateArtwork(ImageUtils.Image2ByteArray(temporaryFilePath));
                    }
                }
            }
            catch (Exception ex)
            {
                LogClient.Error("An error occurred while downloading artwork for the album with title='{0}' and artist='{1}'. Exception: {2}", (string)this.Album.AlbumTitle, (string)this.Album.AlbumArtist, ex.Message);
            }

            this.IsBusy = false;
        }
Ejemplo n.º 3
0
        public async Task SignIn()
        {
            try
            {
                this.sessionKey = await LastfmApi.GetMobileSession(this.username, this.password);

                if (!string.IsNullOrEmpty(this.sessionKey))
                {
                    SettingsClient.Set <string>("Lastfm", "Username", this.username);
                    SettingsClient.Set <string>("Lastfm", "Password", this.password);
                    SettingsClient.Set <string>("Lastfm", "Key", this.sessionKey);
                    LogClient.Info("User '{0}' successfully signed in to Last.fm.", this.username);
                    this.SignInState = SignInState.SignedIn;
                }
                else
                {
                    LogClient.Error("User '{0}' could not sign in to Last.fm.", this.username);
                    this.SignInState = SignInState.Error;
                }
            }
            catch (Exception ex)
            {
                LogClient.Error("User '{0}' could not sign in to Last.fm. Exception: {1}", this.username, ex.Message);
                this.SignInState = SignInState.Error;
            }

            this.SignInStateChanged(this.SignInState);
        }
Ejemplo n.º 4
0
        private async void PlaybackService_PlaybackSuccess(bool isPlayingPreviousTrack)
        {
            if (this.SignInState == SignInState.SignedIn)
            {
                // As soon as a track starts playing, send a Now Playing request.
                this.trackStartTime = DateTime.Now;
                this.canScrobble    = true;
                string artist     = this.playbackService.CurrentTrack.Value.ArtistName != Defaults.UnknownArtistText ? this.playbackService.CurrentTrack.Value.ArtistName : string.Empty;
                string trackTitle = this.playbackService.CurrentTrack.Value.TrackTitle;
                string albumTitle = this.playbackService.CurrentTrack.Value.AlbumTitle != Defaults.UnknownAlbumText ? this.playbackService.CurrentTrack.Value.AlbumTitle : string.Empty;

                if (!string.IsNullOrEmpty(artist) && !string.IsNullOrEmpty(trackTitle))
                {
                    try
                    {
                        bool isSuccess = await LastfmApi.TrackUpdateNowPlaying(this.sessionKey, artist, trackTitle, albumTitle);

                        if (isSuccess)
                        {
                            LogClient.Info("Successfully updated Now Playing for track '{0} - {1}'", artist, trackTitle);
                        }
                        else
                        {
                            LogClient.Error("Could not update Now Playing for track '{0} - {1}'", artist, trackTitle);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogClient.Error("Could not update Now Playing for track '{0} - {1}'. Exception: {2}", artist, trackTitle, ex.Message);
                    }
                }
            }
        }
Ejemplo n.º 5
0
 public LastfmServerEntryPoint(ISessionManager sessionManager, IJsonSerializer jsonSerializer,
                               IHttpClient httpClient, IUserDataManager userDataManager)
 {
     _sessionManager  = sessionManager;
     _userDataManager = userDataManager;
     _lastfmApi       = new LastfmApi(httpClient, jsonSerializer);
     Instance         = this;
 }
Ejemplo n.º 6
0
        /// <inheritdoc />
        /// <summary>
        ///     Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            //Unbind events
            _sessionManager.PlaybackStart   -= PlaybackStart;
            _sessionManager.PlaybackStopped -= PlaybackStopped;
            _userDataManager.UserDataSaved  -= UserDataSaved;

            //Clean up
            _lastfmApi = null;
        }
Ejemplo n.º 7
0
        private async void PlaybackService_PlaybackProgressChanged(object sender, EventArgs e)
        {
            if (this.SignInState == SignInState.SignedIn)
            {
                // When is a scrobble a scrobble?
                // - The track must be longer than 30 seconds
                // - And the track has been played for at least half its duration, or for 4 minutes (whichever occurs earlier)
                string artist     = this.playbackService.CurrentTrack.Value.ArtistName != Defaults.UnknownArtistText ? this.playbackService.CurrentTrack.Value.ArtistName : string.Empty;
                string trackTitle = this.playbackService.CurrentTrack.Value.TrackTitle;
                string albumTitle = this.playbackService.CurrentTrack.Value.AlbumTitle != Defaults.UnknownAlbumText ? this.playbackService.CurrentTrack.Value.AlbumTitle : string.Empty;

                if (this.canScrobble && !string.IsNullOrEmpty(artist) && !string.IsNullOrEmpty(trackTitle))
                {
                    TimeSpan currentTime = this.playbackService.GetCurrentTime;
                    TimeSpan totalTime   = this.playbackService.GetTotalTime;

                    if (totalTime.TotalSeconds > 30)
                    {
                        if (currentTime.TotalSeconds >= totalTime.TotalSeconds / 2 | currentTime.TotalMinutes > 4)
                        {
                            this.canScrobble = false;

                            try
                            {
                                bool isSuccess = await LastfmApi.TrackScrobble(this.sessionKey, artist, trackTitle, albumTitle, this.trackStartTime);

                                if (isSuccess)
                                {
                                    LogClient.Info("Successfully Scrobbled track '{0} - {1}'", artist, trackTitle);
                                }
                                else
                                {
                                    LogClient.Error("Could not Scrobble track '{0} - {1}'", artist, trackTitle);
                                }
                            }
                            catch (Exception ex)
                            {
                                LogClient.Error("Could not Scrobble track '{0} - {1}'. Exception: {2}", artist, trackTitle, ex.Message);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 8
0
        public MusicDataManager()
        {
            _databaseFile =
                new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                                          "Hurricane", "database.sqlite"));

            Images    = new ImagesProvider();
            Artists   = new ArtistProvider(Images);
            Albums    = new AlbumsProvider(Artists);
            Tracks    = new TrackProvider(Artists, Images, Albums);
            Playlists = new PlaylistProvider(Tracks);
            UserData  = new UserDataProvider();

            LastfmApi    = new LastfmApi(Artists);
            MusicManager = new MusicManager();
            MusicManager.TrackChanged   += MusicManager_TrackChanged;
            MusicManager.NewTrackOpened += MusicManager_NewTrackOpened;
            MusicStreamingPluginManager  = new MusicStreamingPluginManager();
        }
Ejemplo n.º 9
0
        public async Task <string> GetAlbumImageAsync(string albumTitle, IList <string> albumArtists, string trackTitle = "", IList <string> trackArtists = null)
        {
            string        title   = string.Empty;
            List <string> artists = new List <string>();

            // Title
            if (!string.IsNullOrEmpty(albumTitle))
            {
                title = albumTitle;
            }
            else if (!string.IsNullOrEmpty(trackTitle))
            {
                title = trackTitle;
            }

            // Artist
            if (albumArtists != null && albumArtists.Count > 0)
            {
                artists.AddRange(albumArtists.Where(a => !string.IsNullOrEmpty(a)));
            }

            if (trackArtists != null && trackArtists.Count > 0)
            {
                artists.AddRange(trackArtists.Where(a => !string.IsNullOrEmpty(a)));
            }

            if (string.IsNullOrEmpty(title) || artists == null)
            {
                return(null);
            }

            foreach (string artist in artists)
            {
                LastFmAlbum lfmAlbum = await LastfmApi.AlbumGetInfo(artist, title, false, "EN");

                if (!string.IsNullOrEmpty(lfmAlbum.LargestImage()))
                {
                    return(lfmAlbum.LargestImage());
                }
            }

            return(null);
        }
Ejemplo n.º 10
0
        public async Task <bool> SendTrackLoveAsync(PlayableTrack track, bool love)
        {
            bool isSuccess = false;

            // We can't send track love for an unknown track
            if (track.ArtistName == Defaults.UnknownArtistText | string.IsNullOrEmpty(track.TrackTitle))
            {
                return(false);
            }

            if (this.SignInState == SignInState.SignedIn)
            {
                if (love)
                {
                    try
                    {
                        isSuccess = await LastfmApi.TrackLove(this.sessionKey, track.ArtistName, track.TrackTitle);
                    }
                    catch (Exception ex)
                    {
                        LogClient.Error("Could not send track.love to Last.fm. Exception: {0}", ex.Message);
                    }
                }
                else
                {
                    try
                    {
                        isSuccess = await LastfmApi.TrackUnlove(this.sessionKey, track.ArtistName, track.TrackTitle);
                    }
                    catch (Exception ex)
                    {
                        LogClient.Error("Could not send track.unlove to Last.fm. Exception: {0}", ex.Message);
                    }
                }
            }

            return(isSuccess);
        }
Ejemplo n.º 11
0
        public async Task SendTrackLoveAsync(TrackViewModel track, bool love)
        {
            // We can't send track love for an unknown track
            if (string.IsNullOrEmpty(track.TrackTitle))
            {
                return;
            }

            if (this.SignInState == SignInState.SignedIn)
            {
                foreach (string artist in DataUtils.SplitAndTrimColumnMultiValue(track.Track.Artists))
                {
                    if (love)
                    {
                        try
                        {
                            await LastfmApi.TrackLove(this.sessionKey, artist, track.TrackTitle);
                        }
                        catch (Exception ex)
                        {
                            LogClient.Error("Could not send track.love to Last.fm. Exception: {0}", ex.Message);
                        }
                    }
                    else
                    {
                        try
                        {
                            await LastfmApi.TrackUnlove(this.sessionKey, artist, track.TrackTitle);
                        }
                        catch (Exception ex)
                        {
                            LogClient.Error("Could not send track.unlove to Last.fm. Exception: {0}", ex.Message);
                        }
                    }
                }
            }
        }
Ejemplo n.º 12
0
        private async Task ShowArtistInfoAsync(PlayableTrack track, bool forceReload)
        {
            this.previousArtist = this.artist;

            // User doesn't want to download artist info, or no track is selected.
            if (!SettingsClient.Get <bool>("Lastfm", "DownloadArtistInformation") || track == null)
            {
                this.ArtistInfoViewModel = this.container.Resolve <ArtistInfoViewModel>();
                this.artist = null;
                return;
            }

            // Artist name is unknown
            if (track.ArtistName == Defaults.UnknownArtistText)
            {
                ArtistInfoViewModel localArtistInfoViewModel = this.container.Resolve <ArtistInfoViewModel>();
                await localArtistInfoViewModel.SetLastFmArtistAsync(new Common.Api.Lastfm.Artist {
                    Name = Defaults.UnknownArtistText
                });

                this.ArtistInfoViewModel = localArtistInfoViewModel;
                this.artist = null;
                return;
            }

            this.artist = new Common.Database.Entities.Artist
            {
                ArtistName = track.ArtistName
            };

            // The artist didn't change: leave the previous artist info.
            if (this.artist.Equals(this.previousArtist) & !forceReload)
            {
                return;
            }

            // The artist changed: we need to show new artist info.
            string artworkPath = string.Empty;

            this.IsBusy = true;

            try
            {
                Common.Api.Lastfm.Artist lfmArtist = await LastfmApi.ArtistGetInfo(track.ArtistName, true, ResourceUtils.GetString("Language_ISO639-1"));

                if (lfmArtist != null)
                {
                    if (string.IsNullOrEmpty(lfmArtist.Biography.Content))
                    {
                        // In case there is no localized Biography, get the English one.
                        lfmArtist = await LastfmApi.ArtistGetInfo(track.ArtistName, true, "EN");
                    }

                    if (lfmArtist != null)
                    {
                        ArtistInfoViewModel localArtistInfoViewModel = this.container.Resolve <ArtistInfoViewModel>();
                        await localArtistInfoViewModel.SetLastFmArtistAsync(lfmArtist);

                        this.ArtistInfoViewModel = localArtistInfoViewModel;
                    }
                    else
                    {
                        throw new Exception("lfmArtist == null");
                    }
                }
            }
            catch (Exception ex)
            {
                LogClient.Error("Could not show artist information for Track {0}. Exception: {1}", track.Path, ex.Message);
                this.ArtistInfoViewModel = this.container.Resolve <ArtistInfoViewModel>();
                this.artist = null;
            }

            this.IsBusy = false;
        }
Ejemplo n.º 13
0
        private async Task ShowArtistInfoAsync(TrackViewModel track, bool forceReload)
        {
            this.previousArtistName = this.artistName;

            // User doesn't want to download artist info, or no track is selected.
            if (!SettingsClient.Get <bool>("Lastfm", "DownloadArtistInformation") || track == null)
            {
                this.ArtistInfoViewModel = this.container.Resolve <ArtistInfoViewModel>();
                this.artistName          = string.Empty;
                return;
            }

            // Artist name is unknown
            if (string.IsNullOrEmpty(track.ArtistName))
            {
                ArtistInfoViewModel localArtistInfoViewModel = this.container.Resolve <ArtistInfoViewModel>();
                await localArtistInfoViewModel.SetArtistInformation(new LastFmArtist { Name = string.Empty }, string.Empty);

                this.ArtistInfoViewModel = localArtistInfoViewModel;
                this.artistName          = string.Empty;
                return;
            }

            this.artistName = track.ArtistName;

            // The artist didn't change: leave the previous artist info.
            if (this.artistName.Equals(this.previousArtistName) & !forceReload)
            {
                return;
            }

            // The artist changed: we need to show new artist info.
            string artworkPath = string.Empty;

            this.IsBusy = true;

            try
            {
                LastFmArtist lfmArtist = await LastfmApi.ArtistGetInfo(track.ArtistName, true, ResourceUtils.GetString("Language_ISO639-1"));

                if (lfmArtist != null)
                {
                    if (string.IsNullOrEmpty(lfmArtist.Biography.Content))
                    {
                        // In case there is no localized Biography, get the English one.
                        lfmArtist = await LastfmApi.ArtistGetInfo(track.ArtistName, true, "EN");
                    }

                    if (lfmArtist != null)
                    {
                        string artistImageUrl = string.Empty;

                        try
                        {
                            // Last.fm was so nice to break their artist image API. So we need to get images from elsewhere.
                            artistImageUrl = await FanartApi.GetArtistThumbnailAsync(lfmArtist.MusicBrainzId);
                        }
                        catch (Exception ex)
                        {
                            LogClient.Warning($"Could not get artist image from Fanart for artist {track.ArtistName}. Exception: {ex}");
                        }

                        ArtistInfoViewModel localArtistInfoViewModel = this.container.Resolve <ArtistInfoViewModel>();
                        await localArtistInfoViewModel.SetArtistInformation(lfmArtist, artistImageUrl);

                        this.ArtistInfoViewModel = localArtistInfoViewModel;
                    }
                    else
                    {
                        throw new Exception("lfmArtist == null");
                    }
                }
            }
            catch (Exception ex)
            {
                LogClient.Error("Could not show artist information for Track {0}. Exception: {1}", track.Path, ex.Message);
                this.ArtistInfoViewModel = this.container.Resolve <ArtistInfoViewModel>();
                this.artistName          = string.Empty;
            }

            this.IsBusy = false;
        }
Ejemplo n.º 14
0
 public RestApi(IJsonSerializer jsonSerializer, IHttpClient httpClient)
 {
     _lastfmApi = new LastfmApi(httpClient, jsonSerializer);
 }
Ejemplo n.º 15
0
 public LastfmSyncTask(IHttpClient httpClient, IJsonSerializer jsonSerializer, IUserManager userManager, IUserDataManager userDataManager)
 {
     _userManager     = userManager;
     _userDataManager = userDataManager;
     _lastfmApi       = new LastfmApi(httpClient, jsonSerializer);
 }