.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searchcode_code.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Searchcode (IT)
3
4"""
5
6from json import loads
7from urllib.parse import urlencode
8
9# about
10about = {
11 "website": 'https://searchcode.com/',
12 "wikidata_id": None,
13 "official_api_documentation": 'https://searchcode.com/api/',
14 "use_official_api": True,
15 "require_api_key": False,
16 "results": 'JSON',
17}
18
19# engine dependent config
20categories = ['it']
21search_api = 'https://searchcode.com/api/codesearch_I/?'
22
23# special code-endings which are not recognised by the file ending
24code_endings = {'cs': 'c#', 'h': 'c', 'hpp': 'cpp', 'cxx': 'cpp'}
25
26# paging is broken in searchcode.com's API .. not sure it will ever been fixed
27# paging = True
28
29
30def request(query, params):
31 args = urlencode(
32 {
33 'q': query,
34 # paging is broken in searchcode.com's API
35 # 'p': params['pageno'] - 1,
36 # 'per_page': 10,
37 }
38 )
39 params['url'] = search_api + args
40 logger.debug("query_url --> %s", params['url'])
41 return params
42
43
44def response(resp):
45 results = []
46
47 search_results = loads(resp.text)
48
49 # parse results
50 for result in search_results.get('results', []):
51 href = result['url']
52 title = "" + result['name'] + " - " + result['filename']
53 repo = result['repo']
54
55 lines = {}
56 for line, code in result['lines'].items():
57 lines[int(line)] = code
58
59 code_language = code_endings.get(
60 result['filename'].split('.')[-1].lower(), result['filename'].split('.')[-1].lower()
61 )
62
63 # append result
64 results.append(
65 {
66 'url': href,
67 'title': title,
68 'content': '',
69 'repository': repo,
70 'codelines': sorted(lines.items()),
71 'code_language': code_language,
72 'template': 'code.html',
73 }
74 )
75
76 # return results
77 return results