Skip to content

Instantly share code, notes, and snippets.

@JoseGonzalez321
Created June 8, 2017 15:55
Show Gist options
  • Save JoseGonzalez321/375168a8d35e45d8e2bb671970dea994 to your computer and use it in GitHub Desktop.
Save JoseGonzalez321/375168a8d35e45d8e2bb671970dea994 to your computer and use it in GitHub Desktop.
Downloading an RSS feed. Using XML type provider to manipulate and saving results into a CSV file.
//https://fsharpforfunandprofit.com/posts/low-risk-ways-to-use-fsharp-at-work-2/#dev-rss-to-csv
System.IO.Directory.SetCurrentDirectory (__SOURCE_DIRECTORY__)
#r @"..\Packages\FSharp.Data\lib\net40\FSharp.Data.dll"
#r "System.Xml.Linq.dll"
open FSharp.Data
type Rss = XmlProvider<"http://stackoverflow.com/feeds/tag/elm">
let prepareStr obj =
obj.ToString()
.Replace("\"","\"\"") // replace single with double quotes
|> sprintf "\"%s\"" // surround with quotes
// convert a list of strings to CSV
let listToCsv list =
let combine s1 s2 = s1 + "," + s2
list
|> Seq.map prepareStr
|> Seq.reduce combine
// extract fields from Entry
let extractFields (entry:Rss.Entry) =
[entry.Title.Value;
entry.Author.Name;
entry.Published.ToShortDateString()]
// write lines to a file
do
use writer = new System.IO.StreamWriter("elm-questions.csv")
let feed = Rss.GetSample()
feed.Entries
|> Seq.map (extractFields >> listToCsv)
|> Seq.iter writer.WriteLine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment