Skip to content

Instantly share code, notes, and snippets.

@claylevering
Created February 15, 2012 02:20
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claylevering/1832595 to your computer and use it in GitHub Desktop.
Save claylevering/1832595 to your computer and use it in GitHub Desktop.
Bit.ly Alfred App Extension
# modified API url to align with changes to bit.ly requirements - @claylevering
# find the .alfredextension here: http://cl.ly/EEUK
declare -r API='https://api-ssl.bitly.com/v3/shorten'
declare -r LOGIN='yourlogin'
declare -r API_KEY='yourapi'
# add http scheme to URL if none is provided
longURL=$(printf "{query}" | sed -E -e 's|^.|http://&|' -e 's|^http://([a-zA-Z\-\.\+]+://)|\1|')
# shorten URL
shortURL=$(curl -sf -d "login=$LOGIN&apiKey=$API_KEY" --data-urlencode "longURL=$longURL" -d "format=txt" $API)
declare -i curlExit=$?
if [ -n "$shortURL" ]; then
printf $shortURL | pbcopy
echo "Bit.ly Generated!"
echo $shortURL
exit 0
elif [ $curlExit -eq 22 ]; then
errMsg="URL '$shortURL' could not be shortened because of a bit.ly server error. Your URL, login or API key may be invalid, or bit.ly may be experiencing technical difficulties."
else
errMsg="URL '$longURL' could not be shortened because of a technical error. The 'curl' command returned with exit code $curlExit. Check the man page for curl for details."
fi # [ -n "$shortURL" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment