github.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Github (IT)
  4. """
  5. from json import loads
  6. from urllib.parse import urlencode
  7. # about
  8. about = {
  9. "website": 'https://github.com/',
  10. "wikidata_id": 'Q364',
  11. "official_api_documentation": 'https://developer.github.com/v3/',
  12. "use_official_api": True,
  13. "require_api_key": False,
  14. "results": 'JSON',
  15. }
  16. # engine dependent config
  17. categories = ['it']
  18. # search-url
  19. search_url = 'https://api.github.com/search/repositories?sort=stars&order=desc&{query}' # noqa
  20. accept_header = 'application/vnd.github.preview.text-match+json'
  21. # do search-request
  22. def request(query, params):
  23. params['url'] = search_url.format(query=urlencode({'q': query}))
  24. params['headers']['Accept'] = accept_header
  25. return params
  26. # get response from search-request
  27. def response(resp):
  28. results = []
  29. search_res = loads(resp.text)
  30. # check if items are recieved
  31. if 'items' not in search_res:
  32. return []
  33. # parse results
  34. for res in search_res['items']:
  35. title = res['name']
  36. url = res['html_url']
  37. if res['description']:
  38. content = res['description'][:500]
  39. else:
  40. content = ''
  41. # append result
  42. results.append({'url': url,
  43. 'title': title,
  44. 'content': content})
  45. # return results
  46. return results