using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using Nancy; using Nancy.Responses; using Sony.Filtr.AdminAPI.ViewModels; using Sony.Filtr.Contracts.Entities; using Sony.Filtr.Core.EditorialPlaylists; using Sony.Filtr.Utility.Extensions; using SpreadsheetLight; namespace Sony.Filtr.AdminAPI.Utility { public static class ExcelUtility { public static string GetFilenameDatePart() { return DateTime.Today.ToString("MM-dd-yy", CultureInfo.InvariantCulture); } public static Response BuildExcelResponse(List playlistStats, string playlistExportFileName) { return BuildExcelResponse(GetExcelStream(playlistStats), playlistExportFileName); } public static object BuildExcelResponse(List playlistAttributes, string playlistExportFileName) { return BuildExcelResponse(GetExcelStream(playlistAttributes), playlistExportFileName); } private static Stream GetExcelStream(List playlists) { MemoryStream stream = new MemoryStream(); var doc = new SLDocument(); doc.SetCellValue(1, 1, "Playlist"); doc.SetCellValue(1, 2, "Spotify Uri"); doc.SetCellValue(1, 3, "Followers"); foreach (var stat in playlists.ItemIndex()) { var index = stat.Index + 2; doc.SetCellValue(index, 1, stat.Item.Name); doc.SetCellValue(index, 2, stat.Item.SpotifyLink); doc.SetCellValue(index, 3, stat.Item.Subscribers ?? 0); } doc.SaveAs(stream); stream.Position = 0; return stream; } private static Stream GetExcelStream(List playlistStats) { MemoryStream stream = new MemoryStream(); var doc = new SLDocument(); doc.SetCellValue(1, 1, "Playlist"); doc.SetCellValue(1, 2, "Spotify Uri"); doc.SetCellValue(1, 3, "Followers"); foreach (var stat in playlistStats.ItemIndex()) { var index = stat.Index + 2; doc.SetCellValue(index, 1, stat.Item.Playlist.Name); doc.SetCellValue(index, 2, stat.Item.Playlist.SpotifyLinkUri); doc.SetCellValue(index, 3, stat.Item.LatestSubscribers); } doc.SaveAs(stream); stream.Position = 0; return stream; } public static Response BuildExcelResponse(List playlistAttributes, string filename) { return BuildExcelResponse(GetExcelStream(playlistAttributes), filename); } private static Stream GetExcelStream(List playlistAttributes) { MemoryStream stream = new MemoryStream(); var doc = new SLDocument(); doc.SetCellValue(1, 1, "Name"); doc.SetCellValue(1, 2, "SpotifyUri"); doc.SetCellValue(1, 3, "Subscribers"); doc.SetCellValue(1, 4, "Subscribers7"); doc.SetCellValue(1, 5, "Subscribers30"); doc.SetCellValue(1, 6, "Active"); doc.SetCellValue(1, 7, "Priority"); foreach (var playlistAttribute in playlistAttributes.ItemIndex()) { var excelRowIndex = playlistAttribute.Index + 2; var playlist = playlistAttribute.Item.EditorialPlaylist; doc.SetCellValue(excelRowIndex, 1, playlist.Name); doc.SetCellValue(excelRowIndex, 2, playlist.SpotifyLinkUri); if (playlistAttribute.Item.Attributes.Followers.HasValue) doc.SetCellValue(excelRowIndex, 3, playlistAttribute.Item.Attributes.Followers.Value); if (playlistAttribute.Item.Attributes.FollowerChange7.HasValue) doc.SetCellValue(excelRowIndex, 4, playlistAttribute.Item.Attributes.FollowerChange7.Value); if (playlistAttribute.Item.Attributes.FollowerChange30.HasValue) doc.SetCellValue(excelRowIndex, 5, playlistAttribute.Item.Attributes.FollowerChange30.Value); doc.SetCellValue(excelRowIndex, 6, playlist.Active.ToString()); doc.SetCellValue(excelRowIndex, 7, playlist.Priority); } doc.SaveAs(stream); stream.Position = 0; return stream; } public static Response BuildExcelResponse(Dictionary statistics, string filename) { return BuildExcelResponse(GetExcelStreamForPlaylistStats(statistics), filename); } private static Stream GetExcelStreamForPlaylistStats(Dictionary statistics) { MemoryStream stream = new MemoryStream(); var sl = new SLDocument(); sl.SetCellValue(1, 1, "Date"); sl.SetCellValue(1, 2, "Subscribers"); sl.SetCellValue(1, 3, "Change"); uint? prev = null; foreach (var stat in statistics.ItemIndex().ToList()) { var excelRowIndex = stat.Index + 2; sl.SetCellValue(excelRowIndex, 1, stat.Item.Key); sl.SetCellStyle(excelRowIndex, 1, new SLStyle() { FormatCode = "MM/dd/yy" }); if (stat.Item.Value.HasValue) { sl.SetCellValue(excelRowIndex, 2, stat.Item.Value.Value); if (prev.HasValue) { sl.SetCellValue(excelRowIndex, 3, (int)stat.Item.Value - (int)prev); } prev = stat.Item.Value.Value; } } sl.SaveAs(stream); stream.Position = 0; return stream; } public static Response BuildExcelResponse(Stream stream, string filename) { return new StreamResponse(() => stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet").WithContentDispositionFileName(filename); } //private static string EncodeFilename(string filename) //{ // var bytes = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(filename)); // return Encoding.ASCII.GetString(bytes); //} } }