Python URL Shortner/Redirecter

Some fun little Python thingy I created with Flask and Replit DB:

from flask import Flask, request, redirect
from replit import db
import random
import uuid
import time

app = Flask(__name__)
app.secret_key = "something-idc"

@app.route("/", methods=["GET"])
def _main(): return "shortnr"

@app.route("/<uid>", methods=["GET"])
def _uid(uid):
  if uid not in db.keys():
    return "Invalid UID"
  return redirect(db[uid]["redirect_url"])

@app.route("/shorten", methods=["GET", "POST"])
def _shortn(): 
  _data = request.get_json()
  _id = uuid.uuid4()
  _id = _id.hex
  if request.method == "POST":
    _url_to_shortn = _data["url"]
    try:
      db[_id] = {"redirect_url": _url_to_shortn, "ts": time.time()}
    except Exception as e:
      return e

if __name__ == "__main__":
  app.run(
    host="0.0.0.0",
    port=80,
    debug=True
  )
1 Like

Shouldn’t is create a new id each time? Also, in some cases, it might be a url expander XD

1 Like

oops I should prolly fix that xd

2 Likes

Are you going to send a link to the Repl? lol

2 Likes