github.py 1.3 KB

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