|
@@ -0,0 +1,144 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+"""
|
|
4
|
+ Arch Linux Wiki
|
|
5
|
+
|
|
6
|
+ @website https://wiki.archlinux.org
|
|
7
|
+ @provide-api no (Mediawiki provides API, but Arch Wiki blocks access to it
|
|
8
|
+ @using-api no
|
|
9
|
+ @results HTML
|
|
10
|
+ @stable no (HTML can change)
|
|
11
|
+ @parse url, title, content
|
|
12
|
+"""
|
|
13
|
+
|
|
14
|
+from urlparse import urljoin
|
|
15
|
+from cgi import escape
|
|
16
|
+from urllib import urlencode
|
|
17
|
+from lxml import html
|
|
18
|
+from searx.engines.xpath import extract_text
|
|
19
|
+
|
|
20
|
+# engine dependent config
|
|
21
|
+categories = ['it']
|
|
22
|
+language_support = True
|
|
23
|
+paging = True
|
|
24
|
+base_url = 'https://wiki.archlinux.org'
|
|
25
|
+
|
|
26
|
+# xpath queries
|
|
27
|
+xpath_results = '//ul[@class="mw-search-results"]/li'
|
|
28
|
+xpath_link = './/div[@class="mw-search-result-heading"]/a'
|
|
29
|
+xpath_content = './/div[@class="searchresult"]'
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+# cut 'en' from 'en_US', 'de' from 'de_CH', and so on
|
|
33
|
+def locale_to_lang_code(locale):
|
|
34
|
+ if locale.find('_') >= 0:
|
|
35
|
+ locale = locale.split('_')[0]
|
|
36
|
+ return locale
|
|
37
|
+
|
|
38
|
+# wikis for some languages were moved off from the main site, we need to make
|
|
39
|
+# requests to correct URLs to be able to get results in those languages
|
|
40
|
+lang_urls = {
|
|
41
|
+ 'all': {
|
|
42
|
+ 'base': 'https://wiki.archlinux.org',
|
|
43
|
+ 'search': '/index.php?title=Special:Search&offset={offset}&{query}'
|
|
44
|
+ },
|
|
45
|
+ 'de': {
|
|
46
|
+ 'base': 'https://wiki.archlinux.de',
|
|
47
|
+ 'search': '/index.php?title=Spezial:Suche&offset={offset}&{query}'
|
|
48
|
+ },
|
|
49
|
+ 'fr': {
|
|
50
|
+ 'base': 'https://wiki.archlinux.fr',
|
|
51
|
+ 'search': '/index.php?title=Spécial:Recherche&offset={offset}&{query}'
|
|
52
|
+ },
|
|
53
|
+ 'ja': {
|
|
54
|
+ 'base': 'https://wiki.archlinuxjp.org',
|
|
55
|
+ 'search': '/index.php?title=特別:検索&offset={offset}&{query}'
|
|
56
|
+ },
|
|
57
|
+ 'ro': {
|
|
58
|
+ 'base': 'http://wiki.archlinux.ro',
|
|
59
|
+ 'search': '/index.php?title=Special:Căutare&offset={offset}&{query}'
|
|
60
|
+ },
|
|
61
|
+ 'tr': {
|
|
62
|
+ 'base': 'http://archtr.org/wiki',
|
|
63
|
+ 'search': '/index.php?title=Özel:Ara&offset={offset}&{query}'
|
|
64
|
+ }
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+# get base & search URLs for selected language
|
|
69
|
+def get_lang_urls(language):
|
|
70
|
+ if language in lang_urls:
|
|
71
|
+ return lang_urls[language]
|
|
72
|
+ return lang_urls['all']
|
|
73
|
+
|
|
74
|
+# Language names to build search requests for
|
|
75
|
+# those languages which are hosted on the main site.
|
|
76
|
+main_langs = {
|
|
77
|
+ 'ar': 'العربية',
|
|
78
|
+ 'bg': 'Български',
|
|
79
|
+ 'cs': 'Česky',
|
|
80
|
+ 'da': 'Dansk',
|
|
81
|
+ 'el': 'Ελληνικά',
|
|
82
|
+ 'es': 'Español',
|
|
83
|
+ 'he': 'עברית',
|
|
84
|
+ 'hr': 'Hrvatski',
|
|
85
|
+ 'hu': 'Magyar',
|
|
86
|
+ 'it': 'Italiano',
|
|
87
|
+ 'ko': '한국어',
|
|
88
|
+ 'lt': 'Lietuviškai',
|
|
89
|
+ 'nl': 'Nederlands',
|
|
90
|
+ 'pl': 'Polski',
|
|
91
|
+ 'pt': 'Português',
|
|
92
|
+ 'ru': 'Русский',
|
|
93
|
+ 'sl': 'Slovenský',
|
|
94
|
+ 'th': 'ไทย',
|
|
95
|
+ 'uk': 'Українська',
|
|
96
|
+ 'zh': '简体中文'
|
|
97
|
+}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+# do search-request
|
|
101
|
+def request(query, params):
|
|
102
|
+ # translate the locale (e.g. 'en_US') to language code ('en')
|
|
103
|
+ language = locale_to_lang_code(params['language'])
|
|
104
|
+
|
|
105
|
+ # if our language is hosted on the main site, we need to add its name
|
|
106
|
+ # to the query in order to narrow the results to that language
|
|
107
|
+ if language in main_langs:
|
|
108
|
+ query += '(' + main_langs[language] + ')'
|
|
109
|
+
|
|
110
|
+ # prepare the request parameters
|
|
111
|
+ query = urlencode({'search': query})
|
|
112
|
+ offset = (params['pageno'] - 1) * 20
|
|
113
|
+
|
|
114
|
+ # get request URLs for our language of choice
|
|
115
|
+ urls = get_lang_urls(language)
|
|
116
|
+ search_url = urls['base'] + urls['search']
|
|
117
|
+
|
|
118
|
+ params['url'] = search_url.format(query=query, offset=offset)
|
|
119
|
+
|
|
120
|
+ return params
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+# get response from search-request
|
|
124
|
+def response(resp):
|
|
125
|
+ # get the base URL for the language in which request was made
|
|
126
|
+ language = locale_to_lang_code(resp.search_params['language'])
|
|
127
|
+ base_url = get_lang_urls(language)['base']
|
|
128
|
+
|
|
129
|
+ results = []
|
|
130
|
+
|
|
131
|
+ dom = html.fromstring(resp.text)
|
|
132
|
+
|
|
133
|
+ # parse results
|
|
134
|
+ for result in dom.xpath(xpath_results):
|
|
135
|
+ link = result.xpath(xpath_link)[0]
|
|
136
|
+ href = urljoin(base_url, link.attrib.get('href'))
|
|
137
|
+ title = escape(extract_text(link))
|
|
138
|
+ content = escape(extract_text(result.xpath(xpath_content)))
|
|
139
|
+
|
|
140
|
+ results.append({'url': href,
|
|
141
|
+ 'title': title,
|
|
142
|
+ 'content': content})
|
|
143
|
+
|
|
144
|
+ return results
|