Skip to content

Commit

Permalink
initial commit for my url shortener project
Browse files Browse the repository at this point in the history
  • Loading branch information
321cyb committed Apr 20, 2012
1 parent 7f2ec6f commit 8f36204
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
6 changes: 6 additions & 0 deletions url-shortener/README
@@ -0,0 +1,6 @@
DEPENDENCIES:
httplib2


Referencies:
https://developers.google.com/url-shortener/v1/getting_started
29 changes: 29 additions & 0 deletions url-shortener/template/home.html
@@ -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>
20 changes: 20 additions & 0 deletions url-shortener/template/result.html
@@ -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>

64 changes: 64 additions & 0 deletions url-shortener/urlshortener.py
@@ -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

0 comments on commit 8f36204

Please sign in to comment.