/// <summary> /// Fill meta data portion of SpotifySearchPO via injected Item object /// </summary> /// <param name="po">SpotifySearchPO</param> /// <param name="dataObject">dataObject</param> private void FillSpotifySearchPOWithMetaDataPortion(SpotifySearchPO po, Item dataObject) { po.ArtistsResults = ArtistsResults; po.TracksResults = TracksResults; if (dataObject.Name != null) { po.Name = dataObject.Name; } po.ExplicitWords = dataObject.ExplicitWords; if (dataObject.Genres != null) { po.Genres = dataObject.Genres; } po.Popularity = dataObject.Popularity; if (dataObject.Followers != null) { po.FollowerTotal = dataObject.Followers.Total; } if (dataObject.Id != null) { po.Id = dataObject.Id; } po.IsLocal = dataObject.Is_local; if (dataObject.Href != null) { dataObject.Href = dataObject.Href.Replace("https://", ""); po.Href = dataObject.Href; } po.TrackNumber = dataObject.Track_number; if (dataObject.available_markets != null) { po.AvailableMarkets = dataObject.available_markets; } if (dataObject.Preview_url != null) { po.PreviewUrl = dataObject.Preview_url; } if (dataObject.artists != null) { po.Artists = new List <Artist>(); for (int i = 0; i < dataObject.artists.Count; i++) { po.Artists.Add(dataObject.artists[i]); } } po.DurationMS = dataObject.duration_ms; if (dataObject.External_urls.Spotify != null) { po.ExternalUrls_Spotify = dataObject.External_urls.Spotify; } po.DiscNumber = dataObject.disc_number; }
/// <summary> /// Exports Data to XML and PDF File /// </summary> public void ExportData() { if (IsValidPOList == false) { MessageBox.Show(@"Export could not be completed due to the following possible causes: 1) Only online Spotify search results can be exported. 2) An offline export file that was imported cannot be exported again.", @"Warning", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); return; } // 1. Build Export XML for import (leverage XMLBuilder) XMLBuilder xmlBuilder = new XMLBuilder(); SpotifySearchPO path = CompleteSpotifySearchList.FirstOrDefault(s => !string.IsNullOrEmpty(s.ImportExportLocationText)); DateTime date = DateTime.Now; string fileAppendDateFormat = $"{date.Year}{date.Day}{date.Month}{date.Hour}{date.Minute}"; string codedPathXml = @"\" + fileAppendDateFormat + "_SpotifySearchResults.xml"; string codedPathPdf = @"\" + fileAppendDateFormat + "_SpotifySearchResults.pdf"; if (path != null && !string.IsNullOrEmpty(path.ImportExportLocationText)) { xmlBuilder.CreateXMLFromSpotifySearchPOList(CompleteSpotifySearchList, path.ImportExportLocationText + codedPathXml); } else { // Place into my documents folder if user hasn't set an actual folder string myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string myPath = myDocuments + @"\" + fileAppendDateFormat + "_SpotifySearchResults.xml"; xmlBuilder.CreateXMLFromSpotifySearchPOList(CompleteSpotifySearchList, myPath); } // 2. Build Export PDF for easy viewing (leverage PDFBuilder) PDFBuilder pdfBuilder = new PDFBuilder(); if (path != null && !string.IsNullOrEmpty(path.ImportExportLocationText)) { pdfBuilder.CreatePdfFromMainFrameDataPoList( CompleteSpotifySearchList, path.ImportExportLocationText + codedPathPdf); } else { // Place into my documents folder if user hasn't set an actual folder string myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string myPath = myDocuments + @"\" + fileAppendDateFormat + "_SpotifySearchResults.pdf"; pdfBuilder.CreatePdfFromMainFrameDataPoList(CompleteSpotifySearchList, myPath); } }
/// <summary> /// Create SpotifySearchPO list from relevant UI elements. /// </summary> /// <returns></returns> private List <SpotifySearchPO> SpotifySearchPOListFromUI() { int searchCount = 0; if (IsArtistSearch) { if (ArtistsResults == null) { return(new List <SpotifySearchPO>()); } searchCount = ArtistsResults.Artists.Items.Count; } if (IsSongSearch) { if (TracksResults == null) { return(new List <SpotifySearchPO>()); } searchCount = TracksResults.Tracks.Items.Count; } List <SpotifySearchPO> list = new List <SpotifySearchPO>(); string defaultPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string selectedDirectory = this.rtxtExportLocation.Text; for (int i = 0; i < searchCount; i++) { SpotifySearchPO po = new SpotifySearchPO(); if (IsArtistSearch) { Item artistObject = ArtistsResults.Artists.Items[i]; po.Title = artistObject.Name; po.Message = $"Artist Spotify ID: {artistObject.Id}" + Environment.NewLine + "Click to view metadata."; // high level artists object FillSpotifySearchPOWithMetaDataPortion(po, artistObject); } if (IsSongSearch) { Item trackObject = TracksResults.Tracks.Items[i]; po.Title = trackObject.Name; po.Message = $"Track Spotify ID: {trackObject.Id}" + Environment.NewLine + "Click to view metadata."; FillSpotifySearchPOWithMetaDataPortion(po, trackObject); } if (rtxtExportLocation != null && !string.IsNullOrEmpty(selectedDirectory)) { po.ImportExportLocationText = selectedDirectory; } else { // Default import / export location is "MyDocuments" environment variable if none selected. po.ImportExportLocationText = defaultPath; } list.Add(po); } return(list); }
/// <summary> /// Creates SpotifySearchPO list from XML Import file. /// </summary> /// <param name="fileNameAndPath"></param> /// <returns></returns> private List <SpotifySearchPO> XMLImportList(string fileNameAndPath) { if (string.IsNullOrEmpty(fileNameAndPath)) { return(new List <SpotifySearchPO>()); } List <SpotifySearchPO> importList = new List <SpotifySearchPO>(); try { XDocument doc = XDocument.Load(fileNameAndPath); XElement spotifySearchResults = doc.Element(SpotifySearchXMLPDFConstants.SpotifySearchResults); foreach (XElement item in spotifySearchResults.Elements()) { SpotifySearchPO po = new SpotifySearchPO(); if (item.Name == SpotifySearchXMLPDFConstants.SearchResults) { po.Title = item .Element(SpotifySearchXMLPDFConstants.Title) .Value; po.Message = item .Element(SpotifySearchXMLPDFConstants.Message) .Value; po.NewLineImport = item .Element(SpotifySearchXMLPDFConstants.EmptyLine) .Value; po.Name = item .Element(SpotifySearchXMLPDFConstants.Name) .Value; po.ExplicitWords = Convert.ToBoolean(item .Element(SpotifySearchXMLPDFConstants.ExplicitWords) .Value); po.Popularity = Convert.ToInt32(item .Element(SpotifySearchXMLPDFConstants.Popularity) .Value); po.FollowerTotal = Convert.ToInt32(item .Element(SpotifySearchXMLPDFConstants.FollowerTotal) .Value); po.Id = item .Element(SpotifySearchXMLPDFConstants.Id) .Value; po.IsLocal = Convert.ToBoolean(item .Element(SpotifySearchXMLPDFConstants.IsLocal) .Value); po.Href = item .Element(SpotifySearchXMLPDFConstants.Href) .Value; po.AvailableMarkets = new string[] { item .Element(SpotifySearchXMLPDFConstants.AvailableMarkets) .Value }; po.PreviewUrl = item .Element(SpotifySearchXMLPDFConstants.PreviewUrl) .Value; string artists = item .Element(SpotifySearchXMLPDFConstants.Artists).Value; string[] artistList = artists.Split(' '); po.Artists = new List <Artist>(); for (int i = 0; i < artistList.Length; i++) { for (int j = 0; j < po.Artists.Count; i++) { artistList[i] = po.Artists[j].Name; } } po.TrackNumber = Convert.ToInt32(item .Element(SpotifySearchXMLPDFConstants.TrackNumber) .Value); po.DurationMS = Convert.ToInt32(item .Element(SpotifySearchXMLPDFConstants.DurationMS) .Value); po.DiscNumber = Convert.ToInt32(item .Element(SpotifySearchXMLPDFConstants.DiscNumber) .Value); po.ExternalUrls_Spotify = item .Element(SpotifySearchXMLPDFConstants.ExternalUrls) .Value; po.ImportExportLocationText = item .Element(SpotifySearchXMLPDFConstants.ImportExportLocationText) .Value; } importList.Add(po); } } catch (Exception e) { MessageBox.Show(@"Unable to Import file. File is either corrupt or xml tags are from a deprecated version of export." + Environment.NewLine + $@" Error: {e}", @"Import File Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); KeepTopMostAndBringToFront(); return(new List <SpotifySearchPO>()); } MessageBox.Show(@"Import Successful! Click 'OK' to update app.", @"Success"); IsOnlineSearch = false; KeepTopMostAndBringToFront(); ImportResults = importList; return(ImportResults); }
public void CreatePdfFromMainFrameDataPoList(List <SpotifySearchPO> list, string path) { if (list == null || !list.Any() || string.IsNullOrEmpty(path)) { return; } bool workComplete = false; try { Document doc = new Document(); PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create)); doc.Open(); Chunk headerChunk = new Chunk("Spotify Search", FontFactory.GetFont("Arial", 48)); Chunk linkChunk = new Chunk(SpotifySearchXMLPDFConstants.HyphenLineHeaderFooter, FontFactory.GetFont("Arial", 11)); Chunk singleSpaceChunk = new Chunk(Environment.NewLine); Chunk doubleSpaceChunk = new Chunk(Environment.NewLine + Environment.NewLine); // Search results Chunk searchResultsHeaderChunk = new Chunk(); List <Chunk> searchResultsList = new List <Chunk>(); string colon = ": "; for (int i = 0; i < list.Count; i++) { SpotifySearchPO po = list[i]; searchResultsHeaderChunk = new Chunk(SpotifySearchXMLPDFConstants.SpotifySearchResultsHeader, FontFactory.GetFont("Arial Bold", 22)); string listItem // List user control items = SpotifySearchXMLPDFConstants.Title + colon + po.Title + po.NewLine + SpotifySearchXMLPDFConstants.Message + colon + po.Message + po.NewLine + po.NewLine + SpotifySearchXMLPDFConstants.MetaData + po.NewLine; if (po.Name != null) { listItem += SpotifySearchXMLPDFConstants.Name + colon + po.Name + po.NewLine; } listItem += SpotifySearchXMLPDFConstants.ExplicitWords + colon + po.ExplicitWords + po.NewLine; listItem += SpotifySearchXMLPDFConstants.Popularity + colon + po.Popularity + po.NewLine; if (po.Followers != null) { listItem += SpotifySearchXMLPDFConstants.FollowerTotal + colon + po.FollowerTotal + po.NewLine; } if (po.Id != null) { listItem += SpotifySearchXMLPDFConstants.Id + colon + po.Id + po.NewLine; } listItem += SpotifySearchXMLPDFConstants.IsLocal + colon + po.IsLocal + po.NewLine; if (po.Href != null) { listItem += SpotifySearchXMLPDFConstants.Href + colon + po.Href + po.NewLine; } if (po.AvailableMarkets != null) { listItem += SpotifySearchXMLPDFConstants.AvailableMarkets + colon + po.NewLine; listItem = po.AvailableMarkets.Aggregate(listItem, (current, market) => current + (market + ", ")); listItem += po.NewLine; } if (po.PreviewUrl != null) { listItem += SpotifySearchXMLPDFConstants.PreviewUrl + colon + po.PreviewUrl + po.NewLine; } if (po.Artists != null) { listItem += SpotifySearchXMLPDFConstants.Artists + colon + po.NewLine; listItem = po.Artists.Aggregate(listItem, (current, artist) => current + artist.Name + " "); listItem += po.NewLine; } listItem += SpotifySearchXMLPDFConstants.TrackNumber + colon + po.TrackNumber + po.NewLine; listItem += SpotifySearchXMLPDFConstants.DurationMS + colon + po.DurationMS + po.NewLine; listItem += SpotifySearchXMLPDFConstants.DiscNumber + colon + po.DiscNumber + po.NewLine; if (po.ExternalUrls_Spotify != null) { listItem += SpotifySearchXMLPDFConstants.ExternalUrls + colon + po.ExternalUrls_Spotify + po.NewLine; } if (!string.IsNullOrEmpty(po.ImportExportLocationText)) { listItem += SpotifySearchXMLPDFConstants.ImportExportPDF + colon + po.ImportExportLocationText + po.NewLine; } listItem += po.NewLine + po.NewLine + po.NewLine; Chunk searchResultsChunk = new Chunk(listItem, FontFactory.GetFont("Arial, 11")); searchResultsList.Add(searchResultsChunk); } DateTime date = DateTime.Now; Chunk dateChunk = new Chunk($"Export Date: {date}", FontFactory.GetFont("Arial", 11)); Chunk creatorChunk = new Chunk($"Developer: Matthew Miller, Email: [email protected], Export Date: {date}", FontFactory.GetFont("Arial", 11)); Paragraph headerParagraph = new Paragraph { Alignment = Element.ALIGN_CENTER }; // list user control paragraph Paragraph searchResultsHeaderParagraph = new Paragraph { Alignment = Element.ALIGN_CENTER }; Paragraph searchResultsParagraph = new Paragraph { Alignment = Element.ALIGN_LEFT }; Paragraph creatorParagraph = new Paragraph { Alignment = Element.ALIGN_RIGHT }; Paragraph dateParagraph = new Paragraph { Alignment = Element.ALIGN_RIGHT }; Paragraph lineParagraph = new Paragraph { Alignment = Element.ALIGN_CENTER }; Paragraph singleSpaceParagraph = new Paragraph { Alignment = Element.ALIGN_CENTER }; Paragraph doubleSpaceParagraph = new Paragraph { Alignment = Element.ALIGN_CENTER }; // Header headerParagraph.Add(headerChunk); // list ui searchResultsHeaderParagraph.Add(searchResultsHeaderChunk); foreach (Chunk item in searchResultsList) { searchResultsParagraph.Add(item + Environment.NewLine); } // Dev creatorParagraph.Add(creatorChunk); dateParagraph.Add(dateChunk); // Formatting singleSpaceParagraph.Add(singleSpaceChunk); doubleSpaceParagraph.Add(doubleSpaceChunk); lineParagraph.Add(linkChunk); // Header doc doc.Add(headerParagraph); doc.Add(lineParagraph); doc.Add(dateParagraph); doc.Add(singleSpaceParagraph); // Search Results doc doc.Add(searchResultsHeaderParagraph); doc.Add(singleSpaceParagraph); doc.Add(searchResultsParagraph); doc.Add(singleSpaceParagraph); // Footer doc doc.Add(lineParagraph); doc.Add(creatorParagraph); doc.Close(); workComplete = true; } catch (Exception e) { MessageBox.Show($@"Reason: {e.Message}", @"Could not Export PDF", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } if (workComplete) { MessageBox.Show($@"Location: {path}", @"PDF Export Successful!", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } }
/// <summary> /// Import SpotifySearchPO per item /// Do not use item.TrackResults or item.ArtistsResults /// </summary> /// <param name="item"></param> /// <param name="index"></param> private void ImportDataToTextBox(SpotifySearchPO item, int index) { if (item == null) { return; } if (this.rtxtMetaData != null) { if (!string.IsNullOrEmpty(this.rtxtMetaData.Text)) { this.rtxtMetaData.Text = string.Empty; } RichTextBox txt = this.rtxtMetaData; string newLine = Environment.NewLine; if (txt != null) { if (item.Name != null) { txt.Text = @"Name: " + item.Name + newLine; } txt.Text += @"Explicit Content: " + item.ExplicitWords + newLine; if (item.Genres != null && index <= item.Genres.Count) { txt.Text += @"Genres: " + item.Genres[index] + newLine; } txt.Text += @"Popularity Total: " + item.Popularity + newLine; if (item.Followers != null) { txt.Text += @"Followers: " + item.Followers.Total + newLine; } if (item.Id != null) { txt.Text += @"ID: " + item.Id + newLine; } txt.Text += @"Is Local: " + item.IsLocal + newLine; if (item.Href != null) { // This link requires a token so remove link state from view. item.Href = item.Href.Replace("https://", ""); txt.Text += @"Internal API Link: " + item.Href + newLine; } txt.Text += @"Track Number: " + item.TrackNumber + newLine; if (item.AvailableMarkets != null && index <= item.AvailableMarkets.Count) { txt.Text += @"Available Markets: " + item.AvailableMarkets[0] + newLine; } if (item.PreviewUrl != null) { txt.Text += @"Preview Url: " + item.PreviewUrl + newLine; } if (item.Artists != null && item.Artists.Count > 0) { txt.Text += @"Artists: " + item.Artists[index].Name + newLine; } txt.Text += @"Song Duration (ms): " + item.DurationMS + newLine; txt.Text += @"Disc Number: " + item.DiscNumber + newLine; if (item.ExternalUrls_Spotify != null) { txt.Text += @"External Urls: " + item.ExternalUrls_Spotify; } } } }