using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using MoreLinq; using Sony.Filtr.Buzz; using Sony.Filtr.Contracts.Definitions; using Sony.Filtr.Core.EditorialPlaylists; using Sony.Filtr.Utility.Extensions; namespace Sony.Filtr.Worker.Onetime { public class UpdatePlaylistBuzzCategoryId { private readonly EditorialPlaylistManager _editorialPlaylistManager; private readonly BuzzAccountManager _buzzAccountManager; public UpdatePlaylistBuzzCategoryId(EditorialPlaylistManager editorialPlaylistManager, BuzzAccountManager buzzAccountManager) { _editorialPlaylistManager = editorialPlaylistManager; _buzzAccountManager = buzzAccountManager; } public async Task FixPlaylistsAsync() { var buzzUsers = (await _buzzAccountManager.GetBuzzUsersAsync()).Where(p=> p.ServiceType == ServiceType.Spotify); var buzzUserCategories = buzzUsers.DistinctBy(k => k.Username).ToDictionary(k => k.Username.ToLowerInvariant(), v => v.BuzzCategoryId); var allPlaylists = await _editorialPlaylistManager.GetEditorialPlaylistsAsync(null, onlySony: false, onlyActive:false, lightWeight:true); int counter = 0; await allPlaylists.ForEachAsync(10, async editorialPlaylist => { int? buzzCategoryId = null; var username = editorialPlaylist.SpotifyLink.ExtractSpotifyPlaylistUserName(); if (buzzUserCategories.TryGetValue(username.ToLowerInvariant(), out buzzCategoryId)) { if (editorialPlaylist.BuzzCategoryId != buzzCategoryId) { Console.WriteLine("Updating {0} of {1}: {2}", counter, allPlaylists.Count, editorialPlaylist.Name); Interlocked.Increment(ref counter); editorialPlaylist.BuzzCategoryId = buzzCategoryId; await _editorialPlaylistManager.UpdateSpotifyFieldsAsync(editorialPlaylist, clearCache: false); } } }); await _editorialPlaylistManager.ClearGetCachesAsync(); await _editorialPlaylistManager.ClearIndividualPlaylistsCachesAsync(); } } }