Skip to content

Instantly share code, notes, and snippets.

@aijcoa
Forked from tybenz/urlshort.sh
Last active December 21, 2015 04:49
Show Gist options
  • Save aijcoa/6252546 to your computer and use it in GitHub Desktop.
Save aijcoa/6252546 to your computer and use it in GitHub Desktop.
Quick fork of tybenz' urlshort.sh to include https support.
#!/bin/bash
echo "Generating URL..."
FULL_PATH="/Users/fin/Documents/dev/projects/urlshort"
#Count is kept based on a list.txt file
#list.txt is a plain text file meant to show relationships between URLs
#Instead of a running count, we just count the lines in list.txt
INITIAL=`echo -n $(cat $FULL_PATH/list.txt | wc -l)`
NUM="$INITIAL/4"
NUM="$(($NUM*10000+10000))"
DICTIONARY=(a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9)
#Generate hash based on current URL count
if [ i = "0" ]; then
echo "${DICTIONARY[0]}"
fi
DIR_NAME=""
base=66
while [ $NUM -gt 0 ]; do
newchar="${DICTIONARY[$(($NUM%$base))]}"
DIR_NAME="$DIR_NAME$newchar"
NUM="$(($NUM/$base))"
done
#Create the directory and file corresponding to hash
#Append to list.txt for legibility and to keep track of total URL count
echo "Creating file..."
URL=$1
regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
#if echo "$URL" | grep "http://"; then
if [[ $URL =~ $regex ]]; then
URL=$1
else
URL="http://$1"
fi
cd $FULL_PATH
N="
"
CONTENTS=`cat $FULL_PATH/list.txt`
FULL_URL="https://2sup.me/$DIR_NAME"
echo "$CONTENTS$N$N$n$FULL_URL$N|$N|------>$URL$N" > $FULL_PATH/list.txt
mkdir $FULL_PATH/$DIR_NAME &> /dev/null
echo "<script type=\"text/javascript\">window.location = '$URL'</script>" > $FULL_PATH/$DIR_NAME/index.html
#Copy into GitHub repo and push to gh-pages branch
echo "Uploading Directory..."
rsync -av --exclude 'list.txt' ~/Documents/Projects/urlshort/ f7n@2sup.me:/media/sdi1/home/f7n/www/2sup.me/public_html/
#Removing local directory
rm -r $FULL_PATH/$DIR_NAME
#Paste shortened URL into clipboard
echo "Putting shortened url in clipboard..."
echo -n "https://2sup.me/$DIR_NAME" | pbcopy &> /dev/null
echo
#Show contents of list.txt to reinforce relationship between long and short URLs
#tail $FULL_PATH/list.txt
echo "Success: $FULL_URL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment