PowerShell/Docs/list-coffee-prices.md

73 lines
1.9 KiB
Markdown
Raw Normal View History

2023-07-29 10:34:04 +02:00
*list-coffee-prices.ps1*
================
2023-05-26 12:20:18 +02:00
list-coffee-prices.ps1
2023-07-29 10:04:38 +02:00
Parameters
----------
2023-05-26 12:20:18 +02:00
```powershell
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Script Content
--------------
2023-05-26 12:20:18 +02:00
```powershell
<#
.SYNOPSIS
List coffee prices
.DESCRIPTION
This PowerShell script queries alphavantage.co and lists the global price of coffee (monthly, in cents per points).
.EXAMPLE
2023-08-06 21:36:33 +02:00
PS> ./list-coffee-prices.ps1
2023-05-26 12:20:18 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-07-29 09:45:37 +02:00
function WriteHorizontalBar { param([float]$Value, [float]$Max)
2023-05-26 12:20:18 +02:00
$Num = ($Value * 100.0) / $Max
while ($Num -ge 1.0) { Write-Host "█" -noNewline; $Num -= 1.0 }
if ($Num -ge 0.875) {
2023-07-29 09:45:37 +02:00
Write-Host "▉" -noNewline
2023-05-26 12:20:18 +02:00
} elseif ($Num -ge 0.75) {
2023-07-29 09:45:37 +02:00
Write-Host "▊" -noNewline
2023-05-26 12:20:18 +02:00
} elseif ($Num -ge 0.625) {
2023-07-29 09:45:37 +02:00
Write-Host "▋" -noNewline
2023-05-26 12:20:18 +02:00
} elseif ($Num -ge 0.5) {
2023-07-29 09:45:37 +02:00
Write-Host "▌" -noNewline
2023-05-26 12:20:18 +02:00
} elseif ($Num -ge 0.375) {
2023-07-29 09:45:37 +02:00
Write-Host "▍" -noNewline
2023-05-26 12:20:18 +02:00
} elseif ($Num -ge 0.25) {
2023-07-29 09:45:37 +02:00
Write-Host "▎" -noNewline
2023-05-26 12:20:18 +02:00
} elseif ($Num -ge 0.125) {
2023-07-29 09:45:37 +02:00
Write-Host "▏" -noNewline
2023-05-26 12:20:18 +02:00
}
Write-Host " $Value"
}
try {
$prices = (Invoke-WebRequest -URI "https://www.alphavantage.co/query?function=COFFEE&interval=monthly&apikey=demo" -userAgent "curl" -useBasicParsing).Content | ConvertFrom-Json
""
"$($prices.name) (by alphavantage.co, in $($prices.unit))"
"---------------------------------------------------------------"
foreach($item in $prices.data) {
if ($Item.value -eq ".") { continue }
2023-07-29 09:45:37 +02:00
Write-Host "$($item.date) " -noNewline
2023-05-26 12:20:18 +02:00
[int]$value = $Item.value
2023-07-29 09:45:37 +02:00
WriteHorizontalBar $value 350.0
2023-05-26 12:20:18 +02:00
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
2023-08-06 21:36:33 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of list-coffee-prices.ps1 as of 08/06/2023 21:36:12)*