Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add url_shortener.py #500

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions url_shortener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import hashlib
import json
from flask import Flask, request, redirect, jsonify

app = Flask(__name__)

# In-memory URL mapping (for demonstration purposes)
url_mapping = {}

def shorten_url(long_url):
# Generate a unique short URL using hash
hash_object = hashlib.md5(long_url.encode())
short_url = hash_object.hexdigest()[:6] # Use first 6 characters
return short_url

@app.route('/shorten', methods=['POST'])
def shorten():
long_url = request.json.get('long_url')
if not long_url:
return jsonify({"error": "No URL provided"}), 400
short_url = shorten_url(long_url)
url_mapping[short_url] = long_url
return jsonify({"short_url": short_url}), 200

@app.route('/<short_url>', methods=['GET'])
def redirect_to_long_url(short_url):
long_url = url_mapping.get(short_url)
if long_url:
return redirect(long_url)
return jsonify({"error": "URL not found"}), 404

if __name__ == '__main__':
app.run(debug=True)