-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 2 client-side python script examples of querying the LFF API, o…
…ne for langtags and one for fonts
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |