searchcode_doc.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 urllib import urlencode
  11. from json import loads
  12. # engine dependent config
  13. categories = ['it']
  14. paging = True
  15. # search-url
  16. url = 'https://searchcode.com/'
  17. search_url = url + 'api/search_IV/?{query}&p={pageno}'
  18. # do search-request
  19. def request(query, params):
  20. params['url'] = search_url.format(query=urlencode({'q': query}),
  21. pageno=params['pageno'] - 1)
  22. # Disable SSL verification
  23. # error: (60) SSL certificate problem: unable to get local issuer
  24. # certificate
  25. params['verify'] = False
  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['type'] + "] " +\
  35. result['namespace'] +\
  36. " " + result['name']
  37. content = '<span class="highlight">[' +\
  38. result['type'] + "] " +\
  39. result['name'] + " " +\
  40. result['synopsis'] +\
  41. "</span><br />" +\
  42. result['description']
  43. # append result
  44. results.append({'url': href,
  45. 'title': title,
  46. 'content': content})
  47. # return results
  48. return results