Permalink
Browse files

Get the original url and title of shortened url OK.

  • Loading branch information...
1 parent 0c29107 commit e54f48397da89c9a412e76b61ba46df24408f720 @ydoovv ydoovv committed Oct 17, 2010
Showing with 171 additions and 3 deletions.
  1. +39 −0 freejump.py
  2. +3 −1 main.py
  3. BIN static/loader.gif
  4. +69 −0 static/test-freejump.html
  5. +1 −1 templates/base.html
  6. +59 −1 templates/main.html
View
@@ -0,0 +1,39 @@
+from google.appengine.ext import webapp
+from google.appengine.api import urlfetch
+from django.utils import simplejson as json
+
+# Free jump the shorten url to the original long url
+# Many shortend url services like bit.ly, j.mp are bloody damn
+# blocked in some countries.
+
+# 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'])
+ # Not setting follow_redirects to False, we can use the final_url
+ # self.redirect(result.final_url)
+ else:
+ self.redirect(short_url)
+
+ 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 title:
+ req += "&title=1"
+ req += "&url=%s" % url
+ res = json.loads(urlfetch.fetch(req).content)
+ if nojump:
+ self.response.out.write("%s:;;:%s" % (res["long-url"],
+ "title" in res and res["title"] or ""))
+ else:
+ self.redirect(res["long-url"])
+# End.
View
@@ -13,6 +13,7 @@
from google.appengine.ext.webapp import template
from simplecookies import Cookies
+from freejump import FreejumpPage
class MainHandler(webapp.RequestHandler):
def get_(self):
@@ -42,7 +43,7 @@ def get(self):
template_values = {"tweets": [], "id": id_, "page": int(page) + 1}
for fav in favs:
if "text" not in fav:
- self.response.out.write("%s" % favs)
+ self.response.out.write("%s" % fav["error"])
return
else:
tweet = fav["text"]
@@ -62,6 +63,7 @@ def main():
application = webapp.WSGIApplication(
[
(r'/', MainHandler),
+ (r'/freejump', FreejumpPage),
],
debug=True)
run_wsgi_app(application)
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,69 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+ <title>digest-favs</title>
+ <link rel=stylesheet type=text/css href="/static/style.css">
+</head>
+<body>
+<script type="text/javascript">
+
+var xmlHttp;
+var obj;
+
+function createXMLHttpRequest()
+ {
+ try
+ {
+ // Firefox, Opera 8.0+, Safari
+ xmlHttp=new XMLHttpRequest();
+ }
+ catch (e)
+ {
+
+ // Internet Explorer
+ try
+ {
+ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
+ }
+ catch (e)
+ {
+
+ try
+ {
+ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
+ }
+ catch (e)
+ {
+ alert("您的浏览器不支持AJAX!");
+ return false;
+ }
+ }
+ }
+}
+
+function longurl_handler()
+{
+ if (xmlHttp.readyState == 4) {
+ if (xmlHttp.status == 200) {
+ alert(xmlHttp.responseText);
+ }
+ }
+ else {
+
+ }
+}
+
+function get_longurl_and_title(url)
+{
+ createXMLHttpRequest();
+ xmlHttp.onreadystatechange = longurl_handler;
+ xmlHttp.open("GET", "/freejump?nojump=1&title=1&url=" + url, true);
+ xmlHttp.send(null);
+}
+</script>
+
+<a class="bookmarklet" href="javascript:void(0);" onClick="get_longurl_and_title('http://goo.gl/7YKY');">Read Later</a>
+
+</body>
+</html>
View
@@ -15,7 +15,7 @@
</div>
{% if flashes %}
<ul class=flashes>
- <li>{{ flashes }}</li>
+ <li><span id="flashes">{{ flashes }}</span></li>
</ul>
{% endif %}
<div class=body>
View
@@ -145,6 +145,64 @@
f.style.display = 'none';
f.parentNode.removeChild(f);
}
+
+// get the origin url and title of the shortened url.
+var xmlHttp;
+var obj = document.getElementById("flashes");
+
+function createXMLHttpRequest()
+ {
+ try
+ {
+ // Firefox, Opera 8.0+, Safari
+ xmlHttp=new XMLHttpRequest();
+ }
+ catch (e)
+ {
+
+ // Internet Explorer
+ try
+ {
+ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
+ }
+ catch (e)
+ {
+
+ try
+ {
+ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
+ }
+ catch (e)
+ {
+ alert("您的浏览器不支持AJAX!");
+ return false;
+ }
+ }
+ }
+}
+
+function longurl_handler()
+{
+ if (xmlHttp.readyState == 4) {
+ if (xmlHttp.status == 200) {
+ res = xmlHttp.responseText.split(":;;:");
+ _rlipb337145(res[0], res[1]); // longurl & title
+ obj.textContent = "Saved!";
+ }
+ }
+ else {
+ obj.textContent = "Please wait...... We are getting the original url and title of the shortened url.";
+ }
+}
+
+function readlater(url)
+{
+ createXMLHttpRequest();
+ xmlHttp.onreadystatechange = longurl_handler;
+ xmlHttp.open("GET", "/freejump?nojump=1&title=1&url=" + url, true);
+ xmlHttp.send(null);
+
+}
</script>
<div class=twitbox>
@@ -162,7 +220,7 @@
<li><p>
<span>{{ tweet.content }}</span>
<!--<span class="bookmarklet" onClick="alert('{{ tweet.inlineurl }}');_rlipb337145('{{ tweet.inlineurl }}, {{ tweet.title }}');void(0)">Read Later</span>-->
- <a class="bookmarklet" href="javascript:void(0);" onClick="alert('{{ tweet.inlineurl }}');_rlipb337145('{{ tweet.inlineurl }}', '{{ tweet.title }}');void(0)">Read Later</a>
+ <a class="bookmarklet" href="javascript:void(0);" onClick="alert('{{ tweet.inlineurl }}');readlater('{{ tweet.inlineurl }}');">Read Later</a>
{% endfor %}
</ul>

0 comments on commit e54f483

Please sign in to comment.