Skip to content
Browse files

freejump.py rewrited.

  • Loading branch information...
1 parent e54f483 commit 41d4f5519c9d4596777764a9a0d9910966a2ca2c @ydoovv ydoovv committed Oct 17, 2010
Showing with 34 additions and 16 deletions.
  1. +34 −16 freejump.py
View
50 freejump.py
@@ -1,6 +1,8 @@
from google.appengine.ext import webapp
from google.appengine.api import urlfetch
from django.utils import simplejson as json
+from urllib2 import urlopen
+import re
# Free jump the shorten url to the original long url
# Many shortend url services like bit.ly, j.mp are bloody damn
@@ -9,31 +11,47 @@
# Use like: http://yourapp.appspot.com/freejump?url=http://biy.ly/fuckGFW
class FreejumpPage(webapp.RequestHandler):
- def get_(self):
- short_url = self.request.get('url')
- result = urlfetch.fetch(url=short_url,
- method='HEAD',
- follow_redirects=False)
- if result.status_code in (301,302):
- self.redirect(result.headers['Location'])
+ def get_actual_url(self, short_url):
+ """Get the Location HTTP header of the shortened url."""
+
+ res = urlfetch.fetch(url=short_url, method='HEAD', follow_redirects=False)
+ if res.status_code in (301,302):
+ return res.headers['Location']
# Not setting follow_redirects to False, we can use the final_url
- # self.redirect(result.final_url)
+ # self.redirect(res.final_url)
else:
- self.redirect(short_url)
+ return short_url # actuall not shortened, not fool me...
+
+ def get_title(self, url):
+ """Get the title of the url."""
+
+ f = urlopen(url)
+ res = f.read(500)
+ title_raw = re.findall('<title>.*</title>', res)
+ if not title_raw: return "No title found of the page..."
+ title = title_raw[0][7:-8] # strip 'title' tag
+ for enc in ("utf-8", "gbk", "big5"):
+ try:
+ t = title.decode(enc)
+ except:
+ continue
+ break
+ return t
def get(self):
- req = "http://api.longurl.org/v2/expand?format=json"
url = self.request.get('url')
title = self.request.get('title')
nojump = self.request.get('nojump') # redirect to the long url by default
- if not url: return
+ if not url:
+ self.response.out.write("Please give us the url...")
+ return
+ actual_url = self.get_actual_url(url)
+ actual_title = ""
if title:
- req += "&title=1"
- req += "&url=%s" % url
- res = json.loads(urlfetch.fetch(req).content)
+ actual_title = self.get_title(actual_url)
if nojump:
- self.response.out.write("%s:;;:%s" % (res["long-url"],
- "title" in res and res["title"] or ""))
+ self.response.out.write("%s:;;:%s" % (actual_url,
+ actual_title and actual_title or ""))
else:
self.redirect(res["long-url"])
# End.

0 comments on commit 41d4f55

Please sign in to comment.
Something went wrong with that request. Please try again.