Last active
April 4, 2017 20:14
-
-
Save carestad/702f76c60a6d91d6084396083d6c0f6b to your computer and use it in GitHub Desktop.
Skyss travel planner [Bash style]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# By: Alexander Karlstad <alexanderkarlstad@gmail.com> | |
# | |
# Requires 'jq' and 'curl' | |
# | |
# To install (on Ubuntu): | |
# sudo apt install jq curl | |
function halp() { | |
echo "Usage: $0 <from> <to> [time]" | |
echo | |
echo " From: tab completion suggests stop names" | |
echo " To: tab completion suggests stop names" | |
echo " Time: Some pre-defined terms in tab completion. Supports whatever the 'date' command's --date argument allows" | |
} | |
api_base=https://api.skyss.no/ws/mobile | |
api_auth="mobile:g7pEtkRF@" | |
from=$1 | |
to=$2 | |
time=$3 | |
if [ -z "$1" -o -z "$2" ]; then | |
halp | |
exit 1 | |
fi | |
if [ -z "$3" ]; then | |
time="+5 minutes" | |
fi | |
echo "Reise fra $from til $to, klokken `date +%H:%M -d "$time"`" | |
echo | |
from_json=`curl -G -s -u "${api_auth}" ${api_base}/places/ --data-urlencode "q=$1" | jq ".places[] | select(.type != \"street\" and .Description == \"$1\")"` | |
to_json=`curl -G -s -u "${api_auth}" ${api_base}/places/ --data-urlencode "q=$2" | jq ".places[] | select(.type != \"street\" and .Description == \"$2\")"` | |
from_id=`echo $from_json | jq -r .Identifier` | |
to_id=`echo $to_json | jq -r .Identifier` | |
from_gps=`echo $from_json | jq -r .Location` | |
to_gps=`echo $to_json | jq -r .Location` | |
timestamp=`date +%Y-%m-%dT%H:%M:%S%:z -d "$time"` | |
results=`curl -G -s -u "${api_auth}" ${api_base}/travelplans --data-urlencode "FromLocation=${from_gps}" --data-urlencode "ToLocation=${to_gps}" --data-urlencode "TS=${timestamp}" --data-urlencode "TimeType=Departure" --data-urlencode "FromStopGroupID=${from_id}" --data-urlencode "ToStopGroupID=${to_id}"` | |
plans=`echo $results | jq .TravelPlans` | |
plans_n=`echo $plans | jq ". | length"` | |
j=0 | |
while [ "$j" -lt "$plans_n" ]; do | |
plan=`echo "$results" | jq ".TravelPlans[${j}]"` | |
steps_n=`echo "$plan" | jq ".TravelSteps | length"` | |
plan_start=`echo $plan | jq -r .StartTime` | |
plan_end=`echo $plan | jq -r .EndTime` | |
echo "Start `date +%H:%M -d "${plan_start}"`, slutt `date +%H:%M -d "${plan_end}"`" | |
i=0 | |
while [ "$i" -lt "$steps_n" ]; do | |
step=`echo $plan | jq .TravelSteps[$i]` | |
if [ $i = $((steps_n-1)) ]; then | |
step_next=$to | |
else | |
step_next=`echo $plan | jq -r .TravelSteps[$((i+1))].Stop.Description` | |
fi | |
type=`echo $step | jq -r .Type` | |
duration=`echo $step | jq -r .Duration | sed 's/PT\([0-9]\+\)H\([0-9]\+\)M/\1t\2m/' | sed 's/PT\([0-9]\+\)M/\1m/'` | |
this=`echo $step | jq -r .Stop.Description` | |
mode=`echo $step | jq -r .RouteDirection.ServiceMode` | |
mode=${mode,,} | |
mode=${mode//bus/buss} | |
mode=${mode//light rail/bybane} | |
mode_nr=`echo $step | jq -r .RouteDirection.PublicIdentifier` | |
mode_dir=`echo $step | jq -r .RouteDirection.DirectionName` | |
step_time=`date +%H:%M -d $(echo $step | jq -r .StartTime)` | |
if [ "$type" = "walk" ]; then | |
echo "[${step_time}] Gå fra ${this} til ${step_next} (${duration})" | |
fi | |
if [ "$type" = "route" ]; then | |
echo "[${step_time}] Gå på ${mode} ${mode_nr} ${mode_dir} og sitt på til ${step_next} (${duration})" | |
fi | |
let i=i+1 | |
done | |
let j=j+1 | |
echo | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_have skyss.sh && _skyss_complete() { | |
local cur=${COMP_WORDS[COMP_CWORD]} | |
local IFS=$'\n' | |
case $COMP_CWORD in | |
1|2) | |
cur_s=${cur//\\ /%20} | |
api_base="https://api.skyss.no/ws/mobile" | |
stops=`curl -s -u 'mobile:g7pEtkRF@' $api_base/places/?q=${cur_s} | jq '.places[] | select(.type != "street") | .Description'` | |
COMPREPLY=($(compgen -W "${stops}" -- $cur)) | |
;; | |
3) | |
times="now\n+10 minutes\n+15 minutes\n-5 minutes\n-10 minutes\n-15 minutes\n+30 minutes\n-30 minutes" | |
COMPREPLY=($(compgen -W "$(echo -e $times)" -- $cur)) | |
;; | |
esac | |
} && complete -o filenames -F _skyss_complete skyss.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment