-
Notifications
You must be signed in to change notification settings - Fork 1
/
knowledgebase.py
49 lines (31 loc) · 1.23 KB
/
knowledgebase.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
"""Searches the Techsupport Knowledgebase for available articles
by rod156 based on module by Scaevolus 2009"""
import re
from util import hook, http, text
api_prefix = "http://kb.comprepair.tk/kb/api.php"
search_url = api_prefix + "?action=opensearch&format=xml"
paren_re = re.compile('\s*\(.*\)$')
@hook.command('kb')
@hook.command
def kb(inp):
"""kb <topic> -- Gets the first article available on <topic>."""
x = http.get_xml(search_url, search=inp)
ns = '{http://opensearch.org/searchsuggest2}'
items = x.findall(ns + 'Section/' + ns + 'Item')
if not items:
if x.find('error') is not None:
return 'error: %(code)s: %(info)s' % x.find('error').attrib
else:
return 'No results found.'
def extract(item):
return [item.find(ns + x).text for x in
('Text', 'Description', 'Url')]
title, desc, url = extract(items[0])
if 'may refer to' in desc:
title, desc, url = extract(items[1])
title = paren_re.sub('', title)
if title.lower() not in desc.lower():
desc = title + desc
desc = u' '.join(desc.split()) # remove excess spaces
desc = text.truncate_str(desc, 200)
return u'{} :: {}'.format(desc, http.quote(url, ':/'))