~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to doc/web/examples/lj.rpy.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-01-16 19:56:10 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060116195610-ykmxbia4mnnod9o2
Tags: 2.1.0-0ubuntu2
debian/copyright: Include copyright for python 2.3; some 2.3 files
are included in the upstream tarball, but not in the binary packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
 
# See LICENSE for details.
3
 
 
4
 
#
5
 
# Syndicate LiveJournal users
6
 
# Demonstrates how to use chained callbacks
7
 
from __future__ import nested_scopes
8
 
 
9
 
from twisted.web import resource as resourcelib
10
 
from twisted.web import client, microdom, domhelpers, server
11
 
 
12
 
urlTemplate = 'http://www.livejournal.com/users/%s/rss'
13
 
 
14
 
class LJSyndicatingResource(resourcelib.Resource):
15
 
 
16
 
    def render_GET(self, request):
17
 
        url = urlTemplate % request.args['user'][0]
18
 
        client.getPage(url).addCallback(
19
 
        microdom.parseString).addCallback(
20
 
        lambda t: domhelpers.findNodesNamed(t, 'item')).addCallback(
21
 
        lambda itms: zip([domhelpers.findNodesNamed(x, 'title')[0]
22
 
                                                               for x in itms],
23
 
                         [domhelpers.findNodesNamed(x, 'link')[0]
24
 
                                                               for x in itms]
25
 
                        )).addCallback(
26
 
        lambda itms: '<html><head></head><body><ul>%s</ul></body></html>' %
27
 
                          '\n'.join(
28
 
               ['<li><a href="%s">%s</a></li>' % (
29
 
                  domhelpers.getNodeText(link), domhelpers.getNodeText(title))
30
 
                       for (title, link) in itms])
31
 
        ).addCallback(lambda s: (request.write(s),request.finish())).addErrback(
32
 
        lambda e: (request.write('Error: %s' % e),request.finish()))
33
 
        return server.NOT_DONE_YET
34
 
 
35
 
resource = LJSyndicatingResource()