Skip to content

Instantly share code, notes, and snippets.

@jfisbein
Last active December 22, 2023 12:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jfisbein/f06e285cd8fadf0befe6 to your computer and use it in GitHub Desktop.
Save jfisbein/f06e285cd8fadf0befe6 to your computer and use it in GitHub Desktop.
Script para chequear numeros loteria de navidad usando api de El Pais
#!/usr/bin/env bash
# Script to check Spanish Christmas Lottery
# more info: https://servicios.elpais.com/sorteos/loteria-navidad/api/
AUTO_RELOAD="120" # seconds
declare -A mainPrizes
mainPrizes['numero1']='1er Premio'
mainPrizes['numero2']='2º Premio'
mainPrizes['numero3']='3er Premio'
mainPrizes['numero4']='4º Premio'
mainPrizes['numero5']='4º Premio'
mainPrizes['numero6']='5º Premio'
mainPrizes['numero7']='5º Premio'
mainPrizes['numero8']='5º Premio'
mainPrizes['numero9']='5º Premio'
mainPrizes['numero10']='5º Premio'
mainPrizes['numero11']='5º Premio'
mainPrizes['numero12']='5º Premio'
mainPrizes['numero13']='5º Premio'
ESC_RESET="\033[0m" # Reset
ESC_TITLE="\033[1m" # Bold
ESC_PRIZE="\033[32m" # Green
ESC_FAIL="\033[31m" # Red
if [ "$#" -lt 1 ]; then
echo "At least one parameter should be passed"
echo "Usage: $(basename $0) number:amount number2:amount2"
echo "Example: if you are playing 20€ on 43636 and 10€ on 12345 run '$(basename $0) 43636:20 12345:10'"
exit 1
fi
# Parse arguments
declare -A bets
for arg; do
bet=(${arg//:/ })
bets[${bet[0]}]=${bet[1]}
done
function getPrize() {
local number=$1
local amount=$2
number=$(echo $number | bc)
local prize=$(curl -s https://api.elpais.com/ws/LoteriaNavidadPremiados?n=$number | cut -d '=' -f 2 | jq -r '.premio')
local realPrize=$(echo "$amount * $prize / 20" | bc)
#realPrize=15
echo $realPrize
}
function getFace() {
local totalPlayed=$1
local totalWon=$2
local face
if (( $(echo "$totalPlayed > $totalWon" | bc -l) )); then
face="${ESC_FAIL}:-(${ESC_RESET}"
else
face="${ESC_PRIZE}:-)${ESC_RESET}"
fi
echo $face
}
function showResumen() {
local resp=$(curl -s https://api.elpais.com/ws/LoteriaNavidadPremiados?n=resumen | cut -d '=' -f 2)
local timestamp=$(echo ${resp} | jq ".timestamp")
echo -e "Data updated on: $(date +"%F %T" -d @${timestamp})\n"
echo -e "${ESC_TITLE}Prizes${ESC_RESET}"
local names=$(echo "${!mainPrizes[*]}" | tr ' ' '\n' | sort -k1.7n)
for name in $names; do
local desc=${mainPrizes["$name"]}
local number=$(echo $resp | jq ".$name")
if (( $number > 0 )); then
echo "$desc: $(printf "%05.0f" "$number")"
fi
done
}
function formatPrize() {
local prize=$1
prize=$(printf "%4s" "$prize")
if (( $prize > 0 )); then
prize="${ESC_PRIZE}$prize€${ESC_RESET}"
else
prize="$prize€"
fi
echo -e "$prize"
}
function showMyNumbers() {
local totalPlayed=0
local totalWon=0
echo -e "${ESC_TITLE}My Numbers${ESC_RESET}"
local numbers=$(echo "${!bets[*]}" | tr ' ' '\n' | sort -n)
for number in $numbers; do
local amount=${bets["$number"]}
local prize=$(getPrize $number $amount)
amount=$(printf "%5s" "$amount")
#exit 1
echo "Number: $(printf "%05.0f" "$number"), Played: ${amount}€, Won: $(formatPrize ${prize})"
totalPlayed=$(echo "$totalPlayed + $amount" | bc )
totalWon=$(echo "$totalWon + $prize" | bc)
done
local face=$(getFace $totalPlayed $totalWon)
totalWon=$(printf "%4s" "$totalWon")
echo '---------------------------------------'
echo -e "${ESC_TITLE}Totals. Played: ${totalPlayed}€, Won: ${totalWon}€ $face${ESC_RESET}"
}
function main() {
echo "Last api check: $(date +"%F %T")"
showResumen
echo
showMyNumbers
echo
}
clear
if ! which jq > /dev/null; then
echo 'You need to install jq to run this script'
echo 'On Linux execute "sudo apt-get install jq" and try again'
echo 'On OSx execute "brew install jq" and try again'
exit 1
fi
while [[ true ]]; do
clear
main
echo
echo "Waiting $AUTO_RELOAD seconds to refresh"
sleep $AUTO_RELOAD
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment