[feat] github: use packages template
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
		
							parent
							
								
									1d6f475fbc
								
							
						
					
					
						commit
						b683aa63fb
					
				| @ -1,10 +1,11 @@ | |||||||
| # SPDX-License-Identifier: AGPL-3.0-or-later | # SPDX-License-Identifier: AGPL-3.0-or-later | ||||||
| """ | # lint: pylint | ||||||
|  Github (IT) | 
 | ||||||
|  | """Github (IT) | ||||||
| """ | """ | ||||||
| 
 | 
 | ||||||
| from json import loads |  | ||||||
| from urllib.parse import urlencode | from urllib.parse import urlencode | ||||||
|  | from dateutil import parser | ||||||
| 
 | 
 | ||||||
| # about | # about | ||||||
| about = { | about = { | ||||||
| @ -20,42 +21,48 @@ about = { | |||||||
| categories = ['it', 'repos'] | categories = ['it', 'repos'] | ||||||
| 
 | 
 | ||||||
| # search-url | # search-url | ||||||
| search_url = 'https://api.github.com/search/repositories?sort=stars&order=desc&{query}'  # noqa | search_url = 'https://api.github.com/search/repositories?sort=stars&order=desc&{query}' | ||||||
| 
 |  | ||||||
| accept_header = 'application/vnd.github.preview.text-match+json' | accept_header = 'application/vnd.github.preview.text-match+json' | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| # do search-request |  | ||||||
| def request(query, params): | def request(query, params): | ||||||
|     params['url'] = search_url.format(query=urlencode({'q': query})) |  | ||||||
| 
 | 
 | ||||||
|  |     params['url'] = search_url.format(query=urlencode({'q': query})) | ||||||
|     params['headers']['Accept'] = accept_header |     params['headers']['Accept'] = accept_header | ||||||
| 
 | 
 | ||||||
|     return params |     return params | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| # get response from search-request |  | ||||||
| def response(resp): | def response(resp): | ||||||
|     results = [] |     results = [] | ||||||
| 
 | 
 | ||||||
|     search_res = loads(resp.text) |     for item in resp.json().get('items', []): | ||||||
|  |         content = [item.get(i) for i in ['language', 'description'] if item.get(i)] | ||||||
| 
 | 
 | ||||||
|     # check if items are received |         # license can be None | ||||||
|     if 'items' not in search_res: |         lic = item.get('license') or {} | ||||||
|         return [] |         lic_url = None | ||||||
|  |         if lic.get('spdx_id'): | ||||||
|  |             lic_url = f"https://spdx.org/licenses/{lic.get('spdx_id')}.html" | ||||||
| 
 | 
 | ||||||
|     # parse results |         results.append( | ||||||
|     for res in search_res['items']: |             { | ||||||
|         title = res['name'] |                 'template': 'packages.html', | ||||||
|         url = res['html_url'] |                 'url': item.get('html_url'), | ||||||
|  |                 'title': item.get('full_name'), | ||||||
|  |                 'content': ' / '.join(content), | ||||||
|  |                 'img_src': 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'), | ||||||
|  |             } | ||||||
|  |         ) | ||||||
| 
 | 
 | ||||||
|         if res['description']: |  | ||||||
|             content = res['description'][:500] |  | ||||||
|         else: |  | ||||||
|             content = '' |  | ||||||
| 
 |  | ||||||
|         # append result |  | ||||||
|         results.append({'url': url, 'title': title, 'content': content}) |  | ||||||
| 
 |  | ||||||
|     # return results |  | ||||||
|     return results |     return results | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user