PowerShell/scripts/list-coffee-prices.ps1

54 lines
1.6 KiB
PowerShell
Raw Normal View History

2023-10-31 12:33:36 +01:00
<#
2023-05-25 15:39:55 +02:00
.SYNOPSIS
List coffee prices
.DESCRIPTION
2023-05-25 15:43:57 +02:00
This PowerShell script queries alphavantage.co and lists the global price of coffee (monthly, in cents per points).
2023-05-25 15:39:55 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./list-coffee-prices.ps1
2023-05-25 15:39:55 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-05-26 12:34:08 +02:00
function WriteHorizontalBar { param([float]$Value, [float]$Max)
2023-05-25 15:39:55 +02:00
$Num = ($Value * 100.0) / $Max
2023-05-25 15:43:57 +02:00
while ($Num -ge 1.0) { Write-Host "" -noNewline; $Num -= 1.0 }
2023-05-25 15:39:55 +02:00
if ($Num -ge 0.875) {
2023-05-26 12:34:08 +02:00
Write-Host "" -noNewline
2023-05-25 15:39:55 +02:00
} elseif ($Num -ge 0.75) {
2023-05-26 12:34:08 +02:00
Write-Host "" -noNewline
2023-05-25 15:39:55 +02:00
} elseif ($Num -ge 0.625) {
2023-05-26 12:34:08 +02:00
Write-Host "" -noNewline
2023-05-25 15:39:55 +02:00
} elseif ($Num -ge 0.5) {
2023-05-26 12:34:08 +02:00
Write-Host "" -noNewline
2023-05-25 15:39:55 +02:00
} elseif ($Num -ge 0.375) {
2023-05-26 12:34:08 +02:00
Write-Host "" -noNewline
2023-05-25 15:39:55 +02:00
} elseif ($Num -ge 0.25) {
2023-05-26 12:34:08 +02:00
Write-Host "" -noNewline
2023-05-25 15:39:55 +02:00
} elseif ($Num -ge 0.125) {
2023-05-26 12:34:08 +02:00
Write-Host "" -noNewline
2023-05-25 15:39:55 +02:00
}
Write-Host " $Value"
}
try {
Write-Progress "Loading data from www.alphavantage.co..."
2023-05-25 15:39:55 +02:00
$prices = (Invoke-WebRequest -URI "https://www.alphavantage.co/query?function=COFFEE&interval=monthly&apikey=demo" -userAgent "curl" -useBasicParsing).Content | ConvertFrom-Json
2023-09-19 15:52:46 +02:00
Write-Progress -completed " "
2023-05-25 15:39:55 +02:00
""
"$($prices.name) (by alphavantage.co, in $($prices.unit))"
"---------------------------------------------------------------"
foreach($item in $prices.data) {
if ($Item.value -eq ".") { continue }
2023-05-26 12:34:08 +02:00
Write-Host "$($item.date) " -noNewline
2023-05-25 15:39:55 +02:00
[int]$value = $Item.value
2023-05-26 12:34:08 +02:00
WriteHorizontalBar $value 350.0
2023-05-25 15:39:55 +02:00
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}