sendsummary.sh 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env bash
  2. #set -euo pipefail
  3. #
  4. # This simple script will post the weather plots given at
  5. # IMG[1-3] to a social media Account using the Mastodon API
  6. # (at least I hope, that it's compatible to the Mastodon API
  7. # because I use Mitra on my server).
  8. #
  9. # The images must be generated by another script and have to be located
  10. # in the same directory as the script.
  11. #
  12. # ===================================================================================
  13. # Change anything in the SECTION "EDIT THESE FOR YOUR NEEDS",
  14. # ===================================================================================
  15. #
  16. # It works (for me) but is still under developement
  17. # Improvements and changes may be done at any time
  18. # mostly there is no check if any command and function
  19. # is implemented on the machine,
  20. # nor if they are being executed correctly.
  21. # Use at your own risk.
  22. #
  23. # ===================================================================================
  24. # CONFIG — "EDIT THESE FOR YOUR NEEDS"
  25. # ===================================================================================
  26. BASE="https://INSTANCE.SOCIAL"
  27. USER="YOUR_USER"
  28. IMG1="YOUR_TEMERATURE_IMAGE_WITH_FILE_EXTENSION"
  29. IMG2="YOUR_PRESSURE_IMAGE_WITH_FILE_EXTENSION"
  30. IMG3="YOUR_HUMIDITY_IMAGE_WITH_FILE_EXTENSION"
  31. VISIBILITY="YOUR_NEEDED_VISIBILITY" # possible values: public / unlisted / private / direct
  32. STATUS_TEXT="YOUR_STATUS_MESSAGE"
  33. TOKEN="TOKEN_FROM_YOUR_FEDIVERSE_ACCOUNT"
  34. # ===================================================================================
  35. # END OF THE SECTION "EDIT THESE FOR YOUR NEEDS"
  36. # ===================================================================================
  37. upload_media() {
  38. local file="$1"
  39. local desc="$2"
  40. curl -s -H "Authorization: Bearer $TOKEN" -F "file=@${file}" -F "description=${desc}" "$BASE/api/v1/media"| jq -r '.id // .media_id // empty'
  41. }
  42. mid1=$(upload_media "$IMG1" "Temperature chart for $(date +%F)")
  43. mid2=$(upload_media "$IMG2" "Pressure chart for $(date +%F)")
  44. mid3=$(upload_media "$IMG3" "Humidity chart for $(date +%F)")
  45. if [[ -z "$mid1" || -z "$mid2" || -z "$mid3" ]]; then
  46. echo "Upload failed; responses:"
  47. echo "IMG1:"
  48. curl -s -H "Authorization: Bearer $TOKEN" -F "file=@${IMG1}" "$BASE/api/v1/media" | jq .
  49. echo "IMG2:"
  50. curl -s -H "Authorization: Bearer $TOKEN" -F "file=@${IMG2}" "$BASE/api/v1/media" | jq .
  51. echo "IMG3:"
  52. curl -s -H "Authorization: Bearer $TOKEN" -F "file=@${IMG3}" "$BASE/api/v1/media" | jq .
  53. exit 1
  54. fi
  55. media_array=$(jq -nc --arg a "$mid1" --arg b "$mid2" --arg c "$mid3" '[ $a, $b, $c ]')
  56. payload=$(jq -nc --arg status "$STATUS_TEXT" --argjson media_ids "$media_array" --arg vis "$VISIBILITY" \
  57. '{status: $status, media_ids: $media_ids, visibility: $vis}')
  58. curl -s -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d "$payload" "$BASE/api/v1/statuses" | jq .