Skip to content
This repository has been archived by the owner on Jul 10, 2020. It is now read-only.

Commit

Permalink
Add some console messages.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jgeurts committed May 8, 2013
1 parent 3d364e9 commit 05928ee
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 59 deletions.
6 changes: 2 additions & 4 deletions RssToEmail/Models/Feed.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RssToEmail.Models
{
public class Feed
{
public string Id { get; set; }
public string Url { get; set; }
public IList<string> SentItems { get; set; }
public IList<FeedItem> SentItems { get; set; }

public Feed()
{
SentItems = new List<string>();
SentItems = new List<FeedItem>();
}
}
}
8 changes: 8 additions & 0 deletions RssToEmail/Models/FeedItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace RssToEmail.Models
{
public class FeedItem
{
public string Id { get; set; }
public string Url { get; set; }
}
}
143 changes: 88 additions & 55 deletions RssToEmail/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Xml;
using System.Xml.Linq;
using Raven.Client.Embedded;
using Raven.Database.Server;
using RssToEmail.Models;

namespace RssToEmail
Expand All @@ -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)
Expand All @@ -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 <content:encoded> item, use that instead of what comes from <description>
foreach (var extension in item.ElementExtensions)
if (!supportsContentEncoding.HasValue || supportsContentEncoding.Value)
{
var element = extension.GetObject<XElement>();
if (element.Name.LocalName == "encoded" && element.Name.Namespace.ToString().Contains("content"))
if (!supportsContentEncoding.HasValue)
supportsContentEncoding = false;

// If the feed has a <content:encoded> item, use that instead of what comes from <description>
foreach (var extension in item.ElementExtensions)
{
content = element.Value;
supportsContentEncoding = true;
break;
var element = extension.GetObject<XElement>();
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("<p style=\"font-size:12px;line-height:1.4em;margin:10px 0px 10px 0px\">View the original article: <a href=\"{0}\">{0}</a></p>", link),
IsBodyHtml = true
};

var message = new MailMessage(from, to) {
Subject = "[New Post] " + item.Title.Text,
Body = content + string.Format("<p style=\"font-size:12px;line-height:1.4em;margin:10px 0px 10px 0px\">View the original article: <a href=\"{0}\">{0}</a></p>", 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();
}
Expand Down
1 change: 1 addition & 0 deletions RssToEmail/RssToEmail.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Models\FeedItem.cs" />
<Compile Include="Models\Feed.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down

0 comments on commit 05928ee

Please sign in to comment.