1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/searx_ynh.git synced 2024-09-03 20:16:30 +02:00
searx_ynh/sources/searx/engines/github.py

62 lines
1.3 KiB
Python
Raw Normal View History

2015-09-08 23:05:37 +02:00
"""
Github (It)
@website https://github.com/
@provide-api yes (https://developer.github.com/v3/)
@using-api yes
@results JSON
@stable yes (using api)
@parse url, title, content
"""
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
from urllib import urlencode
from json import loads
from cgi import escape
2014-12-01 12:26:38 +01:00
# engine dependent config
2014-05-10 11:56:19 +02:00
categories = ['it']
2014-12-01 12:26:38 +01:00
# search-url
2014-05-10 11:56:19 +02:00
search_url = 'https://api.github.com/search/repositories?sort=stars&order=desc&{query}' # noqa
accept_header = 'application/vnd.github.preview.text-match+json'
2014-12-01 12:26:38 +01:00
# do search-request
2014-05-10 11:56:19 +02:00
def request(query, params):
params['url'] = search_url.format(query=urlencode({'q': query}))
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
params['headers']['Accept'] = accept_header
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
return params
2014-12-01 12:26:38 +01:00
# get response from search-request
2014-05-10 11:56:19 +02:00
def response(resp):
results = []
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
search_res = loads(resp.text)
2014-12-01 12:26:38 +01:00
# check if items are recieved
2015-09-08 23:05:37 +02:00
if 'items' not in search_res:
2014-12-01 12:26:38 +01:00
return []
# parse results
2014-05-10 11:56:19 +02:00
for res in search_res['items']:
title = res['name']
url = res['html_url']
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
if res['description']:
content = escape(res['description'][:500])
else:
content = ''
2014-12-01 12:26:38 +01:00
# append result
results.append({'url': url,
'title': title,
'content': content})
# return results
2014-05-10 11:56:19 +02:00
return results