python-dotenv
for environment variables:
UPSTASH_REDIS_REST_URL
and UPSTASH_REST_TOKEN
to your environment:
python-dotenv
to load environment variables from a .env
file:
url_shortener.py
:
0lSLFI
).
generate_short_code
function creates a random string of characters (letters and digits) that will serve as the short code for the URL.
shorten_url
function takes the original URL and stores it in Redis using the randomly generated short code as the key. The ex
parameter sets an expiration time (in seconds) for how long the shortened URL will be valid.
get_original_url
function takes the short code and looks it up in Redis to retrieve the original URL. If the short code doesn’t exist (due to expiration or other reasons), it returns None
.