pkp-tools/trains.sh

36 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
function station() {
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
}
station="$(station "$1")"
if [[ $(echo "$station" | grep -i "multiple") != '' ]]; then
echo "$station"
exit 0
fi
url="http://infopasazer.intercity.pl/$(echo $station | grep -Poh "^[^:]+")"
data="$(curl $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'))
for (( i=0; i<${#links[@]}; i++ )); do
echo ${links[$i]}: ${names[$i]}
done