by shortrounddev2 · 3y ago · 0 comments
$> (invoke-webrequest "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.csv").Content
| ConvertFrom-Csv
| sort "usd"
| select "date" -first 1
Date
----
2000-10-26
Doing it with a zip file would be a little more verbose since there is no built in "gunzip" type command which operates on streams, but you can write one which does basically that out of built in .Net functions: function ConvertFrom-Zip {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[byte[]]$Data
)
process {
$memoryStream = [System.IO.MemoryStream]::new($Data)
$zipArchive = [System.IO.Compression.ZipArchive]::new($memoryStream)
$outputStreams = @()
foreach ($entry in $zipArchive.Entries) {
$reader = [System.IO.StreamReader]::new($entry.Open())
$outputStreams += $reader.ReadToEnd()
$reader.Close()
}
$zipArchive.Dispose()
$memoryStream.Dispose()
return $outputStreams
}
}
and call it like: # unary "," operator required to have powershell
# pipe the byte[] as a single argument rather
# than piping each byte individually to
# ConvertFrom-Zip
$> ,(invoke-webrequest "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.zip").Content
| ConvertFrom-Zip
| ConvertFrom-Csv
| sort "usd"
| select "date" -first 1
Date
----
2000-10-26
I love powershell /tmp> # be kind to the server and only download the file if it's updated
/tmp> curl -s -o /tmp/euro.zip -z /tmp/euro.zip https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.zip
/tmp> unzip -p /tmp/euro.zip | from csv | select Date USD | sort-by USD | first
╭──────┬────────────╮
│ Date │ 2000-10-26 │
│ USD │ 0.83 │
╰──────┴────────────╯
(I removed the pipe to gunzip because 1. gunzip doesn't work like that on mac and 2. it's not something you should expect to work anyway, zip files often won't work like that, they're a container and their unzip can't normally be streamed)I wonder if their webserver supports the If-modified-since http header
tl;dr: the server doesn't support that header, but since the response does include a Last-Modified header, curl helpfully aborts the transfer if the Last-Modified date is the same as the mtime of the previously downloaded file.