forked from gutenbergtools/autocat3
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JSONFormatter.py
53 lines (34 loc) · 1.19 KB
/
JSONFormatter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- mode: python; indent-tabs-mode: nil; -*- coding: iso-8859-1 -*-
"""
JSONFormatter.py
Copyright 2009-2012 by Marcello Perathoner
Distributable under the GNU General Public License Version 3 or newer.
Produce a JSON response.
"""
from __future__ import unicode_literals
import json
import re
from libgutenberg.MediaTypes import mediatypes as mt
import BaseFormatter
RE_WORD = re.compile (r'\W+', re.U)
class JSONFormatter (BaseFormatter.BaseFormatter):
""" Produce JSON output. """
CONTENT_TYPE = mt.json + '; charset=UTF-8'
CONTENT_TYPE = 'application/json; charset=UTF-8'
DOCTYPE = None
def __init__ (self):
super (JSONFormatter, self).__init__ ()
def format (self, dummy_page, os):
# Specs:
# http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
sugg0 = []
sugg1 = []
sugg2 = []
for e in os.entries:
if 'navlink' not in e.class_:
sugg0.append (e.title)
sugg1.append (e.subtitle)
sugg2.append (e.url)
self.send_headers ()
return json.dumps ( [os.query, sugg0, sugg1, sugg2] ).encode ('utf-8')