-
Notifications
You must be signed in to change notification settings - Fork 3
/
opensearch.py
32 lines (26 loc) · 1.09 KB
/
opensearch.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
class OpenSearchDocument(object):
"""Generates OpenSearch documents."""
TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>%(name)s</ShortName>
<Description>%(description)s</Description>
<Tags>%(tags)s</Tags>
<Url type="application/atom+xml;profile=opds-catalog" template="%(url_template)s"/>
</OpenSearchDescription>"""
@classmethod
def search_info(cls, lane):
d = dict(name="Search")
tags = []
if lane is not None and lane.search_target is not None:
tags.append(lane.search_target.name.lower().replace(" ", "-").replace("&", "&"))
description = "Search %s" % lane.search_target.name.replace("&", "&")
else:
description = "Search"
d['description'] = description
d['tags'] = " ".join(tags)
return d
@classmethod
def for_lane(cls, lane, base_url):
info = cls.search_info(lane)
info['url_template'] = base_url + "?q={searchTerms}"
return cls.TEMPLATE % info