Skip to content

Instantly share code, notes, and snippets.

@Thorium
Created June 7, 2016 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thorium/370b0062036ae57f879b6f0be6b617fd to your computer and use it in GitHub Desktop.
Save Thorium/370b0062036ae57f879b6f0be6b617fd to your computer and use it in GitHub Desktop.
Fetch EUR based currency rates of the day and parse currency conversion rates from European Central Bank
#if INTERACTIVE
#r "System.Xml.dll"
#r "System.Xml.Linq.dll"
#endif
open System
open System.Net
open System.IO
open System.Xml.Linq
let fetch() =
let currencyUrl = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
let req = WebRequest.Create (currencyUrl) :?> HttpWebRequest
use stream = req.GetResponse().GetResponseStream()
use reader = new StreamReader(stream)
reader.ReadToEnd()
let parsedoc() =
let xn ns s = XName.Get(s,ns)
let xml = fetch() |> XDocument.Parse
let xns = xn (xml.Root.Attribute(XName.Get("xmlns")).Value)
let cubedata = xml.Descendants(xns "Cube")
|> Seq.filter(fun x -> x.HasAttributes) |> Seq.head
let time = cubedata.Attribute(xn "" "time").Value |> DateTime.Parse
let currencies =
cubedata.Descendants(xns "Cube") |>
Seq.map(fun c -> c.Attribute(xn "" "currency").Value,
c.Attribute(xn "" "rate").Value |> Decimal.Parse)
time, currencies |> Map.ofSeq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment