2021-01-13 10:31:25 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2015-05-02 13:45:17 +00:00
|
|
|
"""
|
2021-01-13 10:31:25 +00:00
|
|
|
Searchcode (IT)
|
2015-05-02 13:45:17 +00:00
|
|
|
"""
|
2014-12-20 06:07:32 +00:00
|
|
|
|
|
|
|
from json import loads
|
2020-08-06 15:42:46 +00:00
|
|
|
from urllib.parse import urlencode
|
2014-12-22 15:26:45 +00:00
|
|
|
|
2021-01-13 10:31:25 +00:00
|
|
|
# about
|
|
|
|
about = {
|
|
|
|
"website": 'https://searchcode.com/',
|
|
|
|
"wikidata_id": None,
|
|
|
|
"official_api_documentation": 'https://searchcode.com/api/',
|
|
|
|
"use_official_api": True,
|
|
|
|
"require_api_key": False,
|
|
|
|
"results": 'JSON',
|
|
|
|
}
|
2014-12-20 06:07:32 +00:00
|
|
|
|
|
|
|
# engine dependent config
|
|
|
|
categories = ['it']
|
|
|
|
paging = True
|
|
|
|
|
|
|
|
# search-url
|
|
|
|
url = 'https://searchcode.com/'
|
2016-01-18 11:47:31 +00:00
|
|
|
search_url = url + 'api/codesearch_I/?{query}&p={pageno}'
|
2014-12-20 06:07:32 +00:00
|
|
|
|
2014-12-22 15:26:45 +00:00
|
|
|
# special code-endings which are not recognised by the file ending
|
2021-12-27 08:26:22 +00:00
|
|
|
code_endings = {'cs': 'c#', 'h': 'c', 'hpp': 'cpp', 'cxx': 'cpp'}
|
2014-12-20 22:33:03 +00:00
|
|
|
|
2014-12-20 06:07:32 +00:00
|
|
|
|
|
|
|
# do search-request
|
|
|
|
def request(query, params):
|
2016-11-30 17:43:03 +00:00
|
|
|
params['url'] = search_url.format(query=urlencode({'q': query}), pageno=params['pageno'] - 1)
|
2014-12-20 06:07:32 +00:00
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
# get response from search-request
|
|
|
|
def response(resp):
|
|
|
|
results = []
|
2014-12-22 15:26:45 +00:00
|
|
|
|
2014-12-20 06:07:32 +00:00
|
|
|
search_results = loads(resp.text)
|
|
|
|
|
|
|
|
# parse results
|
2015-01-27 21:39:25 +00:00
|
|
|
for result in search_results.get('results', []):
|
2014-12-20 06:07:32 +00:00
|
|
|
href = result['url']
|
|
|
|
title = "" + result['name'] + " - " + result['filename']
|
2014-12-20 22:33:03 +00:00
|
|
|
repo = result['repo']
|
2014-12-22 15:26:45 +00:00
|
|
|
|
2014-12-20 06:07:32 +00:00
|
|
|
lines = dict()
|
|
|
|
for line, code in result['lines'].items():
|
|
|
|
lines[int(line)] = code
|
|
|
|
|
2014-12-22 15:26:45 +00:00
|
|
|
code_language = code_endings.get(
|
2021-12-27 08:26:22 +00:00
|
|
|
result['filename'].split('.')[-1].lower(), result['filename'].split('.')[-1].lower()
|
|
|
|
)
|
2014-12-20 22:33:03 +00:00
|
|
|
|
2014-12-20 06:07:32 +00:00
|
|
|
# append result
|
2021-12-27 08:26:22 +00:00
|
|
|
results.append(
|
|
|
|
{
|
|
|
|
'url': href,
|
|
|
|
'title': title,
|
|
|
|
'content': '',
|
|
|
|
'repository': repo,
|
|
|
|
'codelines': sorted(lines.items()),
|
|
|
|
'code_language': code_language,
|
|
|
|
'template': 'code.html',
|
|
|
|
}
|
|
|
|
)
|
2014-12-20 06:07:32 +00:00
|
|
|
|
|
|
|
# return results
|
|
|
|
return results
|