material_icons.py 2.0 KB

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