#!/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