|
@@ -0,0 +1,48 @@
|
|
1
|
+from lxml import etree
|
|
2
|
+from requests import get
|
|
3
|
+from json import loads
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+def dbpedia(query):
|
|
7
|
+ # dbpedia autocompleter
|
|
8
|
+ autocomplete_url = 'http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString={q}' # noqa
|
|
9
|
+
|
|
10
|
+ response = get(autocomplete_url.format(q=query))
|
|
11
|
+
|
|
12
|
+ results = []
|
|
13
|
+
|
|
14
|
+ if response.ok:
|
|
15
|
+ dom = etree.fromstring(response.content)
|
|
16
|
+ results = dom.xpath('//a:Result/a:Label//text()',
|
|
17
|
+ namespaces={'a': 'http://lookup.dbpedia.org/'})
|
|
18
|
+
|
|
19
|
+ return results
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+def google(query):
|
|
23
|
+ # google autocompleter
|
|
24
|
+ autocomplete_url = 'http://suggestqueries.google.com/complete/search?client=toolbar&q={q}' # noqa
|
|
25
|
+
|
|
26
|
+ response = get(autocomplete_url.format(q=query))
|
|
27
|
+
|
|
28
|
+ results = []
|
|
29
|
+
|
|
30
|
+ if response.ok:
|
|
31
|
+ dom = etree.fromstring(response.content)
|
|
32
|
+ results = dom.xpath('//suggestion/@data')
|
|
33
|
+
|
|
34
|
+ return results
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+def wikipedia(query):
|
|
38
|
+ # wikipedia autocompleter
|
|
39
|
+ url = 'https://en.wikipedia.org/w/api.php?action=opensearch&search={q}&limit=10&namespace=0&format=json' # noqa
|
|
40
|
+
|
|
41
|
+ resp = loads(get(url.format(q=query)).text)
|
|
42
|
+ return resp[1]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+backends = {'dbpedia': dbpedia,
|
|
46
|
+ 'google': google,
|
|
47
|
+ 'wikipedia': wikipedia
|
|
48
|
+ }
|