searxng/searx/engines/github.py

68 lines
2 KiB
Python
Raw Permalink Normal View History

# SPDX-License-Identifier: AGPL-3.0-or-later
"""Github (IT)
"""
2014-09-02 15:37:47 +00:00
from urllib.parse import urlencode
from dateutil import parser
2013-10-20 19:53:49 +00:00
# about
about = {
"website": 'https://github.com/',
"wikidata_id": 'Q364',
"official_api_documentation": 'https://developer.github.com/v3/',
"use_official_api": True,
"require_api_key": False,
"results": 'JSON',
}
2014-09-02 15:37:47 +00:00
# engine dependent config
2021-12-22 15:58:52 +00:00
categories = ['it', 'repos']
2013-10-20 19:53:49 +00:00
2014-09-02 15:37:47 +00:00
# search-url
search_url = 'https://api.github.com/search/repositories?sort=stars&order=desc&{query}'
2014-01-20 01:31:20 +00:00
accept_header = 'application/vnd.github.preview.text-match+json'
2013-10-20 19:53:49 +00:00
def request(query, params):
2014-09-02 15:37:47 +00:00
params['url'] = search_url.format(query=urlencode({'q': query}))
2014-01-20 01:31:20 +00:00
params['headers']['Accept'] = accept_header
2014-09-02 15:37:47 +00:00
2013-10-20 19:53:49 +00:00
return params
def response(resp):
results = []
2014-09-02 15:37:47 +00:00
for item in resp.json().get('items', []):
content = [item.get(i) for i in ['language', 'description'] if item.get(i)]
# license can be None
lic = item.get('license') or {}
lic_url = None
if lic.get('spdx_id'):
lic_url = f"https://spdx.org/licenses/{lic.get('spdx_id')}.html"
results.append(
{
'template': 'packages.html',
'url': item.get('html_url'),
'title': item.get('full_name'),
'content': ' / '.join(content),
'thumbnail': item.get('owner', {}).get('avatar_url'),
'package_name': item.get('name'),
# 'version': item.get('updated_at'),
'maintainer': item.get('owner', {}).get('login'),
'publishedDate': parser.parse(item.get("updated_at") or item.get("created_at")),
'tags': item.get('topics', []),
'popularity': item.get('stargazers_count'),
'license_name': lic.get('name'),
'license_url': lic_url,
'homepage': item.get('homepage'),
'source_code_url': item.get('clone_url'),
}
)
2014-09-02 15:37:47 +00:00
2013-10-20 19:53:49 +00:00
return results