Created
July 31, 2013 06:14
-
-
Save lionelyoung/6119739 to your computer and use it in GitHub Desktop.
Replace urls in file with short urls using API from https://github.com/briancray/PHP-URL-Shortener
This file contains 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
#!/usr/bin/bash | |
# Replace all long urls in MYFILE file with short urls using API from: | |
# https://github.com/briancray/PHP-URL-Shortener | |
MYFILE=~/somefile | |
DOMAIN=domain.com | |
URLSERVICE="http://$DOMAIN/shorten.php?longurl=" | |
# Get URL from todo list | |
URLLIST=$(grep "http://" $MYFILE | sed 's/.*http:\/\/\([^ ]*\).*/http:\/\/\1/') | |
for url in $URLLIST; do | |
# Skip already short | |
if [[ "$url" == http://goo.gl/* ]]; then | |
continue | |
elif [[ "$url" == http://$DOMAIN/* ]]; then | |
continue | |
fi | |
# Pull short URL | |
API=${URLSERVICE}${url} | |
SHORTURL=`curl --silent $API` | |
echo Replacing $url with $SHORTURL | |
sed -i 's,'"$url"','"$SHORTURL"',' "$MYFILE" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment