Added API interaction with Python

This commit is contained in:
Ezri Brimhall 2024-10-08 15:38:55 -06:00
parent c60a6d4229
commit 5024838868
Signed by: ezri
GPG Key ID: 058A78E5680C6F24

39
joke-api/joke-api.py Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/env python3
import requests
import json
import time
def download(url):
response = requests.get(url)
content = response.content.decode("utf-8")
return content
def main():
iterations = 0
while iterations < 3:
iterations += 1
programming_jokes = {}
while len(programming_jokes) < 5:
# Parse the JSON response
jokes = json.loads(
download("https://official-joke-api.appspot.com/jokes/random/10")
)
# Filter to just programming jokes and insert them
for joke in jokes:
if joke["type"] == "programming":
programming_jokes[joke["id"]] = joke
for joke in programming_jokes.values():
print(joke["setup"])
for _ in range(3):
print(".", end="", flush=True)
time.sleep(1)
print()
print(joke["punchline"])
time.sleep(2)
if __name__ == "__main__":
main()