فهرست منبع

'sendsummary.sh' hinzufügen

This simple script will post the weather plots given at IMG[1-3] to a social media Account using the Mastodon API (at least I hope, that it's fully compatible to the Mastodon API because I use Mitra on my server).
Stephan Schneider 2 ماه پیش
والد
کامیت
5b57dbb341
1فایلهای تغییر یافته به همراه65 افزوده شده و 0 حذف شده
  1. 65 0
      sendsummary.sh

+ 65 - 0
sendsummary.sh

@@ -0,0 +1,65 @@
+#!/usr/bin/env bash
+#set -euo pipefail
+# 
+# This simple script will post the weather plots given at
+# IMG[1-3] to a social media Account using the Mastodon API
+# (at least I hope, that it's compatible to the Mastodon API
+# because I use Mitra on my server).
+# 
+# The images must be generated by another script and have to be located
+# in the same directory as the script.
+# 
+# ===================================================================================
+# Change anything in the SECTION "EDIT THESE FOR YOUR NEEDS",  
+# ===================================================================================
+# 
+# It works (for me) but is still under developement
+# Improvements and changes may be done at any time
+# mostly there is no check if any command and function
+# is implemented on the machine,
+# nor if they are being executed correctly.
+# Use at your own risk.
+# 
+# ===================================================================================
+# CONFIG — "EDIT THESE FOR YOUR NEEDS"
+# ===================================================================================
+
+BASE="https://INSTANCE.SOCIAL"
+USER="YOUR_USER"
+IMG1="YOUR_TEMERATURE_IMAGE_WITH_FILE_EXTENSION"
+IMG2="YOUR_PRESSURE_IMAGE_WITH_FILE_EXTENSION"
+IMG3="YOUR_HUMIDITY_IMAGE_WITH_FILE_EXTENSION"
+VISIBILITY="YOUR_NEEDED_VISIBILITY"          # possible values: public / unlisted / private / direct
+STATUS_TEXT="YOUR_STATUS_MESSAGE"
+TOKEN="TOKEN_FROM_YOUR_FEDIVERSE_ACCOUNT"
+
+# ===================================================================================
+# END OF THE SECTION "EDIT THESE FOR YOUR NEEDS"
+# ===================================================================================
+
+upload_media() {
+  local file="$1"
+  local desc="$2"
+  curl -s -H "Authorization: Bearer $TOKEN" -F "file=@${file}" -F "description=${desc}" "$BASE/api/v1/media"| jq -r '.id // .media_id // empty'
+}
+
+mid1=$(upload_media "$IMG1" "Temperature chart for $(date +%F)")
+mid2=$(upload_media "$IMG2" "Pressure chart for $(date +%F)")
+mid3=$(upload_media "$IMG3" "Humidity chart for $(date +%F)")
+
+if [[ -z "$mid1" || -z "$mid2" || -z "$mid3" ]]; then
+  echo "Upload failed; responses:"
+  echo "IMG1:"
+  curl -s -H "Authorization: Bearer $TOKEN" -F "file=@${IMG1}" "$BASE/api/v1/media" | jq .
+  echo "IMG2:"
+  curl -s -H "Authorization: Bearer $TOKEN" -F "file=@${IMG2}" "$BASE/api/v1/media" | jq .
+  echo "IMG3:"
+  curl -s -H "Authorization: Bearer $TOKEN" -F "file=@${IMG3}" "$BASE/api/v1/media" | jq .
+  exit 1
+fi
+
+media_array=$(jq -nc --arg a "$mid1" --arg b "$mid2" --arg c "$mid3" '[ $a, $b, $c ]')
+payload=$(jq -nc --arg status "$STATUS_TEXT" --argjson media_ids "$media_array" --arg vis "$VISIBILITY" \
+  '{status: $status, media_ids: $media_ids, visibility: $vis}')
+
+curl -s -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d "$payload" "$BASE/api/v1/statuses" | jq .