diff --git a/FeedParser/Program.cs b/FeedParser/Program.cs index 526e3eed..8009b7a2 100644 --- a/FeedParser/Program.cs +++ b/FeedParser/Program.cs @@ -1,5 +1,6 @@ // See https://aka.ms/new-console-template for more information using System.Xml; +using System.Xml.Linq; using CodeHollow.FeedReader; using Newtonsoft.Json; using OPMLCore.NET; @@ -43,10 +44,16 @@ { var parsedFeed = await FeedReader.ReadAsync(xmlUrl); Console.WriteLine($"Parsed Feed {parsedFeed.Title}"); - // Write to file - XmlDocument xmlDocument = new XmlDocument(); - xmlDocument.LoadXml(parsedFeed.OriginalDocument); - var feedAsJson = JsonConvert.SerializeXmlNode(xmlDocument, Newtonsoft.Json.Formatting.Indented); + // How to remove #cdata-section when convert xml to json using Linq + // https://gist.github.com/micheletolve/4b511875bfff23fe6970960d6ec3d175 + var doc = XElement.Parse(parsedFeed.OriginalDocument); + var node_cdata = doc.DescendantNodes().OfType().ToList(); + foreach (var node in node_cdata) + { + node.Parent.Add(node.Value); + node.Remove(); + } + var feedAsJson = JsonConvert.SerializeXNode(doc, Newtonsoft.Json.Formatting.Indented); File.WriteAllText($"{workingDir}/tmp/dist/podcasts/{helper.GenerateSlug(parsedFeed.Title)}.json", feedAsJson); return parsedFeed; diff --git a/scripts/generate.py b/scripts/generate.py index b2e4a1c6..e51bdd37 100644 --- a/scripts/generate.py +++ b/scripts/generate.py @@ -35,12 +35,13 @@ def first_3_colors(palette): logFile.write("## Color Palette Generation: \n") def log_error(podcast, error): - logFile.write("An exception occurred for: "+ podcast['title']+"\n: "+ str(error) + "\n") + print ("An exception occurred for: ", podcast['rss']['channel']['title'], "\n: ", error) + logFile.write("An exception occurred for: "+ podcast['rss']['channel']['title'] +"\n: "+ str(error) + "\n") def generate_color_palette(podcast_image): palette = [] try: - if ( 'url' in podcast_image): + if ('url' in podcast_image): image = download_image(podcast_image['url']) palette = get_color_palette(image) palette = first_3_colors(palette) @@ -57,10 +58,15 @@ def generate_color_palette(podcast_image): for index, podcastFile in enumerate(podcastFileNames): podFile = open(podcastsFolder+'/'+podcastFile,'r') data = podFile.read() + try: + json.loads(data) + except Exception as error: + log_error(podcast, error) + continue podcast = json.loads(data) palette = [] if('image' in podcast['rss']['channel']): - print('Generating palette for: ', podcast['rss']['channel']['image']) + print('Generating palette for: ', podcast['rss']['channel']['title']) palette = generate_color_palette(podcast['rss']['channel']['image']) podcast['rss']['channel']['image']['palette'] = palette