diff options
author | doc <doc@filenotfound.org> | 2025-06-30 20:11:52 +0000 |
---|---|---|
committer | doc <doc@filenotfound.org> | 2025-06-30 20:11:52 +0000 |
commit | 41e897f4945aaf8fbcdf0b12ac2f08c5e6ae0458 (patch) | |
tree | db7c3520fd91abc3cf56b1a52095d23f3a80d059 /linux_masto.sh |
Diffstat (limited to 'linux_masto.sh')
-rwxr-xr-x | linux_masto.sh | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/linux_masto.sh b/linux_masto.sh new file mode 100755 index 0000000..3e8c92f --- /dev/null +++ b/linux_masto.sh @@ -0,0 +1,52 @@ +#!/bin/bash +# Posts a Linux tip via Mastodon API, with fallback and logging + +INSTANCE="https://chatwithus.live" # <-- Change this! +TOKEN_FILE="/home/doc/genesis-tools/miscellaneous/bash/bin/mastodon_token.secret" +TIP_API="https://linuxtldr.com/api/tips/random" +FALLBACK_FILE="/home/doc/linux_tips.txt" +LOG_FILE="/home/doc/linux_masto_post.log" + +# Load access token +if [[ ! -f "$TOKEN_FILE" ]]; then + echo "Missing access token at $TOKEN_FILE" + exit 1 +fi +ACCESS_TOKEN=$(<"$TOKEN_FILE") + +# Function to log and exit +fail_and_log() { + echo "$(date): ERROR - $1" >> "$LOG_FILE" + echo "ā $1" + exit 1 +} + +# Try fetching a tip from API +response=$(curl -s "$TIP_API") + +if echo "$response" | jq . >/dev/null 2>&1; then + title=$(echo "$response" | jq -r '.title') + tip=$(echo "$response" | jq -r '.tip') + url=$(echo "$response" | jq -r '.url') + POST="š *Linux Tip of the Day*\n\n$title\n\n$tip\n\nš More: $url\n\n#Linux #CommandLine #SysAdmin" +else + # API failed, use fallback + if [[ ! -f "$FALLBACK_FILE" ]]; then + fail_and_log "Both API and fallback file failed. No tips to post." + fi + POST="š *Linux Tip of the Day*\n\n$(shuf -n 1 "$FALLBACK_FILE")\n\n#Linux #CommandLine #SysAdmin" + echo "$(date): Used fallback tip." >> "$LOG_FILE" +fi + +# Post to Mastodon +resp=$(curl -s -X POST "$INSTANCE/api/v1/statuses" \ + -H "Authorization: Bearer $ACCESS_TOKEN" \ + -d "status=$POST" \ + -d "visibility=public") + +# Check response +if echo "$resp" | grep -q '"id":'; then + echo "$(date): ā
Posted successfully: $(echo "$POST" | head -n 1)" >> "$LOG_FILE" +else + fail_and_log "Post failed. Response: $resp" +fi |