url-shortener/urlshortener
2022-07-10 21:05:39 +02:00

104 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
#
# Von Tim Peters, https://z3h.de/url-shortener
##############################################
HOST=
API_KEY=
APP_NAME="URL Shortener"
FILE_CONFIG=~/.urlshortener
addUrl() {
local data=
local result=
local link=
local error=
[ -n "$1" ] && data="${data}\"target\": \"$1\""
[ -n "$2" ] && data="${data},\"customurl\": \"$2\""
[ -n "$3" ] && data="${data},\"reuse\": $3"
[ -n "$4" ] && data="${data},\"expire_in\": \"$4\""
[ -n "$5" ] && data="${data},\"password\": \"$5\""
[ -n "$6" ] && data="${data},\"description\": \"$6\""
result="$( \
curl \
-s -H "X-API-KEY: $API_KEY" \
-H "Content-Type: application/json" \
--data "{$data}" \
"$HOST/api/v2/links"
)"
link="$( echo "$result" | jq .link | tr -d \" )"
error="$( echo "$result" | jq .error | tr -d \" )"
if [ "$link" != "null" ]; then
echo -n "$link" | xclip -selection c
notify-send -t 5000 "$link wurde in die Zwischenablage kopiert"
elif [ "$error" != "null" ]; then
notify-send -t 10000 "$error"
fi
}
addConfig() {
yad \
--form \
--title="URL Shortener" \
--text="Ersteinrichtung" \
--field="URL" \
--field="API-KEY" > "$FILE_CONFIG" || exit
}
getConfig() {
while true; do
[ -r "$FILE_CONFIG" ] || addConfig
HOST="$( cut -d\| -f1 < "$FILE_CONFIG" )"
API_KEY="$( cut -d\| -f2 < "$FILE_CONFIG" )"
if [ -z "$HOST" ] && [ -z "$API_KEY" ]; then
addConfig
else
break
fi
done
}
getUrlFromClipboard() {
xclip -o | grep -E "^https*://"
}
getConfig
FORM="$( \
yad \
--form \
--title="$APP_NAME" \
--text="URL und Einstellmöglichkeiten" \
--field="URL" \
--field="Kurzlink" \
--field="Wiederverwendung:CB" \
--field="Verfallszeit:CBE" \
--field="Passwort:H" \
--field="Beschreibung" \
"$( getUrlFromClipboard )" \
"" \
"true\!false" \
"\!7d\!30d\!180d\!365d" \
"" \
"" \
)"
[ -n "$FORM" ] && \
addUrl \
"$( echo "$FORM" | cut -d\| -f1 )" \
"$( echo "$FORM" | cut -d\| -f2 )" \
"$( echo "$FORM" | cut -d\| -f3 )" \
"$( echo "$FORM" | cut -d\| -f4 )" \
"$( echo "$FORM" | cut -d\| -f5 )" \
"$( echo "$FORM" | cut -d\| -f6 )"