Ver código fonte

update github engine and add comments

Thomas Pointhuber 11 anos atrás
pai
commit
334a286c18
2 arquivos alterados com 30 adições e 3 exclusões
  1. 30
    2
      searx/engines/github.py
  2. 0
    1
      searx/settings.yml

+ 30
- 2
searx/engines/github.py Ver arquivo

1
+## Github (It)
2
+# 
3
+# @website     https://github.com/
4
+# @provide-api yes (https://developer.github.com/v3/)
5
+# 
6
+# @using-api   yes
7
+# @results     JSON
8
+# @stable      yes (using api)
9
+# @parse       url, title, content
10
+
1
 from urllib import urlencode
11
 from urllib import urlencode
2
 from json import loads
12
 from json import loads
3
 from cgi import escape
13
 from cgi import escape
4
 
14
 
15
+# engine dependent config
5
 categories = ['it']
16
 categories = ['it']
6
 
17
 
18
+# search-url
7
 search_url = 'https://api.github.com/search/repositories?sort=stars&order=desc&{query}'  # noqa
19
 search_url = 'https://api.github.com/search/repositories?sort=stars&order=desc&{query}'  # noqa
8
 
20
 
9
 accept_header = 'application/vnd.github.preview.text-match+json'
21
 accept_header = 'application/vnd.github.preview.text-match+json'
10
 
22
 
11
 
23
 
24
+# do search-request
12
 def request(query, params):
25
 def request(query, params):
13
     params['url'] = search_url.format(query=urlencode({'q': query}))
26
     params['url'] = search_url.format(query=urlencode({'q': query}))
27
+
14
     params['headers']['Accept'] = accept_header
28
     params['headers']['Accept'] = accept_header
29
+
15
     return params
30
     return params
16
 
31
 
17
 
32
 
33
+# get response from search-request
18
 def response(resp):
34
 def response(resp):
19
     results = []
35
     results = []
36
+
20
     search_res = loads(resp.text)
37
     search_res = loads(resp.text)
38
+
39
+    # check if items are recieved
21
     if not 'items' in search_res:
40
     if not 'items' in search_res:
22
-        return results
41
+        return []
42
+
43
+    # parse results
23
     for res in search_res['items']:
44
     for res in search_res['items']:
24
         title = res['name']
45
         title = res['name']
25
         url = res['html_url']
46
         url = res['html_url']
47
+
26
         if res['description']:
48
         if res['description']:
27
             content = escape(res['description'][:500])
49
             content = escape(res['description'][:500])
28
         else:
50
         else:
29
             content = ''
51
             content = ''
30
-        results.append({'url': url, 'title': title, 'content': content})
52
+
53
+        # append result
54
+        results.append({'url': url,
55
+                        'title': title,
56
+                        'content': content})
57
+
58
+    # return results
31
     return results
59
     return results

+ 0
- 1
searx/settings.yml Ver arquivo

66
 
66
 
67
   - name : github
67
   - name : github
68
     engine : github
68
     engine : github
69
-    categories : it
70
     shortcut : gh
69
     shortcut : gh
71
 
70
 
72
   - name : google
71
   - name : google