PowerShell/scripts/list-coffee-prices.ps1

60 lines
2.0 KiB
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02: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
2024-05-14 16:18:13 +02:00
Monthly Global Price of Coffee (by alphavantage.co, in cents per pound)
-----------------------------------------------------------------------
2024-04-01 240
2024-10-01 15:11:03 +02:00
...
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
}
2024-05-14 16:18:13 +02:00
Write-Host " $Value" -noNewline
2023-05-25 15:39:55 +02:00
}
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
2024-05-14 16:18:13 +02:00
Write-Progress -completed "Done."
2023-09-19 15:52:46 +02:00
2023-05-25 15:39:55 +02:00
""
2024-05-14 16:18:13 +02:00
"Monthly $($prices.name) (by alphavantage.co, in $($prices.unit))"
"-----------------------------------------------------------------------"
2023-05-25 15:39:55 +02:00
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
2024-05-14 16:18:13 +02:00
Write-Host "ct"
2023-05-25 15:39:55 +02:00
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}