Using F# to Automate Reading–The Morning Brew

I guess most of the .NET Devs read The Morning Brew, if not you should.  It is a morning newspaper for the dev, so I end up reading it first thing when I go to work. I like to  try and automate most of the stuff . So I thought why not write a script that reads the Morning Brew feed, filter the excluded content that I am not interested in and open the urls before I come in. The reason behind using F# is I don’t have to compile the code. I could use it with FSI.exe. Incase if the extraction logic changes and I don’t have to recompile , it is just fixing the script. The best part of writing in F# is I avoid all the ceremony. Here is the F# code, it is nothing fancy. And this code can easily be modified for other link collection sites.

#r @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.ServiceModel.dll"
#r @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Xml.Linq.dll"

open System.IO
open System.ServiceModel.Syndication
open System.Web
open System.Xml
open System.Xml.Linq
open System.Diagnostics

let formatter = new Rss20FeedFormatter()
let sw = new StringWriter();
// Excluded Keywords
let exists (item:string) = File.ReadAllLines @"c:\Users\naveen\exclude.txt" |>
                             Seq.exists(fun e -> item.ToLower().Contains(e.ToLower()))

formatter.ReadFrom(XmlReader.Create "")
formatter.Feed.Items |> Seq.nth 0 |> fun i -> i.SaveAsRss20(new XmlTextWriter(sw))

let data = HttpUtility.HtmlDecode(sw.ToString())
XDocument.Parse(data).Descendants(XName.Get("li")) |>
           Seq.filter(fun i -> not (exists i.Value)) |>
  i -> i.Descendants(XName.Get("a"))) |>
           Seq.iter(fun x -> x |> Seq.iter(fun y ->
               System.Diagnostics.Process.Start (y.Attribute(XName.Get("href")).Value) |> ignore
           |ex -> printfn "Error : %s" ex.Message))

My excluded file at the moment contains javascript and jquery. I am not much into these. Here is the command line

"C:\Program Files (x86)\Microsoft F#\v4.0\Fsi.exe" --quiet --exec "C:\Users\naveen\morningbrew.fsx"

2 thoughts on “Using F# to Automate Reading–The Morning Brew

  1. Pingback: DotNetShoutout

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s