using NLog; using Sony.Filtr.Spotify.Artists; using Sony.Filtr.SpotifyWebAPI; using Sony.Filtr.Utility.Extensions; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Sony.Filtr.Tasks.Tasks.Spotify { public class ScrapeArtistListenersTask : IScheduledTask { private readonly SpotifyArtistMonthlyListenerManager _spotifyArtistMonthlyListenerManager; private readonly SpotifyArtistManager _spotifyArtistManager; private readonly Logger _logger; private readonly InternalSpotifyApiClient _internalSpotifyApiClient; public ScrapeArtistListenersTask(SpotifyArtistMonthlyListenerManager spotifyArtistMonthlyListenerManager, SpotifyArtistManager spotifyArtistManager) { _spotifyArtistMonthlyListenerManager = spotifyArtistMonthlyListenerManager; _spotifyArtistManager = spotifyArtistManager; _logger = LogManager.GetLogger("ScrapeArtistListeners"); _internalSpotifyApiClient = new InternalSpotifyApiClient(_logger); } public async Task ExecuteAsync(Guid scheduledTaskLogId) { var date = DateTime.Today; var existingEntries = _spotifyArtistMonthlyListenerManager.GetEntries(date); var existingArtistIds = existingEntries.Select(p => p.ArtistId); var popularArtistIds = await _spotifyArtistManager.GetArtistIdsWithMinimumFollowerCountAsync(1000); var errorsWhileFetchingCount = 0; var artistIdsToFetch = popularArtistIds.Except(existingArtistIds).ToList(); if (artistIdsToFetch.Any()) { _logger.Debug($"Will fetch info for {artistIdsToFetch.Count} artists"); await artistIdsToFetch.ForEachAsync(5, async artistId => { _logger.Debug($"Getting info for {artistId}"); try { var artistInfo = await _internalSpotifyApiClient.GetArtistInfoAsync(artistId); if (artistInfo?.artistInsights != null) { _logger.Debug($"Saving info for {artistId}"); await _spotifyArtistMonthlyListenerManager.SaveMonthlyListenersAsync(artistId, date, artistInfo.artistInsights.monthly_listeners, artistInfo.artistInsights.monthly_listeners_delta); } else { _logger.Error($"No data for fetch on { artistId }"); } } catch (Exception e) { _logger.Error(e, $"Error while scraping data for artist with ID { artistId }"); Interlocked.Increment(ref errorsWhileFetchingCount); } }); if (errorsWhileFetchingCount > 0) _logger.Warn($"{ errorsWhileFetchingCount } errors occured while fetching individual artists' info."); } return null; } } }