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_ID
inshortener.py
to 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
csv
module 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.)