Created
February 15, 2012 02:20
-
-
Save claylevering/1832595 to your computer and use it in GitHub Desktop.
Bit.ly Alfred App Extension
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
# 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