2024-03-08 20:33:36 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
get_collections() {
|
|
|
|
curl -H "Authorization: Bearer $LINKWARDEN_TOKEN" \
|
|
|
|
-H "Accept: application/json" \
|
|
|
|
"$LINKWARDEN_URL/api/v1/collections" 2>/dev/null \
|
|
|
|
| jq -r '.response | map_values(.name)[]'
|
|
|
|
}
|
|
|
|
|
2024-09-25 23:31:29 +00:00
|
|
|
get_collection_id() {
|
|
|
|
curl -H "Authorization: Bearer $LINKWARDEN_TOKEN" \
|
|
|
|
-H "Accept: application/json" \
|
|
|
|
"$LINKWARDEN_URL/api/v1/collections" 2>/dev/null \
|
|
|
|
| jq -r --arg c "$1" '.response[] | select(.name==$c).id'
|
|
|
|
}
|
|
|
|
|
2024-03-08 20:33:36 +00:00
|
|
|
collection=$(get_collections | $DMENU -p "Collection: ")
|
|
|
|
url=$QUTE_URL
|
|
|
|
name=$(echo "$QUTE_TITLE" | $DMENU -p "Title: ")
|
|
|
|
|
2024-06-13 16:29:58 +00:00
|
|
|
if [ -z "$url" ] || [ -z "$name" ] || [ -z "$collection" ]
|
|
|
|
then
|
|
|
|
echo "message-error \"Failed to save link: Missing required fields\"" >> "$QUTE_FIFO"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2024-03-08 20:33:36 +00:00
|
|
|
payload=$(jq -n \
|
|
|
|
--arg u "$url" \
|
|
|
|
--arg n "$name" \
|
2024-09-25 23:31:29 +00:00
|
|
|
--arg cn "$collection" \
|
|
|
|
--argjson ci "$(get_collection_id $collection)" \
|
2024-03-08 20:33:36 +00:00
|
|
|
'{
|
|
|
|
url: $u,
|
|
|
|
name: $n,
|
2024-09-25 23:31:29 +00:00
|
|
|
collection: {
|
|
|
|
name: $cn,
|
|
|
|
id: $ci
|
|
|
|
},
|
2024-03-08 20:33:36 +00:00
|
|
|
tags: []
|
|
|
|
}')
|
|
|
|
|
2024-09-25 23:31:29 +00:00
|
|
|
echo "$payload" > /tmp/linkwarden-payload.json
|
|
|
|
echo "$(get_collection_id "Werbung")" >> /tmp/linkwarden-payload.json
|
|
|
|
|
2024-03-08 20:33:36 +00:00
|
|
|
response=$(curl -sS --fail-with-body \
|
|
|
|
-H "Authorization: Bearer $LINKWARDEN_TOKEN" \
|
|
|
|
-H "Accept: application/json" \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
-X POST \
|
|
|
|
-d "$payload" \
|
|
|
|
"$LINKWARDEN_URL/api/v1/links" 2>&1)
|
|
|
|
|
|
|
|
code="$?"
|
|
|
|
|
|
|
|
if [ "$code" -ne 0 ]
|
|
|
|
then
|
|
|
|
response=$(echo "$response" | sed 's/"/\\"/g' | sed 's/$/ - /g' | tr -d '\n' | sed 's/ - $//g')
|
|
|
|
echo "message-error \"Failed to save link: $response\"" >> "$QUTE_FIFO"
|
|
|
|
fi
|