-
Notifications
You must be signed in to change notification settings - Fork 0
/
phl.ps1
223 lines (182 loc) · 7.37 KB
/
phl.ps1
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env powershell
# Cron every 15 Min
# */15 * * * * pwsh -File "/opt/phl/phl.ps1"
<#
.SYNOPSIS Phl Waitingtime Processor
.DESCRIPTION
.NOTES Author: Gill Bates, Last Update: 28.06.2024
#>
#Requires -Module Influx
[string]$logPath = "/var/log/phlapi.log"
[string]$bucket = "phl"
[string]$org = "myOrg"
[string]$iflxServer = "https://iflx2.cloudheros.de" # InfluxDB v2
[string]$iflxToken = "ikLgCOW3aSXKFeZbssM11Lnc0G9c6XmoIMfQkFA8PvLmwIwOAc7BYki2M1_ur0xH-lGHiMrrru-oecXGItTJRw=="
# Only required if secure Password Store is used
# [string]$scriptPath = (Get-Location).Path
# [string]$iflxTokenFile = "iflxToken.xml"
###################### FUNCTIONS AREA ######################
############################################################
function Get-Logtime {
# This function is optimzed for Azure Automation!
$Timeformat = "yyyy-MM-dd HH:mm:ss" #yyyy-MM-dd HH:mm:ss.fff
if ((Get-Timezone).Id -ne "W. Europe Standard Time") {
try {
$tDate = (Get-Date).ToUniversalTime()
$tz = [System.TimeZoneInfo]::FindSystemTimeZoneById("W. Europe Standard Time")
$TimeZone = [System.TimeZoneInfo]::ConvertTimeFromUtc($tDate, $tz)
return $(Get-Date -Date $TimeZone -Format $Timeformat)
}
catch {
return $(Get-Date -Format $Timeformat)
}
}
return $(Get-Date -Format $Timeformat)
}
function Get-ParkStatus {
try {
return Invoke-RestMethod -Method GET -Uri "https://api.phlsys.de/api/park-infos"
}
catch {
throw "[ERROR] while fetching Park Status from 'https://api.phlsys.de': $(($_.Exception).Message))!"
}
}
# themeparks.wiki
function Get-PhlWaitTime1 {
Write-Information "[$(Get-Logtime)] [INFO] Fetching Data from 'themeparks.wiki' ..." -InformationAction Continue
try {
$query = (Invoke-RestMethod -Method GET -Uri "https://api.themeparks.wiki/v1/entity/phantasialand/live").liveData | Where-Object { $_.entityType -like "attraction" }
}
catch {
throw "[ERROR] while fetching Waiting Time from 'themeparks.wiki': $($_.Exception.Message)!"
}
$obj = @()
$query | ForEach-Object {
$obj += [PSCustomObject]@{
lastUpdated = [datetime](Get-Date -Format o ($_.lastUpdated ))
name = $_.name
status = ($_.status).ToLower()
waitTime = [int]$_.queue.standby.waitTime
}
}
Write-Information "[$(Get-Logtime)] [INFO] Received Data of '$(($obj).Count)' Rides!" -InformationAction Continue
return $obj | Sort-Object Name
}
# wartezeiten.app
function Get-PhlWaitTime2 {
Write-Information "[$(Get-Logtime)] [INFO] Fetching Data from 'wartezeiten.app' ..." -InformationAction Continue
try {
$Header = @{
"park" = "phantasialand"
"language" = "de"
}
$Params = @{
"URI" = "https://api.wartezeiten.app/v1/waitingtimes"
"Method" = "GET"
"Headers" = $Header
}
$query = (Invoke-RestMethod @Params -SslProtocol Tls12) | Group-Object Name
}
catch {
throw "[ERROR] while fetching Waiting Time from 'wartezeiten.app': $(($_.Exception).Message)!"
}
$lastUpdated = (Get-Date (($query.Group.date | Select-Object -First 1) + " " + ($query.Group.time | Select-Object -First 1 )))
$obj = @()
$query | ForEach-Object {
$obj += [PSCustomObject]@{
ride = $_.name | Select-Object -First 1
status = ($_.Group.status).ToLower() | Select-Object -First 1
waitTime = [int]($_.Group.waitingtime | Select-Object -First 1)
lastUpdated = [datetime]$lastUpdated
}
}
Write-Information "[$(Get-Logtime)] [INFO] Received Data of '$(($obj).Count)' Rides!" -InformationAction Continue
return $obj | Sort-Object ride
}
function Write-ParkState {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 # Set TLS Version
try {
Write-Influx -Server $iflxServer `
-Bucket $bucket `
-Organisation $org `
-Token $iflxToken `
-Timestamp $parkStatus.updatedAt `
-Measure parkState `
-Metrics @{
"isOpen" = $parkStatus.isOpen
"closeAt" = $parkStatus.close
"manuallyForceClosed" = $parkStatus.manuallyForceClosed
}
}
catch {
throw "[ERROR] while writing parkState into InfluxDB '$iflxServer': $(($_.Exception).Message)"
}
}
###################### PROGRAM AREA ######################
##########################################################
$null = Start-Transcript -Path $logPath -UseMinimalHeader
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 # Set TLS Version
#region currentlyBroken
# Checking for iflxTokenFile
# Write-Output "[$(Get-Logtime)] [INFO] Checking if Password File for Influxdb-Token exists ..."
# if (!(Test-Path -Path (Join-Path -Path $scriptPath $iflxTokenFile))) {
# Write-Warning "*** iflxTokenFile '$iflxTokenFile' missing! ***" -WarningAction Continue
# $bucketToken = Read-Host -AsSecureString -Prompt "Enter Bucket Token for '$bucket'"
# $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $bucket, $bucketToken
# $Credential | Export-Clixml $iflxTokenFile
# if ($?) {
# Write-Output "[$(Get-Logtime)] [OK] iflxTokenFile successfully created! Proceed ..."
# }
# }
# Loading Password
# $influxCredential = Import-Clixml -Path $iflxTokenFile
# $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($($influxCredential.Password))
# $UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
# [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR)
#endregion
# Check if the Park is open and processing Park Status Result
$parkStatus = Get-ParkStatus
if (!($parkStatus.isOpen)) {
Write-Warning "Park is currently CLOSED :-( (Closed at '$([datetime]$parkStatus.close)')" -WarningAction Continue
Write-ParkState
if ($?) { Write-Output "[$(Get-Logtime)] [OK] Scan completed. Bye!" }
$null = Stop-Transcript
Exit
}
else {
Write-Output "[$(Get-Logtime)] [INFO] Park is OPEN :-) Write parkState Data into InfluxDB: '$iflxServer' ..."
Write-ParkState
}
# Fetch current WaitingTime for all available Rides
$apiResult = Get-PhlWaitTime2
# Processing Records
Write-Output "[$(Get-Logtime)] [INFO] Writing Data into InfluxDB '$iflxServer' ...`n"
[int]$count = 1
$apiResult | ForEach-Object {
Write-Output "[$(Get-Logtime)] [$count/$($apiResult.count)] ---> '$($_.ride)' ---> | Status: $([string]$_.status) | WaitTime: $([int]$_.waitTime) Min."
Write-Influx -Server $iflxServer `
-Bucket $bucket `
-Organisation $org `
-Token $iflxToken `
-Timestamp $_.lastUpdated `
-Measure waitTime `
-Metrics @{
status = [string]$_.status
$_.ride = [int]$_.waitTime
}
Write-Influx -Server $iflxServer `
-Bucket $bucket `
-Organisation $org `
-Token $iflxToken `
-Timestamp $_.lastUpdated `
-Measure rideState `
-Metrics @{
$_.ride = [string]$_.status
}
$count++
}
# Finishing
if ($?) { Write-Output "[$(Get-Logtime)] [OK] Scan completed. Bye!" }
$null = Stop-Transcript
Exit
# End of Script