diff --git a/flask_url_shortener/main.py b/flask_url_shortener/main.py new file mode 100644 index 0000000..a9f02d1 --- /dev/null +++ b/flask_url_shortener/main.py @@ -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/") +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() diff --git a/flask_url_shortener/templates/form.html b/flask_url_shortener/templates/form.html new file mode 100644 index 0000000..08ca65b --- /dev/null +++ b/flask_url_shortener/templates/form.html @@ -0,0 +1,15 @@ + + + + + Submit url + + +
+ Enter Url: + +
+ +
+ + diff --git a/flask_url_shortener/templates/submit.html b/flask_url_shortener/templates/submit.html new file mode 100644 index 0000000..06c822a --- /dev/null +++ b/flask_url_shortener/templates/submit.html @@ -0,0 +1,19 @@ + + + + + Url submitted + + +

+ Url submitted sucessfully. +

+

+ Your shortened link is {{short_code}} +

+

+ Goto: + Url submit page +

+ + diff --git a/flask_url_shortener/templates/view.html b/flask_url_shortener/templates/view.html new file mode 100644 index 0000000..0062326 --- /dev/null +++ b/flask_url_shortener/templates/view.html @@ -0,0 +1,16 @@ + + + + + Original Link + + +

+ Original link is: {{return_url}} +

+

+ Goto: + Url submit page +

+ +