Skip to content

Commit

Permalink
Added 2 client-side python script examples of querying the LFF API, o…
Browse files Browse the repository at this point in the history
…ne for langtags and one for fonts
  • Loading branch information
n7s committed Mar 20, 2024
1 parent 9439c9a commit f81d853
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
26 changes: 26 additions & 0 deletions contrib/lff_font_query_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/python3
"""
API usage example:
- query a particular font via user input
- return 404 and error message if font is unknown
"""

import json
import requests

URL = "https://lff.api.languagetechnology.org/family/"
family = input("Pick your font family name (e.g. payaplanna): ")
fullURL = URL + family

response = requests.get(fullURL)

if response.status_code == 200:
data = json.loads(response.text)
print("\nLFF API result for font: " + family)
for k in data:
print(k, ":", data[k])

if response.status_code == 404:
print("Error: HTTP status code", response.status_code)
print('"' + family + '"' + " is not a known font family")
print("Sorry, no API result, try another font family")
50 changes: 50 additions & 0 deletions contrib/lff_lang_query_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/python3
"""
API usage example:
- query a particular langtag via user input
- return 404 and error message if langtag is unknown
- access nested fields
- filter out some responses
"""

from pprint import pprint
import json
import requests

URL = "https://lff.api.languagetechnology.org/lang/"
lang = input("Pick your langtag (e.g. nod-Lana-TH wsg-Gong-IN shu-Arab-TD - tag-Script-REGION): ")
fullURL = URL + lang

response = requests.get(fullURL)

if response.status_code == 200:
data = json.loads(response.text)
for k, v in data["families"].items():
print("\nLFF API result for langtag: " + lang)
print(f'family: {v["family"]}')
print(f'version: {v["version"]}')
print(f'source: {v["source"]}')
print(f'distributable: {v["distributable"]}')
print(f'license: {v["license"]}')
print(f'status: {v["status"]}')
print(f'download: {v["packageurl"]}')

print("\nCSS @font-face URLs (filtered for woff2 only)")

result = {
k: f["flourl"]
for v in data["families"].values()
for k, f in v["files"].items()
if k.endswith("woff2")
}
if response.status_code == 404:
print("Error: HTTP status code", response.status_code)
print("\"" + lang + "\"" + " is not a known langtag")

# examine the headers
# pprint(response.headers)

try:
pprint(result)
except NameError:
print("Sorry, no API result, try another langtag")

0 comments on commit f81d853

Please sign in to comment.