material_icons.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Material Icons (images)
  4. """
  5. import re
  6. from json import loads
  7. about = {
  8. "website": 'https://fonts.google.com/icons',
  9. "wikidata_id": 'Q107315222',
  10. "official_api_documentation": None,
  11. "use_official_api": False,
  12. "require_api_key": False,
  13. "results": 'JSON',
  14. }
  15. search_url = "https://fonts.google.com/metadata/icons?key=material_symbols&incomplete=true"
  16. result_url = "https://fonts.google.com/icons?icon.query={query}&selected=Material+Symbols+Outlined:{icon_name}:FILL@0{fill};wght@400;GRAD@0;opsz@24" # pylint: disable=line-too-long
  17. img_src_url = "https://fonts.gstatic.com/s/i/short-term/release/materialsymbolsoutlined/{icon_name}/{svg_type}/24px.svg"
  18. filled_regex = r"(fill)(ed)?"
  19. def request(query, params):
  20. params['url'] = search_url
  21. params['query'] = query
  22. return params
  23. def response(resp):
  24. results = []
  25. query = resp.search_params["query"].lower()
  26. json_results = loads(resp.text[5:])
  27. outlined = not re.findall(filled_regex, query)
  28. query = re.sub(filled_regex, "", query).strip()
  29. svg_type = "fill1" if not outlined else "default"
  30. query_parts = query.split(" ")
  31. for result in json_results["icons"]:
  32. for part in query_parts:
  33. if part in result["name"] or part in result["tags"] or part in result["categories"]:
  34. break
  35. else:
  36. continue
  37. tags = [tag.title() for tag in result["tags"]]
  38. categories = [category.title() for category in result["categories"]]
  39. results.append(
  40. {
  41. 'template': 'images.html',
  42. 'url': result_url.format(icon_name=result["name"], query=result["name"], fill=0 if outlined else 1),
  43. 'img_src': img_src_url.format(icon_name=result["name"], svg_type=svg_type),
  44. 'title': result["name"].replace("_", "").title(),
  45. 'content': ", ".join(tags) + " / " + ", ".join(categories),
  46. }
  47. )
  48. return results