Last active
August 29, 2015 14:02
-
-
Save abiyani/34f25dc8ed2a862517e7 to your computer and use it in GitHub Desktop.
Display current World Cup 2014 match scores as part of terminal prompt (requires jq, and wget/curl)
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
# You will need to have jq (http://stedolan.github.io/jq/) and wget/curl installed on your system before being able to use this script. | |
# | |
# Usage: Include the following bash snippet in your ~/.bashrc file, or save as a separate file and source it. | |
# | |
# Example prompt generated by the script: | |
# abiyani@vaio:~ [BEL-2-1-ALG, RUS-1-1-KOR, BRA-0-0-MEX] | |
# | |
# Note: By default it display score for only active matches, and if there is no active match at the moment, then prompt remains unchanged. | |
function get_scores() { | |
local current_ts="$(date +%s)" | |
if [[ $((current_ts-_LAST_GET_SCORES_TS)) -ge 60 ]]; then | |
_LAST_GET_SCORES_TS="$current_ts" | |
# Comment & uncomment appropriate combination of 4 lines below to make a decision | |
# about which http client to use, and which score(s) to display. | |
local http_agent="wget -q -O - " # Use wget to make http request | |
#local http_agent="curl -s " # Use curl to make http request | |
local json="$($http_agent http://worldcup.sfg.io/matches/current)" # Only show score for active matches | |
#local json="$($http_agent http://worldcup.sfg.io/matches/today)" # Show score for all matches played today | |
local out i t | |
for (( i=0;i<5;i++ )); do | |
t="$(echo "$json"| printf "%s" "$(jq -r ".[$i].home_team.code,.[$i].home_team.goals,.[$i].away_team.goals,.[$i].away_team.code" 2>/dev/null)" | tr '\n' '-')"; | |
if [[ -z "$t" || "$t" =~ null ]]; then break; fi; | |
if [[ -z "$out" ]];then | |
out="$t"; | |
else | |
out+=", $t"; | |
fi | |
done | |
if [[ ! -z "$out" ]]; then | |
_LAST_GET_SCORES_VAL="[$out] " | |
else | |
_LAST_GET_SCORES_VAL="" | |
fi | |
fi | |
} | |
PROMPT_COMMAND='get_scores' | |
PS1+='`printf "%s" "$_LAST_GET_SCORES_VAL"`' # Append output of get_scores() to PS1. Feel free to tweak to any alternative format you like. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment