Created
June 7, 2016 09:55
-
-
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
This file contains 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
#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