searchcode_code.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """
  2. Searchcode (It)
  3. @website https://searchcode.com/
  4. @provide-api yes (https://searchcode.com/api/)
  5. @using-api yes
  6. @results JSON
  7. @stable yes
  8. @parse url, title, content
  9. """
  10. from json import loads
  11. from searx.url_utils import urlencode
  12. # engine dependent config
  13. categories = ['it']
  14. paging = True
  15. # search-url
  16. url = 'https://searchcode.com/'
  17. search_url = url + 'api/codesearch_I/?{query}&p={pageno}'
  18. # special code-endings which are not recognised by the file ending
  19. code_endings = {'cs': 'c#',
  20. 'h': 'c',
  21. 'hpp': 'cpp',
  22. 'cxx': 'cpp'}
  23. # do search-request
  24. def request(query, params):
  25. params['url'] = search_url.format(query=urlencode({'q': query}), pageno=params['pageno'] - 1)
  26. return params
  27. # get response from search-request
  28. def response(resp):
  29. results = []
  30. search_results = loads(resp.text)
  31. # parse results
  32. for result in search_results.get('results', []):
  33. href = result['url']
  34. title = "" + result['name'] + " - " + result['filename']
  35. repo = result['repo']
  36. lines = dict()
  37. for line, code in result['lines'].items():
  38. lines[int(line)] = code
  39. code_language = code_endings.get(
  40. result['filename'].split('.')[-1].lower(),
  41. result['filename'].split('.')[-1].lower())
  42. # append result
  43. results.append({'url': href,
  44. 'title': title,
  45. 'content': '',
  46. 'repository': repo,
  47. 'codelines': sorted(lines.items()),
  48. 'code_language': code_language,
  49. 'template': 'code.html'})
  50. # return results
  51. return results