on master help sign in
Merge pull request #13 from pw3t/master

Filecrop & Yacy engine

commit27cae541972cabd460142aa3d009ea00c37c29d5 authorAdam Tauber <asciimoo@gmail.com> dateSat 4 Jan 2014, 14:55 (13 years ago) parent42c85a0efb6eca
3 files changed+114
.gitignore+3 searx/engines/filecrop.py+73 searx/engines/yacy.py+38
.gitignore+3
@@ -1,2 +1,5 @@
11env
22engines.cfg
3+
4+*.pyc
5+*/*.pyc
searx/engines/filecrop.pynew file+73
@@ -0,0 +1,73 @@
1+from json import loads
2+from urllib import urlencode
3+from searx.utils import html_to_text
4+from HTMLParser import HTMLParser
5+
6+url = 'http://www.filecrop.com/'
7+search_url = url + '/search.php?{query}&size_i=0&size_f=100000000&engine_r=1&engine_d=1&engine_e=1&engine_4=1&engine_m=1'
8+
9+class FilecropResultParser(HTMLParser):
10+ def __init__(self):
11+ HTMLParser.__init__(self)
12+ self.__start_processing = False
13+
14+ self.results = []
15+ self.result = {}
16+
17+ self.tr_counter = 0
18+ self.data_counter = 0
19+
20+ def handle_starttag(self, tag, attrs):
21+
22+ if tag == 'tr':
23+ if ('bgcolor', '#edeff5') in attrs or ('bgcolor', '#ffffff') in attrs:
24+ self.__start_processing = True
25+
26+ if not self.__start_processing:
27+ return
28+
29+ if tag == 'label':
30+ self.result['title'] = [attr[1] for attr in attrs if attr[0] == 'title'][0]
31+ elif tag == 'a' and ('rel', 'nofollow') in attrs and ('class', 'sourcelink') in attrs:
32+ if 'content' in self.result:
33+ self.result['content'] += [attr[1] for attr in attrs if attr[0] == 'title'][0]
34+ else:
35+ self.result['content'] = [attr[1] for attr in attrs if attr[0] == 'title'][0]
36+ self.result['content'] += ' '
37+ elif tag == 'a':
38+ self.result['url'] = url + [attr[1] for attr in attrs if attr[0] == 'href'][0]
39+
40+ def handle_endtag(self, tag):
41+ if self.__start_processing is False:
42+ return
43+
44+ if tag == 'tr':
45+ self.tr_counter += 1
46+
47+ if self.tr_counter == 2:
48+ self.__start_processing = False
49+ self.tr_counter = 0
50+ self.data_counter = 0
51+ self.results.append(self.result)
52+ self.result = {}
53+
54+ def handle_data(self, data):
55+ if not self.__start_processing:
56+ return
57+
58+ if 'content' in self.result:
59+ self.result['content'] += data + ' '
60+ else:
61+ self.result['content'] = data + ' '
62+
63+ self.data_counter += 1
64+
65+def request(query, params):
66+ params['url'] = search_url.format(query=urlencode({'w' :query}))
67+ return params
68+
69+def response(resp):
70+ parser = FilecropResultParser()
71+ parser.feed(resp.text)
72+
73+ return parser.results
searx/engines/yacy.pynew file+38
@@ -0,0 +1,38 @@
1+from json import loads
2+from urllib import urlencode, quote
3+
4+url = 'http://localhost:8090'
5+search_url = '/yacysearch.json?{query}&maximumRecords=10'
6+
7+def request(query, params):
8+ params['url'] = url + search_url.format(query=urlencode({'query':query}))
9+ return params
10+
11+def response(resp):
12+ raw_search_results = loads(resp.text)
13+
14+ if not len(raw_search_results):
15+ return []
16+
17+ search_results = raw_search_results.get('channels', {})[0].get('items', [])
18+
19+ results = []
20+
21+ for result in search_results:
22+ tmp_result = {}
23+ tmp_result['title'] = result['title']
24+ tmp_result['url'] = result['link']
25+ tmp_result['content'] = ''
26+
27+ if len(result['description']):
28+ tmp_result['content'] += result['description'] +"<br/>"
29+
30+ if len(result['pubDate']):
31+ tmp_result['content'] += result['pubDate'] + "<br/>"
32+
33+ if result['size'] != '-1':
34+ tmp_result['content'] += result['sizename']
35+
36+ results.append(tmp_result)
37+
38+ return results