Skip to content

Instantly share code, notes, and snippets.

@bsesic
Last active April 18, 2024 19:14
Show Gist options
  • Save bsesic/7b3bff70c6f8deaf71e2f0e9484c9952 to your computer and use it in GitHub Desktop.
Save bsesic/7b3bff70c6f8deaf71e2f0e9484c9952 to your computer and use it in GitHub Desktop.
Get GND ID
import requests
def get_gnd_id(name: str, type: str) -> str:
"""Get the GND ID for a given name and type.
Args:
name (str): Name of the entity.
type (str): Type of the entity.
Returns:
str: GND ID of the entity.
"""
try:
request = requests.get(
"https://lobid.org/gnd/search?q=" + name + "&filter=type:" + type + "&format=json"
)
request_json = request.json()
#print(json.dumps(request_json, indent=4))
gnd_id = request_json["member"][0]["gndIdentifier"]
return gnd_id
except:
print("No GND ID found for " + name + " of type " + type + ".")
return None
# test it
print(get_gnd_id("Bach, Johann Sebastian", "Person"))
print(get_gnd_id("Löb, Aach", "Person"))
print(get_gnd_id("Aachen", "PlaceOrGeographicName"))
print(get_gnd_id("Komponist", "professionOrOccupation"))
@bsesic
Copy link
Author

bsesic commented Apr 18, 2024

This function fetches the GND Id for a person or a place with a given type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment