voidlinux.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """SearXNG engine for `Void Linux binary packages`_. Void is a general purpose
  3. operating system, based on the monolithic Linux kernel. Its package system
  4. allows you to quickly install, update and remove software; software is provided
  5. in binary packages or can be built directly from sources with the help of the
  6. XBPS source packages collection.
  7. .. _Void Linux binary packages: https://voidlinux.org/packages/
  8. """
  9. import re
  10. from urllib.parse import quote_plus
  11. from searx.utils import humanize_bytes
  12. about = {
  13. 'website': 'https://voidlinux.org/packages/',
  14. 'wikidata_id': 'Q19310966',
  15. 'use_official_api': True,
  16. 'official_api_documentation': None,
  17. 'require_api_key': False,
  18. 'results': 'JSON',
  19. }
  20. categories = ['packages', 'it']
  21. base_url = "https://xq-api.voidlinux.org"
  22. pkg_repo_url = "https://github.com/void-linux/void-packages"
  23. void_arch = 'x86_64'
  24. """Default architecture to search for. For valid values see :py:obj:`ARCH_RE`"""
  25. ARCH_RE = re.compile('aarch64-musl|armv6l-musl|armv7l-musl|x86_64-musl|aarch64|armv6l|armv7l|i686|x86_64')
  26. """Regular expression that match a architecture in the query string."""
  27. def request(query, params):
  28. arch_path = ARCH_RE.search(query)
  29. if arch_path:
  30. arch_path = arch_path.group(0)
  31. query = query.replace(arch_path, '').strip()
  32. else:
  33. arch_path = void_arch
  34. params['url'] = f"{base_url}/v1/query/{arch_path}?q={quote_plus(query)}"
  35. return params
  36. def response(resp):
  37. """
  38. At Void Linux, several packages sometimes share the same source code
  39. (template) and therefore also have the same URL. Results with identical
  40. URLs are merged as one result for SearXNG.
  41. """
  42. packages = {}
  43. for result in resp.json()['data']:
  44. # 32bit and dbg packages don't have their own package templates
  45. github_slug = re.sub(r"-(32bit|dbg)$", "", result['name'])
  46. pkg_url = f"{pkg_repo_url}/tree/master/srcpkgs/{github_slug}"
  47. pkg_list = packages.get(pkg_url, [])
  48. pkg_list.append(
  49. {
  50. 'title': result['name'],
  51. 'content': f"{result['short_desc']} - {humanize_bytes(result['filename_size'])}",
  52. 'package_name': result['name'],
  53. 'version': f"v{result['version']}_{result['revision']}",
  54. 'tags': result['repository'],
  55. }
  56. )
  57. packages[pkg_url] = pkg_list
  58. results = []
  59. for pkg_url, pkg_list in packages.items():
  60. results.append(
  61. {
  62. 'url': pkg_url,
  63. 'template': 'packages.html',
  64. 'title': ' | '.join(x['title'] for x in pkg_list),
  65. 'content': pkg_list[0]['content'],
  66. 'package_name': ' | '.join(x['package_name'] for x in pkg_list),
  67. 'version': pkg_list[0]['version'],
  68. 'tags': [x['tags'] for x in pkg_list],
  69. }
  70. )
  71. return results