From f81d85315bf9144c11877c49345a7cf84b3e6785 Mon Sep 17 00:00:00 2001 From: Nicolas Spalinger Date: Wed, 20 Mar 2024 09:22:27 -0500 Subject: [PATCH] Added 2 client-side python script examples of querying the LFF API, one for langtags and one for fonts --- contrib/lff_font_query_example.py | 26 ++++++++++++++++ contrib/lff_lang_query_example.py | 50 +++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100755 contrib/lff_font_query_example.py create mode 100755 contrib/lff_lang_query_example.py diff --git a/contrib/lff_font_query_example.py b/contrib/lff_font_query_example.py new file mode 100755 index 0000000..e5443ec --- /dev/null +++ b/contrib/lff_font_query_example.py @@ -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") diff --git a/contrib/lff_lang_query_example.py b/contrib/lff_lang_query_example.py new file mode 100755 index 0000000..e9c60f1 --- /dev/null +++ b/contrib/lff_lang_query_example.py @@ -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")