.dotfiles/.bin/screenshot

141 lines
3 KiB
Plaintext
Raw Permalink Normal View History

2021-03-30 20:59:59 +00:00
#!/usr/bin/env sh
2023-07-01 20:35:40 +00:00
tool="grim"
2024-09-25 23:32:25 +00:00
now=$(date '+%F_%H-%M-%S')
now_iso=$(date -Is)
expire_date=$(date -Is -d "+1hour")
rclone_remote="s3-serguzim-me:public/screenshots"
screenshot_host="https://s3.serguzim.me/public/screenshots"
2024-09-25 23:32:25 +00:00
screenshot_path="/tmp/screenshot_$now.webp"
file="$now.webp"
2021-03-30 20:59:59 +00:00
2021-07-09 22:50:17 +00:00
clip_image()
2021-04-23 17:38:05 +00:00
{
2023-07-01 20:35:40 +00:00
if [ -n "$WAYLAND_DISPLAY" ]
then
2023-07-06 21:38:19 +00:00
wl-copy -t image/png < "$screenshot_path"
2023-07-01 20:35:40 +00:00
else
xclip -t image/png "$screenshot_path" -selection clipboard
fi
2021-07-09 22:50:17 +00:00
}
2021-03-30 20:59:59 +00:00
2021-07-09 22:50:17 +00:00
upload_image()
{
2024-09-25 23:32:25 +00:00
share_url=$(upload_image_immich)
echo "$share_url"
if [ -n "$share_url" ]
then
notify-send "Screenshot" "Uploaded screenshot"
else
notify-send "Screenshot" "Failed to upload screenshot"
fi
xdg-open "$share_url"
# to have link available in clipboard manager
2023-07-01 20:35:40 +00:00
if [ -n "$WAYLAND_DISPLAY" ]
then
2024-09-25 23:32:25 +00:00
wl-copy "$share_url"
wl-copy -p "$share_url"
2023-07-01 20:35:40 +00:00
else
2024-09-25 23:32:25 +00:00
printf '%s' "$share_url" | xclip -selection clipboard
printf '%s' "$share_url" | xclip -selection primary
2023-07-01 20:35:40 +00:00
fi
2024-09-25 23:32:25 +00:00
}
upload_image_rclone()
{
set -e
rclone copyto "$screenshot_path" "$rclone_remote/$file" >/dev/null
echo "$screenshot_host/$file"
}
# Immich url to get api key for service account (skip oauth)
# https://my.immich.app/auth/login?autoLaunch=0
upload_image_immich()
{
set -e
result_upload=$(curl -sSfL "$IMMICH_INSTANCE_URL/api/assets" \
-H 'Content-Type: multipart/form-data' \
-H 'Accept: application/json' \
-H "x-api-key: $IMMICH_API_KEY_SCREENSHOTS" \
-F 'deviceId="screenshots"' \
-F "deviceAssetId=\"screenshots-$screenshot_path\"" \
-F "fileModifiedAt=\"$now_iso\"" \
-F "fileCreatedAt=\"$now_iso\"" \
-F "assetData=@\"$screenshot_path\"")
asset_id=$(echo "$result_upload" | jq -r '.id')
result_share=$(curl -sSfL "$IMMICH_INSTANCE_URL/api/shared-links" \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H "x-api-key: $IMMICH_API_KEY_SCREENSHOTS" \
-d "{
\"allowDownload\": true,
\"allowUpload\": false,
\"assetIds\": [
\"$asset_id\"
],
\"expiresAt\": \"$expire_date\",
\"showMetadata\": true,
\"type\": \"INDIVIDUAL\"
}")
share_key=$(echo "$result_share" | jq -r '.key')
2023-07-01 20:35:40 +00:00
2024-09-25 23:32:25 +00:00
#share_url="$IMMICH_INSTANCE_URL/share/$share_key/photos/$asset_id"
share_url="$IMMICH_INSTANCE_URL/api/assets/$asset_id/original?key=$share_key"
echo "$share_url"
2021-04-23 17:38:05 +00:00
}
2021-03-30 20:59:59 +00:00
2023-07-01 20:35:40 +00:00
call_screenshot_tool() {
case $tool in
flameshot)
result=$(flameshot gui -r | tee $screenshot_path | wc -c) ;;
grim)
2024-05-20 16:59:26 +00:00
result=$(grim -g "$(slurp -c bd93f9)" - | tee $screenshot_path | wc -c) ;;
2023-07-01 20:35:40 +00:00
*)
echo "Unknown screenshot tool: $tool"
return 1 ;;
esac
if [ "$result" -gt 0 ]
then
return 0
else
return 1
fi
}
2021-07-09 22:50:17 +00:00
main_no_upload()
{
2023-07-01 20:35:40 +00:00
if call_screenshot_tool
2021-04-23 17:38:05 +00:00
then
2023-07-06 21:38:19 +00:00
clip_image
2021-04-23 17:38:05 +00:00
fi
2021-07-09 22:50:17 +00:00
}
main()
{
if [ -n "$1" ] && [ -e "$1" ]
then
2024-09-25 23:32:25 +00:00
magick "$1" "$screenshot_path"
2023-07-01 20:35:40 +00:00
upload_image
clip_image
2021-07-09 22:50:17 +00:00
else
2023-07-01 20:35:40 +00:00
if call_screenshot_tool
2021-07-09 22:50:17 +00:00
then
2023-07-01 20:35:40 +00:00
upload_image
clip_image
2021-07-09 22:50:17 +00:00
fi
fi
}
case $1 in
2024-01-09 15:53:25 +00:00
"--no-upload") main_no_upload ;;
*) main "$1" ;;
2021-07-09 22:50:17 +00:00
esac