Snippets

Tuomas Hietanen Fetch EUR based currency rates of the day and parse currency conversion rates from European Central Bank

Created by Tuomas Hietanen
#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

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.