Skip to content

Instantly share code, notes, and snippets.

@meleu
Last active March 19, 2020 00:04
Show Gist options
  • Save meleu/802737c09609e6deedc4135882004e18 to your computer and use it in GitHub Desktop.
Save meleu/802737c09609e6deedc4135882004e18 to your computer and use it in GitHub Desktop.
An URL shortener using a free bit.ly account.
#!/usr/bin/env bash
# urlshort.sh
#############
#
# An URL shortener using a free bit.ly account.
#
# Before running this script, you're gonna need a bitly access token.
#
# INSTRUCTIONS:
# 1. Create an account at https://bitly.com
# 2. Once you're logged in and in your dashboard:
# - click on your account name on the top-right corner
# - Profile Settings > Generic Access Token
# - enter your password and you'll get your token
# 3. Copy your token and paste it in the BITLY_TOKEN declaration below
#
# meleu - February/2020
readonly BITLY_TOKEN=''
readonly BITLY_ENDPOINT='https://api-ssl.bitly.com/v4/shorten'
shortener() {
local long_url="$1"
while [[ -z "$long_url" ]]; do
read -r -p "Digite a url: " long_url
done
curl -s \
--header "Authorization: Bearer ${BITLY_TOKEN}" \
--header "Content-Type: application/json" \
--data "{\"long_url\":\"${long_url}\"}" \
"${BITLY_ENDPOINT}" \
| jq -r 'if .link == null then .description else .link end'
}
if [[ -z "$BITLY_TOKEN" ]]; then
echo "ERROR: API access token not found" >&2
echo "(have you filled the BITLY_TOKEN variable with your token?)" >&2
exit 1
fi
shortener "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment