using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using MoreLinq; using NLog; using Sony.Filtr.AppleMusic; using Sony.Filtr.AppleMusic.Curators; using Sony.Filtr.AppleMusic.Data; using Sony.Filtr.AppleMusic.Data.Internal; using Sony.Filtr.AppleMusic.Models; using Sony.Filtr.AppleMusic.Playlists; using Sony.Filtr.Buzz; using Sony.Filtr.Contracts.Abstractions; using Sony.Filtr.Contracts.Definitions; using Sony.Filtr.Contracts.Entities.Buzz; using Sony.Filtr.Utility.Extensions; namespace Sony.Filtr.Tasks.Tasks.AppleMusic { public class UpdateAppleMusicBuzzUsersTask : IScheduledTask { private readonly AppleMusicCuratorManager _appleMusicCuratorManager; private readonly BuzzAccountManager _buzzAccountManager; private Logger _logger; public UpdateAppleMusicBuzzUsersTask(AppleMusicCuratorManager appleMusicCuratorManager, BuzzAccountManager buzzAccountManager) { _appleMusicCuratorManager = appleMusicCuratorManager; _buzzAccountManager = buzzAccountManager; _logger = LogManager.GetLogger("UpdateAppleBuzzUsers"); } public async Task ExecuteAsync(Guid scheduledTaskLogId) { _logger.Debug($"Begin update"); await UpdateBuzzAccountsAsync(); _logger.Debug($"Done updating buzz accounts"); return null; } private async Task UpdateBuzzAccountsAsync() { var allAppleBuzzUsers = await _buzzAccountManager.GetBuzzUsersAsync(musicServiceId: (int)MusicService.AppleMusic); var allCurators = _appleMusicCuratorManager.GetCurators(); _logger.Debug($"Loaded {allAppleBuzzUsers.Count()} buzz users and {allCurators.Count} curators."); int count = 0; foreach(var curator in allCurators) { var buzzUser = allAppleBuzzUsers.FirstOrDefault(u => u.Username.Equals(curator.Id.ToString())); if (buzzUser != null) { _logger.Debug($"Updating username {buzzUser.Username}."); buzzUser.DisplayName = curator.Name; buzzUser.BuzzCategoryId = GetBuzzCategoryId(curator); buzzUser.Subscribers = null; buzzUser.Image = null; buzzUser.Error = false; await _buzzAccountManager.UpdateBuzzUserAsync(buzzUser); count++; } } _logger.Debug($"Updated {count} buzz users."); //var appleBuzzAccountsByCountryCode = (await _buzzAccountManager.GetBuzzUsersAsync(musicServiceId: (int)MusicService.AppleMusic)).GroupBy(u => u.CountryCode).ToList(); //_logger.Debug($"Got {appleBuzzAccountsByCountryCode.Sum(u => u.Count())} Apple Music buzz accounts to update."); //foreach(var countryCodeUsers in appleBuzzAccountsByCountryCode) //{ // _logger.Debug($"Updating users from {countryCodeUsers.Key}."); // await countryCodeUsers.Batch(100).ForEachAsync(10, async userBatch => // { // await UpdateBuzzAccountInformationAsync(countryCodeUsers.Key, userBatch); // }); //} } private int? GetBuzzCategoryId(AppleMusicCurator curator) { if (string.IsNullOrWhiteSpace(curator.CuratorType)) return null; if (curator.CuratorType.Equals(AppleMusicCuratorType.AppleCurator, StringComparison.InvariantCultureIgnoreCase)) return (int?)StaticBuzzCategory.AppleCurator; if (curator.CuratorType.Equals(AppleMusicCuratorType.Artists, StringComparison.InvariantCultureIgnoreCase)) return (int?)StaticBuzzCategory.Artists; return null; } //private async Task UpdateBuzzAccountInformationAsync(string storefront, IEnumerable userBatch) //{ // var curators = await _appleMusicApi.GetCuratorsAsync(storefront, userBatch.Select(u => u.Username)); // foreach(var buzzUser in userBatch) // { // var curator = curators.FirstOrDefault(c => c.id.Equals(buzzUser.Username, StringComparison.InvariantCulture)); // if (curator == null) // return; // buzzUser.DisplayName = curator.attributes?.name; // buzzUser.Image = curator.attributes?.artwork?.url; // await _buzzAccountManager.UpdateBuzzUserAsync(buzzUser); // } //} } }