From 05928ee6dfbc75b8b06f5cebdc649afa75a3cc8a Mon Sep 17 00:00:00 2001 From: Jim Geurts Date: Wed, 8 May 2013 10:46:56 -0500 Subject: [PATCH] Add some console messages. Store id and url for feed items to try to eliminate duplicates from sending. Handle feed ids that have "key" query string and use that for the id. --- RssToEmail/Models/Feed.cs | 6 +- RssToEmail/Models/FeedItem.cs | 8 ++ RssToEmail/Program.cs | 143 +++++++++++++++++++++------------- RssToEmail/RssToEmail.csproj | 1 + 4 files changed, 99 insertions(+), 59 deletions(-) create mode 100644 RssToEmail/Models/FeedItem.cs diff --git a/RssToEmail/Models/Feed.cs b/RssToEmail/Models/Feed.cs index f5640c4..15477b3 100644 --- a/RssToEmail/Models/Feed.cs +++ b/RssToEmail/Models/Feed.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; namespace RssToEmail.Models { @@ -9,11 +7,11 @@ public class Feed { public string Id { get; set; } public string Url { get; set; } - public IList SentItems { get; set; } + public IList SentItems { get; set; } public Feed() { - SentItems = new List(); + SentItems = new List(); } } } diff --git a/RssToEmail/Models/FeedItem.cs b/RssToEmail/Models/FeedItem.cs new file mode 100644 index 0000000..0dcd8bb --- /dev/null +++ b/RssToEmail/Models/FeedItem.cs @@ -0,0 +1,8 @@ +namespace RssToEmail.Models +{ + public class FeedItem + { + public string Id { get; set; } + public string Url { get; set; } + } +} diff --git a/RssToEmail/Program.cs b/RssToEmail/Program.cs index a2487da..fc37c81 100644 --- a/RssToEmail/Program.cs +++ b/RssToEmail/Program.cs @@ -7,6 +7,7 @@ using System.Xml; using System.Xml.Linq; using Raven.Client.Embedded; +using Raven.Database.Server; using RssToEmail.Models; namespace RssToEmail @@ -19,12 +20,20 @@ static void Main(string[] args) var to = new MailAddress(ConfigurationManager.AppSettings["to"]); bool sendAllForNewFeeds; bool.TryParse(ConfigurationManager.AppSettings["SendAllForNewFeeds"] ?? "false", out sendAllForNewFeeds); - + Console.Write("Starting raven..."); +// NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(43233); var documentStore = new EmbeddableDocumentStore { - ConnectionStringName = "RavenDB" - }.Initialize(); - + ConnectionStringName = "RavenDB", +// UseEmbeddedHttpServer = true + }; +// documentStore.Configuration.Port = 43233; + documentStore.Initialize(); + Console.WriteLine("Done"); + //using (var session = documentStore.OpenSession()) + //{ + // Console.Read(); + //} using (var session = documentStore.OpenSession()) { foreach (var url in urls) @@ -34,77 +43,101 @@ static void Main(string[] args) Url = url }; var isNew = !savedFeed.SentItems.Any(); - - using (var f = XmlReader.Create(url)) + Console.Write("Processing " + url + "..."); + try { - var feed = SyndicationFeed.Load(f); - var from = new MailAddress(ConfigurationManager.AppSettings["from"], feed.Title.Text); - - bool? supportsContentEncoding = null; - foreach (var item in feed.Items.Reverse()) + using (var f = XmlReader.Create(url)) { - // Ignore previously processed items - if (savedFeed.SentItems.Contains(item.Id)) - continue; + var feed = SyndicationFeed.Load(f); + var from = new MailAddress(ConfigurationManager.AppSettings["from"], feed.Title.Text); - // Only send an email if the url is not new or if the user configures that new urls should send all current items - if (!isNew || sendAllForNewFeeds) + bool? supportsContentEncoding = null; + foreach (var item in feed.Items.Reverse()) { - try + var linkUri = item.Links.FirstOrDefault(); + var link = string.Empty; + if (linkUri != null) + link = linkUri.Uri.ToString().Split('?').First(); + + var id = item.Id; + if (item.Id.Contains("?key=")) { - var content = string.Empty; - if (item.Summary != null) - { - content = item.Summary.Text; - } - else - { - var textContent = item.Content as TextSyndicationContent; - if (textContent != null) - content = textContent.Text; - } + id = item.Id.Split(new [] { "?key=" }, StringSplitOptions.RemoveEmptyEntries)[1]; + } + // Ignore previously processed items + if (savedFeed.SentItems.Any(x => + x.Id.Equals(id, StringComparison.OrdinalIgnoreCase) || + x.Id.Equals(item.Id, StringComparison.OrdinalIgnoreCase) || + (!string.IsNullOrEmpty(x.Url) && x.Url == link))) + continue; - if (!supportsContentEncoding.HasValue || supportsContentEncoding.Value) + // Only send an email if the url is not new or if the user configures that new urls should send all current items + if (!isNew || sendAllForNewFeeds) + { + try { - if (!supportsContentEncoding.HasValue) - supportsContentEncoding = false; + var content = string.Empty; + if (item.Summary != null) + { + content = item.Summary.Text; + } + else + { + var textContent = item.Content as TextSyndicationContent; + if (textContent != null) + content = textContent.Text; + } + - // If the feed has a item, use that instead of what comes from - foreach (var extension in item.ElementExtensions) + if (!supportsContentEncoding.HasValue || supportsContentEncoding.Value) { - var element = extension.GetObject(); - if (element.Name.LocalName == "encoded" && element.Name.Namespace.ToString().Contains("content")) + if (!supportsContentEncoding.HasValue) + supportsContentEncoding = false; + + // If the feed has a item, use that instead of what comes from + foreach (var extension in item.ElementExtensions) { - content = element.Value; - supportsContentEncoding = true; - break; + var element = extension.GetObject(); + if (element.Name.LocalName == "encoded" && element.Name.Namespace.ToString().Contains("content")) + { + content = element.Value; + supportsContentEncoding = true; + break; + } } } - } - var link = item.Links.FirstOrDefault(); + var message = new MailMessage(from, to) { + Subject = "[New Post] " + item.Title.Text, + Body = content + string.Format("

View the original article: {0}

", link), + IsBodyHtml = true + }; - var message = new MailMessage(from, to) { - Subject = "[New Post] " + item.Title.Text, - Body = content + string.Format("

View the original article: {0}

", link != null ? link.Uri.ToString() : string.Empty), - IsBodyHtml = true - }; - - var smtp = new SmtpClient(); - smtp.Send(message); + var smtp = new SmtpClient(); + smtp.Send(message); + } + catch (Exception ex) + { + // Ignore this for now... we'll just try again next time the app runs + continue; + } } - catch (Exception ex) + + savedFeed.SentItems.Add(new FeedItem { - // Ignore this for now... we'll just try again next time the app runs - continue; - } + Id = id, + Url = link + }); } - - savedFeed.SentItems.Add(item.Id); } + session.Store(savedFeed); + Console.WriteLine("Done"); + } + catch (Exception ex) + { + Console.WriteLine("\n" + ex); } - session.Store(savedFeed); } session.SaveChanges(); } diff --git a/RssToEmail/RssToEmail.csproj b/RssToEmail/RssToEmail.csproj index 8348fb0..13df594 100644 --- a/RssToEmail/RssToEmail.csproj +++ b/RssToEmail/RssToEmail.csproj @@ -92,6 +92,7 @@ +