1. #
  2. # Copyright 2022 Simon Duerr dev@simonduerr.eu
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>
  16. #
  17. """
  18. Access Humanality artwork.
  19. Based directly on Bioicons script by Simon Deurr.
  20. Downloads the database to cache and uses it locally for searching.
  21. """
  22. import json
  23. import requests
  24. from datetime import datetime
  25. import os
  26. from cachecontrol import CacheControl, CacheControlAdapter
  27. from cachecontrol.caches.file_cache import FileCache
  28. from cachecontrol.heuristics import ExpiresAfter
  29. from import_sources import RemoteSource
  30. def local_search(query, db):
  31. for item in db:
  32. if query.lower() in item["name"].lower():
  33. yield item
  34. return
  35. class Humanality(RemoteSource):
  36. name = "Humanality Project"
  37. icon = "sources/humanality.svg"
  38. db_url = "https://rolandixor.pro/assets/humanality.json"
  39. icon_url = "https://rolandixor.pro/assets/humanality"
  40. def __init__(self, cache_dir):
  41. self.session = requests.session()
  42. self.cache_dir = cache_dir
  43. self.session.mount(
  44. "https://",
  45. CacheControlAdapter(
  46. cache=FileCache(cache_dir),
  47. heuristic=ExpiresAfter(days=5),
  48. ),
  49. )
  50. # check if local db is up to date with Last Modified header
  51. try:
  52. response = requests.head(self.db_url)
  53. except Exception:
  54. response = None
  55. self._json = os.path.join(self.cache_dir, "humanality.json")
  56. if not os.path.isfile(self._json):
  57. last_modified = 0
  58. else:
  59. last_modified = os.path.getmtime(self._json)
  60. if response is not None:
  61. last_update = datetime.strptime(
  62. response.headers["Last-Modified"], "%a, %d %b %Y %H:%M:%S GMT"
  63. ).timestamp()
  64. # if icon db was modified download new database
  65. if last_modified < last_update:
  66. self.to_local_file(self.db_url)
  67. def search(self, query):
  68. results = []
  69. if os.path.isfile(self._json):
  70. with open(self._json, "r") as f:
  71. db = json.load(f)
  72. results = local_search(query, db)
  73. for item in results:
  74. yield {
  75. "id": item["name"],
  76. "name": item["name"],
  77. "summary": item["category"],
  78. "created": None,
  79. "popularity": 0,
  80. "author": item["author"],
  81. "thumbnail": f"{self.icon_url}/{item['name']}.svg",
  82. "file": f"{self.icon_url}/{item['name']}.svg",
  83. "license": item["license"],
  84. }
 
 

284

humanality.py

-

PasteBin

Linhas
98
Palavras
327
Tamanho
3,2 KB
Criado
Tipo
text/x-python
General Public License v3 (GPLv3)
Por favor faça seu login para deixar um comentário!