Permalink
Browse files

expose atom feed for postbin contents

  • Loading branch information...
1 parent a320fb4 commit c70f915ccd193c135eabca17ec70a17f24a89797 @qmacro qmacro committed Aug 7, 2009
Showing with 51 additions and 8 deletions.
  1. +12 −8 bin.py
  2. +39 −0 templates/bin.atom
View
20 bin.py
@@ -5,29 +5,33 @@
from google.appengine.api import urlfetch
from models import Bin, Post
import urllib
+import re
class BinHandler(webapp.RequestHandler):
def get(self):
- if self.request.path[-1] == '/':
- self.redirect(self.request.path[:-1])
- bin = self._get_bin()
+ path = self.request.path
+ if path[-1] == '/':
+ self.redirect(path[:-1])
+ path, feed = re.subn(r'^(.+)\/feed$', r'\1', path)
+ bin = self._get_bin(path)
if self.request.query_string:
self._record_post(bin, True)
self.redirect('/%s' % bin.name)
else:
posts = bin.post_set.order('-created').fetch(50)
- self.response.out.write(template.render('templates/bin.html', {'bin':bin, 'posts':posts, 'request':self.request}))
+ type = 'atom' if feed else 'html'
+ self.response.out.write(template.render('templates/bin.' + type, {'bin':bin, 'posts':posts, 'request':self.request}))
def post(self):
- bin = self._get_bin()
+ bin = self._get_bin(self.request.path)
self._record_post(bin)
if 'http://' in self.request.query_string:
urlfetch.fetch(url=self.request.query_string.replace('http://', 'http://hookah.webhooks.org/'),
payload=urllib.urlencode(self.request.POST.items()), method='POST')
self.redirect('/%s' % bin.name)
def head(self):
- bin = self._get_bin()
+ bin = self._get_bin(self.request.path)
if self.request.query_string:
self._record_post(bin, True)
else:
@@ -45,8 +49,8 @@ def _record_post(self, bin, use_get=False):
post.form_data = [[k,v] for k,v in data_source.items()]
post.put()
- def _get_bin(self):
- name = self.request.path.replace('/', '')
+ def _get_bin(self, path):
+ name = path.replace('/', '')
bin = Bin.all().filter('name =', name).get()
if bin:
return bin
View
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+
+ <title>Postbin {{ bin.name }}</title>
+ <link href="{{ request.scheme }}://{{ request.host }}/{{ bin.name }}"/>
+ <link rel="self" type="application/atom+xml" href="{{ request.url }}"/>
+ <updated>2003-12-13T18:30:02Z</updated>
+ <author>
+ <name>Postbin</name>
+ </author>
+ <id>{{ request.url }}</id>
+
+ {% for post in posts %}
+ <entry>
+ <title>
+ {{ post.id }} @ {{ post.created|time:"H:i" }} {{ post.created|date:"M d Y" }}
+ --
+ {{ post.remote_addr }}
+ </title>
+ <link href="{{ request.scheme }}://{{ request.host }}/{{ bin.name }}#{{ post.id }}"/>
+ <id>{{ request.url }}#{{ post.id}}</id>
+ <updated>{{ post.created|date:"Y-m-d" }}T{{ post.created|time:"H:i:s" }}Z</updated>
+ <content type="html" xml:base="{{ request.scheme }}://{{ request.host }}/{{ bin.name }}#{{ post.id}}">
+ <![CDATA[
+ {% for h in post.headers.items %}{{ h.0 }}: <em>{{ h.1 }}</em><br/>
+ {% endfor %}
+ <table>
+ {% for part in post %}
+ <tr>
+ <td title="{{ part.0 }}" valign="top" class="key">{{ part.0 }}</td>
+ <td>{% ifequal part.0 "body" %}<pre>{% endifequal %}{{ part.1|escape }}{% ifequal part.0 "body" %}</pre>{% endifequal %}</td></tr>
+ {% endfor %}
+ </table>
+ ]]>
+ </content>
+ </entry>
+ {% endfor %}
+
+</feed>

0 comments on commit c70f915

Please sign in to comment.