Parcourir la source

[enh] 1st version of filecrop engine, to discover how searx works

pw3t il y a 11 ans
Parent
révision
c2092b9fce
2 fichiers modifiés avec 77 ajouts et 0 suppressions
  1. 3
    0
      .gitignore
  2. 74
    0
      searx/engines/filecrop.py

+ 3
- 0
.gitignore Voir le fichier

@@ -1,2 +1,5 @@
1 1
 env
2 2
 engines.cfg
3
+
4
+*.pyc
5
+*/*.pyc

+ 74
- 0
searx/engines/filecrop.py Voir le fichier

@@ -0,0 +1,74 @@
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?w={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
+        print data
58
+
59
+        if 'content' in self.result:
60
+            self.result['content'] += data + ' '
61
+        else:
62
+            self.result['content'] = data + ' '
63
+        
64
+        self.data_counter += 1
65
+
66
+def request(query, params):
67
+    params['url'] = search_url.format(query=urlencode({'q': query}))
68
+    return params
69
+
70
+def response(resp):
71
+    parser = FilecropResultParser()
72
+    parser.feed(resp.text)
73
+
74
+    return parser.results