add spectacle uploader

This commit is contained in:
2026-08-11 21:04:45 +02:00
parent 32a55b6816
commit 8fe81cf149

93
spectacle_uploader.sh Executable file
View File

@@ -0,0 +1,93 @@
#!/usr/bin/env bash
# ── 0. Load Environment Variables from .env ─────────────────────────────
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOTENV_PATH="${SCRIPT_DIR}/.env"
if [ -f "$DOTENV_PATH" ]; then
set -o allexport
source "$DOTENV_PATH"
set +o allexport
fi
# Environment variables from .env
F0CKM_URL="${F0CKM_URL}"
API_KEY="${API_KEY}"
RATING="${RATING}" # sfw, nsfw, or nsfl
TAGS="${TAGS}" # Comma-separated tags
VISIBILITY="${VISIBILITY}" # 0 = public, 1 = unlisted, 2 = private
# Notification app header title & preview dimensions
APP_NAME="${APP_NAME:-f0ckm}"
PREVIEW_MAX_HEIGHT="${PREVIEW_MAX_HEIGHT:-80}" # Max height in pixels without cropping
# ── 1. Clipboard Helper Function (Wayland / X11) ───────────────────────────
copy_to_clipboard() {
local content="$1"
if command -v wl-copy &>/dev/null; then
echo -n "$content" | wl-copy
elif command -v xclip &>/dev/null; then
echo -n "$content" | xclip -selection clipboard
elif command -v xsel &>/dev/null; then
echo -n "$content" | xsel -b
else
echo "Warning: No clipboard utility found (wl-clipboard or xclip)."
fi
}
# ── 1. Create Output Directory & Timestamped File Path ───────────────────
SCREENSHOT_DIR="${HOME}/Pictures/Screenshots"
mkdir -p "$SCREENSHOT_DIR"
SAVE_PATH="${SCREENSHOT_DIR}/screenshot_$(date +%Y%m%d_%H%M%S).png"
# ── 2. Capture Region Screenshot with Spectacle ──────────────────────────
spectacle -r -b -n -o "$SAVE_PATH"
# ── 3. Upload File & Process Response ────────────────────────────────────
if [ -f "$SAVE_PATH" ]; then
echo "Uploading '$SAVE_PATH' to ${F0CKM_URL}/api/v2/upload..."
RESPONSE=$(curl -s -X POST "${F0CKM_URL}/api/v2/upload" \
-H "X-Api-Key: ${API_KEY}" \
-F "file=@${SAVE_PATH}" \
-F "rating=${RATING}" \
-F "tags=${TAGS}" \
-F "visibility=${VISIBILITY}")
# Parse response JSON
SUCCESS=$(echo "$RESPONSE" | jq -r '.success // false')
ITEM_URL=$(echo "$RESPONSE" | jq -r '.url // empty')
MSG=$(echo "$RESPONSE" | jq -r '.msg // "Upload failed"')
if [ "$SUCCESS" = "true" ] && [ -n "$ITEM_URL" ]; then
# Copy URL to clipboard
copy_to_clipboard "$ITEM_URL"
# Create preview thumbnail scaled up to 80px height (no cropping/cutting)
PREVIEW_THUMB="/tmp/f0ckm_preview.png"
if command -v magick &>/dev/null; then
magick "$SAVE_PATH" -resize "x${PREVIEW_MAX_HEIGHT}>" "$PREVIEW_THUMB"
else
PREVIEW_THUMB="$SAVE_PATH"
fi
# HTML body with preview max height 80px (no cut), and link on a new line
BODY_TEXT="<div><a href=\"${ITEM_URL}\"><img src=\"file://${PREVIEW_THUMB}\" style=\"max-height:${PREVIEW_MAX_HEIGHT}px; max-width:100%;\" /></a></div><br/><br/><a href=\"${ITEM_URL}\">${ITEM_URL}</a>"
# Send Notification
notify-send \
-a "${APP_NAME}" \
"" \
"${BODY_TEXT}"
echo "Success! $ITEM_URL"
else
# Show error notification
notify-send -a "${APP_NAME}" -u critical "Upload Failed" "$MSG"
echo "Upload failed: $RESPONSE"
exit 1
fi
else
echo "Screenshot capture canceled."
fi