Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit for my url shortener project
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
DEPENDENCIES: | ||
httplib2 | ||
|
||
|
||
Referencies: | ||
https://developers.google.com/url-shortener/v1/getting_started |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<!DOCTYPE HTML> | ||
<html> | ||
<head> | ||
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | ||
|
||
<title>URL Shortener!</title> | ||
|
||
</head> | ||
|
||
<body> | ||
<div class="centerarea"> | ||
|
||
<form action="/" method="post" accept-charset="utf-8"> | ||
<div class="long"> | ||
<p> | ||
Choose your action: | ||
<select name="action_id" id="action_id"> | ||
<option value="shorten">shorten</option> | ||
<option value="long">long</option> | ||
</select> | ||
</p> | ||
<input type="text" name="inputurl" value=""> | ||
<input type="submit" value="Submit"> | ||
</div> | ||
</form> | ||
|
||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE HTML> | ||
<html> | ||
<head> | ||
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | ||
|
||
<title>URL Shortener Result Page</title> | ||
|
||
</head> | ||
|
||
<body> | ||
<div class="centerarea"> | ||
<p> | ||
Result is: {{result_url}} | ||
</p> | ||
|
||
<p> <a href="/">Return to Home</a> </p> | ||
</div> | ||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env python3 | ||
#-*- coding: utf-8 -*- | ||
#Author: Kevin Chen | ||
#Date: | ||
|
||
|
||
import tornado.web | ||
import tornado.ioloop | ||
|
||
import httplib2 | ||
import json | ||
|
||
h = httplib2.Http("~/.cache/httplib2/") | ||
|
||
class HomeHandler(tornado.web.RequestHandler): | ||
def get(self): | ||
self.render("home.html") | ||
|
||
def post(self): | ||
action_id = self.get_argument("action_id") | ||
inputurl = self.get_argument("inputurl") | ||
if not (inputurl.startswith("http://") or inputurl.startswith("https://")): | ||
inputurl = "http://" + inputurl | ||
if action_id == "shorten": | ||
result = get_short_url(inputurl) | ||
if result is not None: | ||
self.render("result.html", result_url = result) | ||
elif action_id == "long": | ||
result = get_long_url(inputurl) | ||
if result is not None: | ||
self.render("result.html", result_url = result) | ||
|
||
self.set_status(500) | ||
|
||
|
||
def get_long_url(shorturl): | ||
rsp, content = h.request("https://www.googleapis.com/urlshortener/v1/url?shortUrl=" + shorturl) | ||
if rsp.status == 200: | ||
j = json.loads(content.decode()) | ||
if j["status"] == "OK": | ||
return j["longUrl"] | ||
|
||
|
||
def get_short_url(longurl): | ||
rsp, content = h.request("https://www.googleapis.com/urlshortener/v1/url", "POST", | ||
json.dumps({"longUrl":longurl}), {"Content-Type": "application/json"}) | ||
if rsp.status == 200: | ||
j = json.loads(content.decode()) | ||
return j["id"] | ||
|
||
|
||
|
||
app = tornado.web.Application([ | ||
(r"/", HomeHandler) | ||
], template_path="template") | ||
|
||
|
||
if __name__ == "__main__": | ||
app.listen(8888) | ||
tornado.ioloop.IOLoop.instance().start() | ||
|
||
|
||
|
||
# vim: ai ts=8 sts=8 et sw=8 |