github.py 828 B

1234567891011121314151617181920212223242526272829
  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&'
  6. def request(query, params):
  7. global search_url
  8. params['url'] = search_url + urlencode({'q': query})
  9. params['headers']['Accept'] = 'application/vnd.github.preview.text-match+json'
  10. return params
  11. def response(resp):
  12. results = []
  13. search_res = loads(resp.text)
  14. if not 'items' in search_res:
  15. return results
  16. for res in search_res['items']:
  17. title = res['name']
  18. url = res['html_url']
  19. if res['description']:
  20. content = escape(res['description'][:500])
  21. else:
  22. content = ''
  23. results.append({'url': url, 'title': title, 'content': content})
  24. return results