diff --git a/README.md b/README.md index 89d87c2..26c9a36 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pkp.sh b/pkp.sh new file mode 100755 index 0000000..b69159d --- /dev/null +++ b/pkp.sh @@ -0,0 +1,98 @@ +#!/bin/bash +# PKP.sh - common script housing all pkp-tools functions +# ./pkp.sh (platform|stops|trains) + +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///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 "")" + + IFS=$'\n' + trains=$(echo $data | grep -Poh '.*?
.*?
') + links=($(echo $data | grep -Poh "\?p\=train&id\=[0-9]+")) + names=($(echo "$trains" | grep -Poh ">.*
.*" | sed -s 's// /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 '.*//;s/\&/\&/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/\&/\&/g;s/\&\&/\&/g;s/trainlink/http\:\/\/rozklad-pkp.pl\/pl\/ti?&/g;s/&"$//g' + rm /tmp/cookiemonster.txt +fi diff --git a/platform.sh b/platform.sh deleted file mode 100755 index 72609ea..0000000 --- a/platform.sh +++ /dev/null @@ -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 '.*//;s/\&/\&/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/\&/\&/g;s/\&\&/\&/g;s/trainlink/http\:\/\/rozklad-pkp.pl\/pl\/ti?&/g;s/&"$//g' -rm cookies.txt diff --git a/trains.sh b/trains.sh deleted file mode 100755 index 4517b57..0000000 --- a/trains.sh +++ /dev/null @@ -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///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 "")" - -IFS=$'\n' -trains=$(echo $data | grep -Poh '.*?
.*?
') -links=($(echo $data | grep -Poh "\?p\=train&id\=[0-9]+")) -names=($(echo "$trains" | grep -Poh ">.*
.*" | sed -s 's// /g;s/<\/a>//g;s/^>//g')) - -for (( i=0; i<${#links[@]}; i++ )); do - echo ${links[$i]}: ${names[$i]} -done