Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove google fonts api key #334

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions sphinx_immaterial/google_fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import json
import os
import re
from typing import Dict, List, Set, Tuple, Optional
from typing import Dict, List, Set, Tuple, Optional, cast, Any
import urllib.parse

import sphinx.application
Expand All @@ -24,9 +24,6 @@

logger = sphinx.util.logging.getLogger(__name__)

# From Google Fonts API Explorer
_GOOGLE_FONTS_API_KEY = "AIzaSyAa8yy0GdcGPHdtD083HiGGx_S0vMPScDM"

# https://stackoverflow.com/questions/25011533/google-font-api-uses-browser-detection-how-to-get-all-font-variations-for-font
_FONT_FORMAT_USER_AGENT = {
"ttf": "Safari 3.1 Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_2; en-gb) AppleWebKit/526+ (KHTML, like Gecko) Version/3.1 iPhone",
Expand Down Expand Up @@ -144,13 +141,13 @@ async def do_fetch():
css_futures = []
# Fetch list of fonts
font_metadata = json.loads(
get_url(
cache_dir,
f"https://content-webfonts.googleapis.com/v1/webfonts?key={_GOOGLE_FONTS_API_KEY}",
headers={"x-referer": "https://explorer.apis.google.com"},
).decode("utf-8")
get_url(cache_dir, "https://fonts.google.com/metadata/fonts").decode(
"utf-8"
)
)
font_families = {item["family"]: item for item in font_metadata["items"]}
font_families = {
item["family"]: item for item in font_metadata["familyMetadataList"]
}
for font in fonts:
metadata = font_families.get(font)
if metadata is None:
Expand All @@ -160,7 +157,9 @@ async def do_fetch():
sorted(font_families),
)
continue
for variant in metadata["variants"]:
for variant in cast(
Dict[str, Dict[str, Any]], metadata["fonts"]
).keys():
css_future_keys.append((font, variant))
css_futures.append(fetch_font(font, variant))
css_content = dict(zip(css_future_keys, await asyncio.gather(*css_futures)))
Expand Down
14 changes: 12 additions & 2 deletions sphinx_immaterial/graphviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,18 @@ def render_dot_html(
ttf_font_paths = google_fonts.get_ttf_font_paths(self.builder.app)
ttf_font: Optional[str] = None
if ttf_font_paths and font is not None:
# can only support the chosen font if cache exists and a Google font is used
ttf_font = ttf_font_paths[(font, "regular")]
try:
# can only support the chosen font if cache exists and a Google font is used
ttf_font = ttf_font_paths[(font, "400")]
except KeyError as exc:
# weight `400` might not exist for the specified font
all_font_keys = [i for i in ttf_font_paths.keys() if i[0] == font]
if not all_font_keys:
raise FileNotFoundError(
f"Font file for {font} could not be found in cache"
) from exc
# just use first weight for the specified font
ttf_font = ttf_font_paths[all_font_keys[0]]

code = _replace_resolved_xrefs(node, code)

Expand Down