Last active
September 9, 2019 04:37
-
-
Save mkolod/3c1b980effb7cb9915c5b56fd885e406 to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import hashlib, math, struct, sys" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def base62(num, domain=\"byte.ly\"):\n", | |
" lookup = \"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n", | |
" s = \"\"\n", | |
" while num > 0:\n", | |
" s = str(lookup[num % 62]) + s\n", | |
" num //= 62\n", | |
" return \"{}{}/{}\".format(\"https://\", domain, s)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# This simple hash is only here to show that we can cook up something quickly.\n", | |
"# The MD5 hash below is way better, and ideally we would use say SHA-256.\n", | |
"def simple_hash(string):\n", | |
" num = 0\n", | |
" for i, c in enumerate(string):\n", | |
" num = (num*10 + ord(c)) % sys.maxsize\n", | |
" return num" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def md5_hash(string, encoded_len=7):\n", | |
" m = hashlib.md5()\n", | |
" m.update(string.encode('utf-8'))\n", | |
" return struct.unpack('>QQ', m.digest())[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def url_shortener_base62(string, base62_len=7, hash_fun=md5_hash):\n", | |
" num = hash_fun(string)\n", | |
" num %= int(math.pow(62, base62_len)-1)\n", | |
" return base62(num) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"Short length=5\n", | |
"https://facebook.com -> https://byte.ly/lIB9Z\n", | |
"https://google.com -> https://byte.ly/ozP5H\n", | |
"https://amazon.com -> https://byte.ly/uxot5\n", | |
"\n", | |
"Short length=7\n", | |
"https://facebook.com -> https://byte.ly/P6BwfKK\n", | |
"https://google.com -> https://byte.ly/lSdbdlp\n", | |
"https://amazon.com -> https://byte.ly/qN3tn6K\n", | |
"\n", | |
"Short length=9\n", | |
"https://facebook.com -> https://byte.ly/q5P6BivkF\n", | |
"https://google.com -> https://byte.ly/BNlScY1JC\n", | |
"https://amazon.com -> https://byte.ly/4sqN3sW2i\n" | |
] | |
} | |
], | |
"source": [ | |
"for short_len in [5, 7, 9]:\n", | |
" print(\"\\nShort length={}\".format(short_len))\n", | |
" for url in [\"https://facebook.com\", \"https://google.com\", \"https://amazon.com\"]:\n", | |
" print(\"{} -> {}\".format(url, url_shortener_base62(url, base62_len=short_len)))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment