Advertisement
Guest User

Untitled

a guest
Apr 26th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.79 KB | None | 0 0
  1. set -euo pipefail
  2. GOAL=lichess-fast
  3. BEE="https://www.beeminder.com/api/v1"
  4. BEE_AUTH="auth_token=$(pass show beeminder-auth-token)"
  5.  
  6. TMP_FILE=$(mktemp)
  7. curl -s "${BEE}/users/znewman01/goals/lichess-fast/datapoints.json?${BEE_AUTH}&count=1" > $TMP_FILE
  8. BEE_GAMES=$(jq first.value $TMP_FILE)
  9. echo "Current Beeminder # of games: ${BEE_GAMES}"
  10.  
  11. LI_GAMES=$(curl -s https://lichess.org/api/user/znewman01 | jq '.perfs.bullet.games + .perfs.blitz.games')
  12. echo "Current Lichess # of games: ${LI_GAMES}"
  13.  
  14. if [ $LI_GAMES -eq $BEE_GAMES ]; then
  15.     echo "Beeminder is up-to-date; exiting."
  16.     exit 0
  17. fi
  18.  
  19. echo "Posting the new data point..."
  20. DATAPOINT_ID=$(curl -s -X POST \
  21.     --data "${BEE_AUTH}&value=${LI_GAMES}" \
  22.     "${BEE}/users/znewman01/goals/${GOAL}/datapoints.json" \
  23.     | jq '.id' | sed 's/"//g')
  24.  
  25. sleep 1
  26.  
  27. BAREMIN=$(curl -s "${BEE}/users/znewman01/goals/lichess-fast?${BEE_AUTH}&count=1" | jq '.baremin' | sed 's/["+]//g')
  28. if [ $BAREMIN -ge 0 ]; then
  29.     echo "Didn't derail!"
  30.     exit
  31. fi
  32.  
  33. TOTAL_CHARGE=$(expr 0 - $BAREMIN || true)
  34. echo "Total to charge: $TOTAL_CHARGE"
  35.  
  36. ALREADY_CHARGED=$(jq 'try (first.comment | tonumber) catch 0' $TMP_FILE | sed 's/"//g')
  37. echo "Already charged today: ${ALREADY_CHARGED}"
  38.  
  39. TO_CHARGE=$(expr $TOTAL_CHARGE - $ALREADY_CHARGED || true)
  40. echo "Need to charge: ${TO_CHARGE}"
  41.  
  42. if [ $TO_CHARGE -gt 0 ]; then
  43.     echo "Charging..."
  44.     echo "(not really)"
  45.     echo curl -s -X POST "${BEE}/charges.json?${BEE_AUTH}&amount=${TO_CHARGE}&note=lichess-fast"
  46. else
  47.     echo "Not charging."
  48. fi
  49. echo curl -X PUT "${BEE}/users/znewman01/goals/lichess-fast/datapoints/${DATAPOINT_ID}.json?${BEE_AUTH}&comment=$TOTAL_CHARGE"
  50. curl -X PUT "${BEE}/users/znewman01/goals/lichess-fast/datapoints/${DATAPOINT_ID}.json?${BEE_AUTH}&comment=$TOTAL_CHARGE"
  51.  
  52. rm -f "${TMP_FILE}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement