Permalink
Browse files

added app based on twisted.web

  • Loading branch information...
1 parent 5631ff4 commit 957a181eaac4a71e821a4449f02637c7857ff9c2 @zed zed committed Nov 8, 2010
Showing with 97 additions and 0 deletions.
  1. +2 −0 README.md
  2. +54 −0 twisted/app.py
  3. +41 −0 twisted/templates/index.html
View
@@ -24,3 +24,5 @@ We have the same app in these frameworks.
* [django](http://djangoproject.com/)
* [web2py](http://web2py.com/)
* [pyramid](http://docs.pylonshq.com/faq/pyramid.html)
+
+[twisted](http://twistedmatrix.com/trac/wiki/TwistedProject)
View
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+"""http://agiliq.com/blog/2010/11/i-am-so-starving-same-web-app-in-various-python-we/
+
+The app(s) talks to Facebook, and finds out recent people who have
+posted a public status with the text "so starving".
+
+http://localhost:8880/
+"""
+from twisted.internet import reactor, defer
+from twisted.python import log
+from twisted.web import client, resource, server
+
+from jinja2 import FileSystemLoader, Environment
+
+from werkzeug.contrib.cache import SimpleCache
+cache = SimpleCache()
+
+import json as simplejson
+
+class StarvingResource(resource.Resource):
+ isLeaf = True
+
+ def render_GET(self, request):
+ d = defer.maybeDeferred(self._render_GET, request)
+ d.addCallbacks(request.write, request.processingFailed)
+ d.addBoth(self._finishRequest, request)
+ request.notifyFinish().addBoth(self._cancelRequest, request)
+ return server.NOT_DONE_YET
+
+ @defer.inlineCallbacks
+ def _render_GET(self, request):
+ fml_endpoint = 'http://graph.facebook.com/search?q=%22so%20starving%22&type=post'
+ fb_data = cache.get(key=fml_endpoint)
+ if not fb_data:
+ fb_response = yield client.getPage(fml_endpoint)
+ fb_data = simplejson.loads(fb_response)["data"]
+ cache.set(fml_endpoint, fb_data, 30 * 60)
+ env = Environment(loader=FileSystemLoader('templates'))
+ template = env.get_template("index.html").render
+ defer.returnValue(template(data=fb_data).encode('utf-8'))
+
+ def _cancelRequest(self, unused, request):
+ request.finish = lambda: None
+
+ def _finishRequest(self, unused, request):
+ request.finish()
+
+
+if __name__=="__main__":
+ import sys
+ log.startLogging(sys.stderr)
+ resource = StarvingResource()
+ reactor.listenTCP(8880, server.Site(resource))
+ reactor.run()
@@ -0,0 +1,41 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+ "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+ <title>FMl</title>
+ <style>
+ .author{
+ font-style:italic;
+ }
+ li{
+ list-style: none;
+ }
+ .picture, .message{
+ float:left;
+ }
+ </style>
+</head>
+<body>
+
+
+ <ul>
+ {% for post in data %}
+ <li>
+ <div>
+ <div class="picture">
+ <img src="https://graph.facebook.com/{{ post.from.id }}/picture" />
+ </div>
+ <div class="message">
+ {{ post.message }} : <span class="author">{{ post.from.name }}</span>
+ </div>
+
+ </div>
+
+
+ </li>
+ {% endfor %}
+ </ul>
+
+
+</body>
+</html>

0 comments on commit 957a181

Please sign in to comment.