Url shortener

Written in flask
This commit is contained in:
bendtherules
2014-02-27 17:07:52 +05:30
parent fde2ccb814
commit 851c36b887
4 changed files with 97 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
from flask import Flask
from flask import *
from urllib2 import unquote,quote
import redis
app=Flask(__name__)
global list_shortened_links
list_shortened_links=[]
db=redis.Redis()
db.client_setname("Url_shortener")
db_list_name= "shortened_links"
@app.route("/")
def show_form():
return render_template("form.html")
@app.route("/submit")
def make_shortened_url():
dict_args=request.args.to_dict()
if dict_args.has_key("url"):
link_to_commit=unquote(dict_args["url"])
#list_shortened_links.append(link_to_commit)
db.rpush(db_list_name,link_to_commit)
#len_list=len(list_shortened_links)
len_list=db.llen(db_list_name)
return render_template("submit.html",short_code=str(len_list))
else:
return redirect(r"./")
@app.route("/view/<short_code>")
def show_shortened_url(short_code):
short_code=int(short_code)
#len_list=len(list_shortened_links)
len_list=db.llen(db_list_name)
if len_list>=short_code:
#return_url= list_shortened_links[short_code-1]
return_url=db.lindex(db_list_name,short_code-1)
return render_template("view.html",return_url=return_url)
else:
return "Invalid short url"
app.run()
+15
View File
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Submit url</title>
</head>
<body>
<form action="./submit" method="get">
Enter Url:
<input type="text" name="url" />
<br>
<input type="submit" value="submit" />
</form>
</body>
</html>
+19
View File
@@ -0,0 +1,19 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Url submitted</title>
</head>
<body>
<p>
Url submitted sucessfully.
</p>
<p>
Your shortened link is <a href="./view/{{short_code}}"> {{short_code}}</a>
</p>
<p>
Goto:
<a href="./">Url submit page</a>
</p>
</body>
</html>
+16
View File
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Original Link</title>
</head>
<body>
<p>
Original link is: {{return_url}}
</p>
<p>
Goto:
<a href="./">Url submit page</a>
</p>
</body>
</html>