This code accompanies "A Concise URL Shortener in Python".
- Follow the instructions in the blog post to set up the database (spreadsheet).
- Acquire access to a machine with a fixed, public IP address. I use a cheap VPS from DigitalOcean (non-referral link).
- Clone this repo onto your server:
git clone https://github.com/guoguo12/gsheets-url-shortener.git. - Change
SHEET_IDinshortener.pyto your spreadsheet's ID, and also configurePORT. - Install dependencies with
pip install -r requirements.txt. (Make sure you're using the pip for your Python 3 installation.) - Run the server:
python3 shortener.py.
You may also want to register a domain name and point it at your server.
Ideas for improving the code:
- Find a way to modify the spreadsheet such that the server crashes while parsing the CSV.
Then use Python's
csvmodule to parse the database instead. - When a user requests a short URL that isn't in the database, our server responds with an ugly "Internal Server Error" page. Handle this gracefully.
- Cache database queries using
functools.lru_cache, then add a special endpoint for clearing the cache. - Keep track of how many times each short URL has been accessed. (Bonus points for writing click-count data back to the spreadsheet.)