From 6eb658d442ab2b3f9e8e106db8f6aad5461849b5 Mon Sep 17 00:00:00 2001 From: Dominika Liberda Date: Sun, 6 Jun 2021 19:52:17 +0200 Subject: [PATCH] + new pretty showTrain UI --- .gitignore | 3 +++ func.sh | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ pkp.sh | 27 +-------------------- showTrain.sh | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 26 deletions(-) create mode 100644 .gitignore create mode 100644 func.sh create mode 100755 showTrain.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..50bcbf2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +temp +uwu +owo diff --git a/func.sh b/func.sh new file mode 100644 index 0000000..ceb84ca --- /dev/null +++ b/func.sh @@ -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///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 "")" + + IFS=$'\n' + trains=$(grep -Poh '.*?
.*?
' <<< "$data") + links=($(grep -Poh "\?p\=train&id\=[0-9]+" <<< "$data")) + names=($(grep -Poh ">.*
.*" <<< "$trains" | sed -s 's// /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 '' <<< "$1" | grep -Poh 'span.*?//;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 +} diff --git a/pkp.sh b/pkp.sh index b69159d..0e995d0 100755 --- a/pkp.sh +++ b/pkp.sh @@ -2,32 +2,7 @@ # 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')) -} +source func.sh # trains if [[ $1 == "trains" ]]; then diff --git a/showTrain.sh b/showTrain.sh new file mode 100755 index 0000000..5e7bd1e --- /dev/null +++ b/showTrain.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +if [[ "$1" == '' ]]; then + echo "usage: $0 " +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