consolidated everything in one file, started writing another function

master
Dominika 2020-09-11 12:00:05 +02:00
parent 8d38c55b53
commit 5ecc50b6dd
4 changed files with 103 additions and 58 deletions

View File

@ -1,7 +1,9 @@
# sdomi's PKP tools
This is a (very) work-in-progress project; Currently, there are two messy scripts:
This is a (very) work-in-progress project;
`./platform.sh "Warszawa Centralna" "Opole Główne"` - gives links to PKP's static timetable page, usually with platforms listed
`./pkp.sh platform "Warszawa Centr" "Opole Głó"` - gives links to PKP's static timetable page, usually with platforms listed
`./trains.sh "Łódź Widzew"` - lists trains that will stop on station in the next few hours
`./pkp.sh trains "Łódź Widzew"` - lists trains that will stop on station in the next few hours
`./pkp.sh stops` - WiP, currently same as `trains` for 10-or-so common stations

98
pkp.sh Executable file
View File

@ -0,0 +1,98 @@
#!/bin/bash
# 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'))
}
# trains
if [[ $1 == "trains" ]]; then
station="$(getStations "$2")"
if [[ $(echo "$station" | grep -i "multiple") != '' ]]; then
echo "$station"
exit 0
fi
echo $station | grep -Poh "^[^:]+"
getTrains $station
for (( i=0; i<${#links[@]}; i++ )); do
echo ${links[$i]}: ${names[$i]}
done
fi
if [[ $1 == "stops" ]]; then
commonStations=(60608 33605 73312 7500 62653 273 30601 80416 27805 46409 60103 24000)
# 60608 - opole główne
# 33605 - Warszawa Centralna
# 73312 - Katowice
# 24000 - Białystok
# 7500 - Gdańsk Główny
# 62653 - Częstochowa
# 273 - Szczecin
# 30601 - Poznań
# 80416 - Krk
# 19703 - Toruń
# 27805 - Zielona Góra
# 46409 - Łódź Widzew
# 60103 - Wrocław
for i in ${commonStations[@]}; do
getTrains "?p=station&id=$i"
for (( i=0; i<${#links[@]}; i++ )); do
echo ${links[$i]}: ${names[$i]}
done
done
fi
if [[ $1 == "platform" ]]; then
stationStart=$(jq -nr --arg v "$2" '$v|@uri')
stationEnd=$(jq -nr --arg v "$3" '$v|@uri')
if [[ "$4" == "" ]]; then
time=$(date "+%H:%M")
else
time=$(date --date "$4" "+%H:%M")
fi
if [[ "$5" == "" ]]; then
date=$(date "+%d.%m.%y")
else
date=$(date --date "$5" "+%d.%m.%y")
fi
stationStartId=$(curl -s "https://rozklad-pkp.pl/station/search?term=$stationStart&short=false" | jq '.[0].value|tonumber')
stationEndId=$(curl -s "https://rozklad-pkp.pl/station/search?term=$stationEnd&short=false" | jq '.[0].value|tonumber')
detailsUrl=$(curl -s -c /tmp/cookiemonster.txt "https://rozklad-pkp.pl/pl/tp?queryPageDisplayed=yes&REQ0JourneyStopsS0A=1&REQ0JourneyStopsS0G=$stationStartId&REQ0JourneyStopsZ0A=1&REQ0JourneyStopsZ0G=$stationEndId&date=$date&time=$time&REQ0JourneyProduct_opt_section_0_list=1:100000&start=start&came_from_form=1" | grep -Poh '<a class="do-print".*?Rozwi&#324; wszystkie' | sed -E 's/.*href="//g;s/" >.*//;s/\&amp;/\&/g;s/\&\&/\&/g;')
echo "https://rozklad-pkp.pl$detailsUrl"
echo ---
curl -s -b /tmp/cookiemonster.txt "https://rozklad-pkp.pl$detailsUrl" | grep -Poh 'trainlink.*?"' | sed -s 's/\&amp;/\&/g;s/\&\&/\&/g;s/trainlink/http\:\/\/rozklad-pkp.pl\/pl\/ti?&/g;s/&"$//g'
rm /tmp/cookiemonster.txt
fi

View File

@ -1,20 +0,0 @@
#!/bin/bash
stationStart=$(jq -nr --arg v "$1" '$v|@uri')
stationEnd=$(jq -nr --arg v "$2" '$v|@uri')
stationStartId=$(curl -s "https://rozklad-pkp.pl/station/search?term=$stationStart&short=false" | jq '.[0].value|tonumber')
stationEndId=$(curl -s "https://rozklad-pkp.pl/station/search?term=$stationEnd&short=false" | jq '.[0].value|tonumber')
time=$(date "+%H:%M")
date=$(date "+%d.%m.%y")
detailsUrl=$(curl -s -c cookies.txt "https://rozklad-pkp.pl/pl/tp?queryPageDisplayed=yes&REQ0JourneyStopsS0A=1&REQ0JourneyStopsS0G=$stationStartId&REQ0JourneyStopsZ0A=1&REQ0JourneyStopsZ0G=$stationEndId&date=$date&time=$time&REQ0JourneyProduct_opt_section_0_list=1:100000&start=start&came_from_form=1" | grep -Poh '<a class="do-print".*?Rozwi&#324; wszystkie' | sed -E 's/.*href="//g;s/" >.*//;s/\&amp;/\&/g;s/\&\&/\&/g;')
echo "https://rozklad-pkp.pl$detailsUrl"
echo ---
curl -s -b cookies.txt "https://rozklad-pkp.pl$detailsUrl" | grep -Poh 'trainlink.*?"' | sed -s 's/\&amp;/\&/g;s/\&\&/\&/g;s/trainlink/http\:\/\/rozklad-pkp.pl\/pl\/ti?&/g;s/&"$//g'
rm cookies.txt

View File

@ -1,35 +0,0 @@
#!/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