using System.IO; using System.Linq; using System.Threading.Tasks; using MoreLinq; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Sony.Filtr.Buzz; using Sony.Filtr.Contracts.Definitions; using Sony.Filtr.Core.EditorialPlaylists; namespace Sony.Filtr.Worker.Onetime { public class AllPlaylistsJsonExport { private readonly EditorialPlaylistManager _editorialPlaylistManager; private readonly BuzzAccountManager _buzzAccountManager; public AllPlaylistsJsonExport(EditorialPlaylistManager editorialPlaylistManager, BuzzAccountManager buzzAccountManager) { _editorialPlaylistManager = editorialPlaylistManager; _buzzAccountManager = buzzAccountManager; } public async Task ExecuteAsync() { var playlists = await _editorialPlaylistManager.GetEditorialPlaylistsAsync(null, onlySony: false, onlyActive: false, lightWeight: true); var playlistViewModels = playlists.DistinctBy(p=> p.SpotifyLink).Select(p => new CustomPlaylistViewModel() { Uri = p.SpotifyLinkUri, PlaylistDisplayName = p.Name }); //Console.WriteLine("Writing a total of {0} playlists", allPlaylists.Count); await SaveJsonAsync(playlistViewModels, "AllPlaylistExport.json"); var allBuzzUsers = await _buzzAccountManager.GetBuzzUsersAsync(); var buzzCategories = _buzzAccountManager.GetBuzzCategories(); var buzzCategoryName = buzzCategories.ToDictionary(k => k.ID, v => v.Name); var buzzUserViewModel = allBuzzUsers.Where(b => b.ServiceType == ServiceType.Spotify) .Select(b => new { Username = b.Username, DisplayName = b.DisplayName, BuzzCategory = b.BuzzCategoryId.HasValue ? buzzCategoryName[b.BuzzCategoryId.Value] : null, Country = b.CountryCode } ).ToList(); await SaveJsonAsync(buzzUserViewModel, "AllBuzzUsers.json"); } private static async Task SaveJsonAsync(object playlistViewModels, string filename) { var json = JsonConvert.SerializeObject(playlistViewModels, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver(), Formatting = Formatting.Indented }); using(var saveFile = File.Open(filename, FileMode.OpenOrCreate)) { using(var fileWriter = new StreamWriter(saveFile)) { await fileWriter.WriteAsync(json); } } } public class CustomPlaylistViewModel { public string Uri { get; set; } public string PlaylistDisplayName { get; set; } } } }