-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvocal_message.ps1
61 lines (50 loc) · 2.22 KB
/
vocal_message.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
# REQUIRED LIBRARIES
Add-Type -AssemblyName System.speech; # SPEECH SYNTHETIS
. ./apiservice.ps1 # if executed from task-scheduler... define full path here for the .ps1 file OR specifying the running directory for scheduled task (-WorkingDirectory argument)
function getSchedules() {
# GET DATA AS JSON
$apiService = [ApiService]::new($apiParams)
$apiJsonResponse = ConvertTo-Json $apiService.Invoke()
# FILTER DATA
$dataFilter = "[.[]| select(.station==$stationOrigin and .destination==$stationDestination" + ')] | group_by(.line, .updated) | map({ "line": .[0].line, "updated": .[0].updated, "delays_minutes": map(.delay/60) | unique })'
$filteredResponse = $apiJsonResponse | jq $dataFilter
return $filteredResponse
<# [ {
"line": "R1",
"updated": "2022-02-22T17:04:43",
"delays_minutes": [ 1.2833333333333334, 7.283333333333333 ] } ]
#>
}
function vocalNotification($schedules) {
$nextBuses = $schedules | ConvertFrom-Json
$speaker = New-Object System.Speech.Synthesis.SpeechSynthesizer;
foreach ($bus in $nextBuses)
{
write-host " Line $($bus.line)"
$speaker.Speak("Next bus to Saint Germain-en-Laye, line $($bus.line.split('')) in");
$bus.delays_minutes | foreach {
$nextDeparture = [System.Math]::Floor($_)
$sentence = ($nextDeparture -lt 1) ? "less than one minute" : "$($nextDeparture) minutes";
write-host " 🚌 $sentence"
$speaker.Speak($sentence);
}
}
$speaker.Dispose()
}
# API CALL PARAMETERS
$apiParams = @{
contentType = 'application/json';
method = 'POST';
body = 'token=undefined';
uri = 'https://www.transdev-idf.com/ajax/station/540445338/nextbus';
headers = @{ 'Host' = 'www.transdev-idf.com'
'Accept' = 'application/json, text/javascript, */*; q=0.01'
'X-Requested-With' = 'XMLHttpRequest'
'Origin' = 'https://www.transdev-idf.com'
'Accept-Encoding' = 'gzip, deflate, br'};
}
# Data Filter Parameters
$stationOrigin = "540445338" # Lycée Leonard de Vinci
$stationDestination = "50012439" # Saint Germain en Laye RER
$schedules = getSchedules
vocalNotification($schedules)