add paramter logo_format to convert inlined logos (bug 1276)
[mygpo-feedservice.git] / feedservice / main.py
blob358f710c07d0b2cbd94803e6ec91320021796c8f
1 from google.appengine.ext import webapp
2 from google.appengine.ext.webapp.util import run_wsgi_app
4 import urllib
5 import httputils
6 import feeddownloader
7 import simplejson as json
9 class MainPage(webapp.RequestHandler):
10 def get(self):
11 self.response.headers['Content-Type'] = 'text/plain'
12 self.response.out.write('Hello, webapp World!')
14 class Parse(webapp.RequestHandler):
16 def post(self):
17 return self.get()
19 def get(self):
20 urls = map(urllib.unquote, self.request.get_all('url'))
22 inline_logo = self.request.get_range('inline_logo', 0, 1, default=0)
23 scale_to = self.request.get_range('scale_logo', 0, 1, default=0)
24 logo_format = self.request.get('logo_format')
25 strip_html = self.request.get_range('strip_html', 0, 1, default=0)
26 use_cache = self.request.get_range('use_cache', 0, 1, default=1)
27 modified = self.request.headers.get('If-Modified-Since', None)
28 accept = self.request.headers.get('Accept', 'application/json')
30 if urls:
31 podcasts, last_modified = feeddownloader.parse_feeds(urls, inline_logo, scale_to, logo_format, strip_html, modified, use_cache)
32 self.send_response(podcasts, last_modified, accept)
34 else:
35 self.response.set_status(400)
36 self.response.out.write('parameter url missing')
39 def send_response(self, podcasts, last_modified, formats):
40 self.response.headers.add_header('Vary', 'Accept, User-Agent, Accept-Encoding')
42 format = httputils.select_matching_option(['text/html', 'application/json'], formats)
44 if format in (None, 'application/json'): #serve json as default
45 content_type = 'application/json'
46 content = json.dumps(podcasts, sort_keys=True, indent=None, separators=(',', ':'))
47 from email import utils
48 import time
49 self.response.headers.add_header('Last-Modified', utils.formatdate(time.mktime(last_modified.timetuple())))
52 else:
53 import cgi
54 content_type = 'text/html'
55 pretty_json = json.dumps(podcasts, sort_keys=True, indent=4)
56 pretty_json = cgi.escape(pretty_json)
57 content = """<html><head>
58 <link href="static/screen.css" type="text/css" rel="stylesheet" />
59 <link href="static/prettify.css" type="text/css" rel="stylesheet" />
60 <script type="text/javascript" src="static/prettify.js"></script>
61 </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
63 self.response.headers['Content-Type'] = content_type
64 self.response.out.write(content)
67 application = webapp.WSGIApplication([
68 ('/', MainPage),
69 ('/parse', Parse)
71 debug=True)
73 def main():
74 run_wsgi_app(application)
76 if __name__ == "__main__":
77 main()