-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-CryptoPrice.ps1
More file actions
127 lines (105 loc) · 3.25 KB
/
Get-CryptoPrice.ps1
File metadata and controls
127 lines (105 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<#
.SYNOPSIS
Fetches the price of various cryptocurrencies.
.DESCRIPTION
Fetches the price of various cryptocurrencies via cryptocompare's API.
.PARAMETER Coin
Specifies the coin abbreviation.
.PARAMETER Refresh
Specifies the refresh time (in seconds).
.PARAMETER Currency
Specifies the preferred currency for output.
.INPUTS
None. You cannot pipe objects.
.OUTPUTS
Returns a string with the price of the coin.
.EXAMPLE 1: Refresh Ethereum Price every 10 minutes (EUR)
PS> ./Get-CryptoPrice.ps1 -coin ETH -currency EUR -refresh 600
.EXAMPLE 2: Refresh Ethereum Price every 10 minutes (USD)
PS> ./Get-CryptoPrice.ps1 -coin ETH -currency USD -refresh 600
#>
# Parameters
param(
[Parameter(Mandatory=$false)][string]$coin,
[Parameter(Mandatory=$false)][ValidateSet("EUR", "USD")][string]$currency,
[Parameter(Mandatory=$false)][string]$refresh
)
# Autofill if blank
if (!$coin) {
$coin = "BTC"
}
if (!$currency) {
$currency = "EUR"
}
if (!$refresh) {
$refresh = "600"
}
# Var
if ($currency -eq "EUR"){
$currency_symbol = [char]8364
}
elseif ($currency -eq "USD"){
$currency_symbol = "$"
}
else {
$currency_symbol = [char]8364
}
clear
Write-Host "=======================================" -ForegroundColor Yellow
Write-Host " " $coin.ToUpper() "Price Ticker" -ForegroundColor Yellow
Write-Host "=======================================" -ForegroundColor Yellow
# Window Title
$Host.UI.RawUI.WindowTitle = $coin.ToUpper() + " Price Ticker"
# Window Size
$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.windowsize
$newsize.width = 40
$newsize.height = 18
$pswindow.windowsize = $newsize
# Buffer size
$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
$newsize.width = 40
$newsize.height = 3000
$pswindow.buffersize = $newsize
#Loop
while($true){
#Query URL
$query_url = "https://min-api.cryptocompare.com/data/price?fsym="+$coin+"&"+"tsyms="+$currency
#Time
$Date = Get-Date
try {
$last = Invoke-RestMethod -Uri $query_url -ErrorAction SilentlyContinue
}
catch {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$last = Invoke-RestMethod -Uri $query_url -ErrorAction Stop
}
start-sleep $refresh
try {
$current = Invoke-RestMethod -Uri $query_url -ErrorAction SilentlyContinue
}
catch {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$current = Invoke-RestMethod -Uri $query_url -ErrorAction Stop
}
# Write output
if ($current.$currency -gt $last.$currency){
#Trend Up
Write-Host "+" $Date "-" $current.$currency $currency_symbol -ForegroundColor Green
}
elseif ($current.$currency -lt $last.$currency){
#Trend Down
Write-Host "-" $Date "-" $current.$currency $currency_symbol -ForegroundColor Red
}
elseif ($current.$currency -eq $last.$currency){
#Stable Trend
Write-Host "=" $Date "-" $current.$currency $currency_symbol
}
else {
#?!?
Write-Host "?" $Date "-" $current.$currency $currency_symbol -ForegroundColor Magenta
}
}