github.py 887 B

1234567891011121314151617181920212223242526272829303132
  1. from urllib import urlencode
  2. from json import loads
  3. from cgi import escape
  4. categories = ['it']
  5. search_url = 'https://api.github.com/search/repositories?sort=stars&order=desc&{query}' # noqa
  6. accept_header = 'application/vnd.github.preview.text-match+json'
  7. def request(query, params):
  8. global search_url
  9. params['url'] = search_url.format(query=urlencode({'q': query}))
  10. params['headers']['Accept'] = accept_header
  11. return params
  12. def response(resp):
  13. results = []
  14. search_res = loads(resp.text)
  15. if not 'items' in search_res:
  16. return results
  17. for res in search_res['items']:
  18. title = res['name']
  19. url = res['html_url']
  20. if res['description']:
  21. content = escape(res['description'][:500])
  22. else:
  23. content = ''
  24. results.append({'url': url, 'title': title, 'content': content})
  25. return results