-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
39 lines (27 loc) · 884 Bytes
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from flask import Flask, render_template, request, redirect
import pyshorteners
import os
app = Flask(__name__)
url_mapping = {}
@app.route('/')
def index():
return render_template('index.html')
@app.route('/index')
def indexA():
return render_template('index.html')
@app.route('/shorten', methods=['POST'])
def shorten():
original_url = request.form['url']
shortener = pyshorteners.Shortener()
short_url = shortener.tinyurl.short(original_url)
url_mapping[short_url] = original_url
return render_template('shorten.html', short_url=short_url)
@app.route('/<short_url>')
def redirect_to_url(short_url):
if short_url in url_mapping:
original_url = url_mapping[short_url]
return redirect(original_url)
else:
return "Invalid short URL"
if __name__ == '__main__':
app.run()