~emersion/dotfiles

51fd2089234da079f5dd2af0e95f115d85497665 — Simon Ser 4 months ago 1257aaf
bin/srht: update for legacy.sr.ht migration
1 files changed, 9 insertions(+), 136 deletions(-)

M bin/srht
M bin/srht => bin/srht +9 -136
@@ 1,141 1,14 @@
#!/bin/bash
#
# srht - Upload files/scrots/urls to sr.ht (rip pomf & uguu)
# By onodera, modified by SirCmpwn
#!/bin/sh -eu

## CONFIGURATION
source ~/.config/srht
. ~/.config/sr.ht

## FUNCTIONS

# This function sets $file to a selection scrot
selection() {
	uploadme="/tmp/scrot.""$screenshot_extension"

	maim --hidecursor -s -b 3 -c 250,250,250 "$uploadme" 2> "/dev/null"
	if [[ "$?" -ge 1 ]]; then
		echo "Selection cancelled."
		exit 1
	fi

	word=selection
}

# This function sets $file to your clipboard contents
clipboard() {
	uploadme="/tmp/scrot.txt"

	xclip -sel clipboard -o > "$uploadme"

	word=clipboard
}

# This function sets $file an url
url() {
	type="$(echo "$location" | rev | cut -d "." -f 1 | rev)"
	uploadme="/tmp/url.$type"

	wget --quiet "$location" -O "$uploadme"

	word=url
}

# This function sets $file a file
file() {
	if [[ -f "$location" ]]; then
		uploadme="$location"
	else
		echo "File not found."
		exit 1
	fi

	word=file
}

# This function sets $file to a full screen scrot
desktop() {
	uploadme="/tmp/scrot.""$screenshot_extension"

	if [ -n "$SWAYSOCK" ]; then
		swaygrab "$uploadme"
	else
		maim --hidecursor "$uploadme"
	fi

	word=desktop
}

# This function uploads the $file
upload() {
	url="$(curl --silent -F key="$key" -F file="@$uploadme" "https://sr.ht/api/upload" | grep -o -i "https://sr.ht/*.[a-z0-9._-]*")"
}

# This function logs the url,  copies the url to the clipboard, and/or opens the url in your browser
feedback() {
	# Copy url to clipboard
	if [[ "$clipboard" == true ]]; then
		echo "$url" | xclip -selection primary
		echo "$url" | xclip -selection clipboard
	fi

	# Log url
	if [[ "$log" == true ]]; then
		echo "$url" >> "$logfile"
	fi

	# Open url in browser
	if [[ "$browser" == true ]]; then
		xdg-open "$url"
	fi

	# Send notification
	if [[ "$notify" == true ]]; then
		notify-send "Upload complete: $url"
	fi

	echo "$url"
}

## EXECUTE

if [[ "$#" -ge 1 ]]; then
	case "$@" in
		-h|--help)
			echo "usage: srht [options] [file/url]"
			echo "options:"
			echo "  -h,   --help            print help and exit"
			echo "  -p,   --paste       	upload your clipboard as text"
			echo "  -s,   --selection       upload selection scrot"
			echo "  -v,   --version         print version and exit"
			exit 0
			;;
		-s|--selection)
			selection
			;;
		-v|--version)
			echo "srht 0.9.1"
			exit 0
			;;
		-p|--paste)
			clipboard
			;;
		http*)
			location="$@"
			url
			;;
		*)
			location="$@"
			file
			;;
	esac
if [ $# -gt 0 ]; then
	src="$1"
else
	desktop
fi


if [[ "$#" -eq 0 ]]; then
	desktop
	src="-;type=text/plain;filename=file.txt"
fi

upload
feedback
url=$(curl -H "Authorization: token $oauth_token" \
	-F "file=@$src" "https://legacy.sr.ht/api/upload" \
	| jq -r '.url')
echo "$url"