Created
December 8, 2024 10:24
-
-
Save keizie/51e4aa765abe05539aa20b6e134b76b6 to your computer and use it in GitHub Desktop.
Expand short URLs from linkwarden and update Link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# linkwarden > Settings > Access Token | |
TOKEN="" | |
HOST_URL="" | |
COLLECTION_ID="" | |
# collection where invalid urls go | |
INVALID_COLLECTION_ID="" | |
# some notes | |
# 1. "while read -r" is required to preserve escape from compact output from jq | |
# 2. NEW_URL can make jq breaking if NEW_URL itself have double-qoutations (which is not escaped) | |
curl "$HOST_URL/api/v1/links?collectionId=$COLLECTION_ID&searchQueryString=/bit.ly/&searchByUrl=true" -H "Authorization: Bearer $TOKEN" 2> /dev/null \ | |
| jq -c '.response.[]' \ | |
| while read -r JSON | |
do | |
#sleep 1 | |
ID=$(jq -r .id <<<"$JSON") | |
URL=$(jq -r .url <<<"$JSON") | |
##echo $JSON | |
echo -n $ID $URL" " | |
#echo; continue; | |
NEW_URL=$(curl -v $URL 2>&1 | grep location: | cut -d: -f2- 2>/dev/null | sed 's#^ ##' | sed 's#http://sidebar.io/out?url=##' | tr -d '\r' | sed 's#"#\\"#') | |
#echo $NEW_URL | |
if [ "x$NEW_URL" == "x" ]; then | |
echo INVALID URL | |
INV_JSON=$(jq ".collection.id = $INVALID_COLLECTION_ID" <<<"$JSON") | |
##echo $INV_JSON | |
curl -XPUT $HOST_URL/api/v1/links/$ID -d "$INV_JSON" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" | |
echo | |
continue | |
fi | |
#continue; | |
echo $NEW_URL | |
NEW_JSON=$(jq ".url = \"$NEW_URL\"" <<<"$JSON") | |
##echo $NEW_JSON | |
curl -XPUT $HOST_URL/api/v1/links/$ID -d "$NEW_JSON" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" | |
echo | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment