Self-Test: API#
Note
This self-test helps you check your understanding of the chapter API: Working with DraCor Programmatically.
There is no grading and nothing is stored.
Read the feedback for each option carefully, including when you answered correctly.
The exercises at the end contain code that you can run and change. Launch this page as a notebook from the rocket icon at the top (see Appendix A), or copy the code into your own Python environment.
Estimated time: 20–30 minutes.
Question 1#
Question 2#
Question 3#
Question 4#
Question 5#
Question 6#
Question 7#
Exercise 1: From front-end URL to metadata#
The following code turns a front-end URL into an API URL and prints some metadata of the play. Run it. Then find another play in the German Shakespeare Drama Corpus in the front-end, paste its URL and run the cell again.
import requests
frontend_url = "https://dracor.org/gersh/was-ihr-wollt"
corpusname, playname = frontend_url.replace("https://dracor.org/", "").split("/")
api_url = f"https://dracor.org/api/v1/corpora/{corpusname}/plays/{playname}"
response = requests.get(api_url, timeout=60)
if response.status_code == 200:
play = response.json()
print(play["title"], play["yearNormalized"], len(play["characters"]), "characters")
else:
print(f"Request failed with status code: {response.status_code}")
Was ihr wollt 1797 18 characters
Now change one letter in the play name and run the cell again. Which status code do you get, and why?
Exercise 2: Corpus metadata as a table#
The following code loads the metadata of all plays in the German Shakespeare Drama Corpus as a table. Run it, then answer with one or two lines of code: Which play has the most words (wordCountText)?
import io
import pandas as pd
import requests
url = "https://dracor.org/api/v1/corpora/gersh/metadata"
response = requests.get(url, headers={"Accept": "text/csv"}, timeout=60)
metadata_df = pd.read_csv(io.StringIO(response.text))
print(len(metadata_df), "plays")
metadata_df[["title", "yearNormalized", "numOfSpeakers", "wordCountText"]].head()
38 plays
| title | yearNormalized | numOfSpeakers | wordCountText | |
|---|---|---|---|---|
| 0 | Antonius und Cleopatra | 1831 | 75 | 24933 |
| 1 | Coriolanus | 1831 | 98 | 27822 |
| 2 | Cymbeline | 1832 | 41 | 27465 |
| 3 | Das Wintermärchen | 1832 | 38 | 25481 |
| 4 | Der Kaufmann von Venedig | 1799 | 26 | 21214 |
Solution
metadata_df.sort_values(by="wordCountText", ascending=False)[["title", "wordCountText"]].head(1)
Exercise 3: The same play with PyDraCor#
Get the play from Exercise 1 with PyDraCor and print the number of its characters. Do you get the same number as with requests?
from pydracor import DraCorAPI
dracor = DraCorAPI()
play = dracor.get_play("gersh", "was-ihr-wollt")
print(play.title)
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[10], line 1
----> 1 from pydracor import DraCorAPI
2
3 dracor = DraCorAPI()
4 play = dracor.get_play("gersh", "was-ihr-wollt")
ModuleNotFoundError: No module named 'pydracor'
Solution
print(len(play.characters), "characters")
Both numbers are the same: PyDraCor calls the same API endpoint as the requests code in Exercise 1.