+ new pretty showTrain UI

master
Dominika Liberda 2021-06-06 19:52:17 +02:00
parent 5ecc50b6dd
commit 6eb658d442
4 changed files with 130 additions and 26 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
temp
uwu
owo

66
func.sh Normal file
View File

@ -0,0 +1,66 @@
#!/bin/bash
function getStations() {
a="$@"
s=$(jq -nr --arg v "$a" '$v|@uri')
data=$(curl -s "http://infopasazer.intercity.pl/?p=stations&q=$s")
IFS=$'\n'
links=($(grep -Poh "\?p\=station\&id\=[0-9]+" <<< "$data"))
names=($(grep -i ">$@" <<< "$data" | tr -d '\r' | sed -s 's/<span class="">//g;s/ //g;s/<\/span>//g'))
if [[ ${#links[@]} -gt 1 ]]; then
echo "Multiple stations found! Narrow your search down, pretty please"
fi
for (( i=0; i<${#links[@]}; i++ )); do
echo "${links[$i]}:${names[$i]}"
done
}
function getTrains() {
url="http://infopasazer.intercity.pl/$(grep -Poh "^[^:]+" <<< $1)"
data="$(curl -s $url | tr -d '\r' | tr -d '\n' | grep -Poh "<tr.*?</tr>")"
IFS=$'\n'
trains=$(grep -Poh '<a href=".*?">.*?<br/>.*?</a>' <<< "$data")
links=($(grep -Poh "\?p\=train&id\=[0-9]+" <<< "$data"))
names=($(grep -Poh ">.*<br/>.*</a>" <<< "$trains" | sed -s 's/<br\/>/ /g;s/<\/a>//g;s/^>//g'))
}
function getTrainInfo() {
IFS=$'\n'
res=($(curl -s "http://infopasazer.intercity.pl/?p=train&id=$1" | tr -d '\n\r' | sed -E 's/ //g' | grep -Poh '<tr class="_TableDelayCol.*?</tr>'))
unset IFS
}
function parseStationInfo() {
IFS=$'\n'
local -n _station=$2
uwu=($(grep -Poh '<td class="Col".*?</td>' <<< "$1" | grep -Poh 'span.*?</s' | sed -E 's@</a.*@@;s/.*>//;s/<\/s//'))
_station[train_number]=${uwu[0]}
_station[date]=${uwu[1]}
_station[relation]=${uwu[2]}
_station[name]=${uwu[3]}
_station[arrival]=${uwu[4]}
_station[arrival_delay]=${uwu[5]}
_station[departure]=${uwu[6]}
_station[departure_delay]=${uwu[7]}
unset IFS
}
function getCurrentStation() {
IFS=$'\n';
local -n _dat=$1
local -n _res=$2
for (( i=0; i<${#_dat[@]}; i++ )); do
if [[ "$(grep current <<< "${_dat[$i]}")" != '' ]]; then
_res=$i
break
fi
done
}

27
pkp.sh
View File

@ -2,32 +2,7 @@
# PKP.sh - common script housing all pkp-tools functions
# ./pkp.sh (platform|stops|trains) <params>
function getStations() {
a="$@"
s=$(jq -nr --arg v "$a" '$v|@uri')
data=$(curl -s "http://infopasazer.intercity.pl/?p=stations&q=$s")
IFS=$'\n'
links=($(echo "$data" | grep -Poh "\?p\=station\&id\=[0-9]+"))
names=($(echo "$data" | grep -i ">$@" | tr -d '\r' | sed -s 's/<span class="">//g;s/ //g;s/<\/span>//g'))
if [[ ${#links[@]} -gt 1 ]]; then
echo "Multiple stations found! Narrow your search down, pretty please"
fi
for (( i=0; i<${#links[@]}; i++ )); do
echo "${links[$i]}:${names[$i]}"
done
}
function getTrains() {
url="http://infopasazer.intercity.pl/$(echo $1 | grep -Poh "^[^:]+")"
data="$(curl -s $url | tr -d '\r' | tr -d '\n' | grep -Poh "<tr.*?</tr>")"
IFS=$'\n'
trains=$(echo $data | grep -Poh '<a href=".*?">.*?<br/>.*?</a>')
links=($(echo $data | grep -Poh "\?p\=train&id\=[0-9]+"))
names=($(echo "$trains" | grep -Poh ">.*<br/>.*</a>" | sed -s 's/<br\/>/ /g;s/<\/a>//g;s/^>//g'))
}
source func.sh
# trains
if [[ $1 == "trains" ]]; then

60
showTrain.sh Executable file
View File

@ -0,0 +1,60 @@
#!/bin/bash
if [[ "$1" == '' ]]; then
echo "usage: $0 <train_id>"
fi
source func.sh
function drawLine() {
printf "%"$1"s" | tr ' ' '*'
echo ""
}
getTrainInfo $1
getCurrentStation res currentId
declare -A start_station end_station current_station
parseStationInfo "${res[0]}" start_station
parseStationInfo "${res[-1]}" end_station
parseStationInfo "${res[$currentId]}" current_station
declare -A train
train[name]="${start_station[train_number]}"
train[from]="${start_station[name]}"
train[to]="${end_station[name]}"
train[current]="${current_station[name]}"
train[delay]="${current_station}"
display+=("${train[name]}")
display+=("${train[from]} ---> ${train[to]}")
display+=("")
display+=("${train[current]} (${current_station[arrival]} - ${current_station[departure]})")
display+=("Delayed: ${current_station[arrival_delay]}")
display+=("")
display+=("Luna is cute:3")
for i in "${display[@]}"; do
length=$(wc -m <<< "$i")
if [[ "$length" -gt "$maxlength" ]]; then
maxlength=$length
fi
done
maxlength=$((maxlength-1))
for (( i=0; i<${#display[@]}; i++ )); do
l="$(wc -m <<< "${display[$i]}")"
space=$(((maxlength-l)/2))
display[$i]="$(printf "%"$space"s")${display[$i]}$(printf "%"$space"s")"
done
drawLine $maxlength
IFS=$'\n'
for i in "${display[@]}"; do
echo "$i"
done
drawLine $maxlength