Created
June 8, 2017 15:55
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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