Skip to content

Commit d79b92b

Browse files
Add files via upload
1 parent 72a5597 commit d79b92b

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

app.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from flask import Flask, render_template, request, redirect
2+
import pyshorteners
3+
4+
app = Flask(__name__)
5+
6+
url_mapping = {}
7+
8+
9+
@app.route('/')
10+
def index():
11+
return render_template('index.html')
12+
13+
14+
@app.route('/shorten', methods=['POST'])
15+
def shorten():
16+
original_url = request.form['url']
17+
shortener = pyshorteners.Shortener()
18+
short_url = shortener.tinyurl.short(original_url)
19+
url_mapping[short_url] = original_url
20+
return render_template('shorten.html', short_url=short_url)
21+
22+
23+
@app.route('/<short_url>')
24+
def redirect_to_url(short_url):
25+
if short_url in url_mapping:
26+
original_url = url_mapping[short_url]
27+
return redirect(original_url)
28+
else:
29+
return "Invalid short URL"
30+
31+
32+
if __name__ == '__main__':
33+
app.run(debug=True)

templates/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>URL Shortener</title>
5+
</head>
6+
<body>
7+
<h1>URL Shortener</h1>
8+
<form action="/shorten" method="POST">
9+
<input type="text" name="url" placeholder="Enter URL">
10+
<button type="submit">Shorten</button>
11+
</form>
12+
</body>
13+
</html>

templates/shorten.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>URL Shortener</title>
5+
</head>
6+
<body>
7+
<h1>Shortened URL:</h1>
8+
<p>{{ short_url }}</p>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)