Skip to content

Commit

Permalink
noto-fonts-monochrome-emoji: Automatically update metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
nbraud committed Sep 9, 2023
1 parent 0f2c6ab commit dc83279
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkgs/data/fonts/noto-fonts/noto-emoji.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"menu": "http://fonts.gstatic.com/s/notoemoji/v46/bMrnmSyK7YY-MEu6aWjPDs-ar6uWaGWuob-r0gwuQeU.ttf"
}
]
}
}
47 changes: 45 additions & 2 deletions pkgs/data/fonts/noto-fonts/noto-emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
import json


def getMetadata(apiKey: str, family: str = "Noto Emoji"):
'''Fetches the Google Fonts metadata for a given family.
An API key can be obtained by anyone with a Google account (🚮) from
`https://developers.google.com/fonts/docs/developer_api#APIKey`
'''
from urllib.parse import urlencode

with request.urlopen(
"https://www.googleapis.com/webfonts/v1/webfonts?" +
urlencode({ 'key': apiKey, 'family': family })
) as req:
return json.load(req)

def getUrls(metadata) -> Iterable[str]:
'''Fetch all files' URLs from Google Fonts' metadata.
Expand Down Expand Up @@ -81,10 +95,39 @@ def atomicFileUpdate(target: Path):


if __name__ == "__main__":
from os import environ
from urllib.error import HTTPError

environVar = 'GOOGLE_FONTS_TOKEN'
currentDir = Path(__file__).parent
metadataPath = currentDir / 'noto-emoji.json'

with (currentDir / 'noto-emoji.json').open() as f:
metadata = json.load(f)
try:
metadata = getMetadata(environ[environVar])

except (KeyError, HTTPError) as exn:
# No API key in the environment, or the query was rejected.
match exn:
case KeyError(environVar):
print(f"No '{environVar}' in the environment, "
"skipping metadata update")
case HTTPError(400):
print(f"Got HTTP 400 (Bad Request), is your API token valid?")
case _:
# Unknown error, let's bubble it up
raise

# In that case just use the existing metadata
with metadataPath.open() as f:
metadata = json.load(f)

lastModified = metadata["items"][0]["lastModified"];
print(f"Using metadata from file, last modified '{lastModified}'")

else:
# If metadata was successfully fetched, let's validate it
with atomicFileUpdate(metadataPath) as metadataFile:
json.dump(metadata, metadataFile, indent = 2)

hashPath = currentDir / 'noto-emoji.hashes.json'
try:
Expand Down

0 comments on commit dc83279

Please sign in to comment.