/// <summary> /// Catch PlayerEvent and schedule new lastfm query, as long as current Artist is not the last artist queried /// </summary> /// <param name="args"> /// A <see cref="PlayerEventArgs"/> /// </param> private static void OnPlayerEvent(PlayerEventArgs args) { DatabaseTrackInfo currentTrack = ServiceManager.PlayerEngine.CurrentTrack as DatabaseTrackInfo; if (currentTrack == null) { return; } long currentArtistId = currentTrack.ArtistId; if (ServiceManager.PlayerEngine.ActiveEngine.CurrentState == PlayerState.Playing && !processedArtists.Contains(currentArtistId)) { if (!weightedRandom.Contains(currentArtistId)) { // User changed Track to a not similar artist, clear list Log.Debug("RandomByLastfmSimilarArtists: User changed track, clearing lists and resetting depth"); weightedRandom.Clear(); weightedRandom.Add(currentArtistId, 1d); } lock (searchActive_lock) { if (searchActive) { Log.Debug("RandomByLastfmSimilarArtists: Another Query is already running, aborting"); return; } else { searchActive = true; } } ThreadAssist.SpawnFromMain(delegate { QueryLastfm(); lock (searchActive_lock) { searchActive = false; } }); } }
/// <summary> /// Query Lastfm for UserTopArtists /// </summary> /// <remarks>Executed via Kernel Scheduler</remarks> public static void QueryLastfm() { Account account = LastfmCore.Account; if (string.IsNullOrEmpty(account.UserName) || !ServiceManager.Get <Network>().Connected) { return; } LastfmUserData userData = new LastfmUserData(account.UserName); LastfmData <UserTopArtist> topArtists = userData.GetTopArtists(TopType.Overall); Log.Debug("RandomByLastfmUserTopArtists: Searching for present artists"); Dictionary <int, int> artists = GetPresentArtists(topArtists); Log.Debug(String.Format("RandomByLastfmUserTopArtists: Found {0} of {1} Artists", artists.Count, topArtists.Count)); foreach (int cArtistId in artists.Keys) { weightedRandom.Add(cArtistId, artists[cArtistId]); } }