mention Vary header in documentation
[mygpo-feedservice.git] / feedservice / main.py
blob115d7507f2b361ac5b1b8a3ddf683e8346c5f451
1 from google.appengine.ext import webapp
2 from google.appengine.ext.webapp.util import run_wsgi_app
4 import urllib
5 import feeddownloader
6 import simplejson as json
8 class MainPage(webapp.RequestHandler):
9 def get(self):
10 self.response.headers['Content-Type'] = 'text/plain'
11 self.response.out.write('Hello, webapp World!')
13 class Parse(webapp.RequestHandler):
15 def post(self):
16 return self.get()
18 def get(self):
19 urls = self.request.get_all('url')
20 urls = map(urllib.unquote, urls)
21 inline_logo = self.request.get_range('inline_logo', 0, 1, default=0)
22 scale_to = self.request.get_range('scale_logo', 0, 1, default=0)
23 strip_html = self.request.get_range('strip_html', 0, 1, default=0)
24 modified = self.request.headers.get('If-Modified-Since', None)
25 accept = self.request.headers.get('Accept', 'application/json')
27 if urls:
28 podcasts, last_modified = feeddownloader.parse_feeds(urls, inline_logo, scale_to, strip_html, modified)
29 self.send_response(podcasts, last_modified, accept)
31 else:
32 self.response.set_status(400)
33 self.response.out.write('parameter url missing')
36 def send_response(self, podcasts, last_modified, format):
37 self.response.headers.add_header('Vary', 'Accept')
39 if 'json' in format:
40 content_type = 'application/json'
41 content = json.dumps(podcasts, sort_keys=True, indent=None, separators=(',', ':'))
42 from email import utils
43 import time
44 self.response.headers.add_header('Last-Modified', utils.formatdate(time.mktime(last_modified)))
47 else:
48 import cgi
49 content_type = 'text/html'
50 pretty_json = json.dumps(podcasts, sort_keys=True, indent=4)
51 pretty_json = cgi.escape(pretty_json)
52 content = """<html><head>
53 <link href="static/screen.css" type="text/css" rel="stylesheet" />
54 <link href="static/prettify.css" type="text/css" rel="stylesheet" />
55 <script type="text/javascript" src="static/prettify.js"></script>
56 </head><body onload="prettyPrint()"><h1>HTML Response</h1><p>This response is HTML formatted. To get just the JSON data for processing in your client, <a href="/#accept">send the HTTP Header <em>Accept: application/json</em></a>. <a href="/">Back to the Documentation</a></p><pre class="prettyprint">%s</pre></body></html>""" % pretty_json
58 self.response.headers['Content-Type'] = content_type
59 self.response.out.write(content)
62 application = webapp.WSGIApplication([
63 ('/', MainPage),
64 ('/parse', Parse)
66 debug=True)
68 def main():
69 run_wsgi_app(application)
71 if __name__ == "__main__":
72 main()