Added API interaction with Python
This commit is contained in:
39
joke-api/joke-api.py
Executable file
39
joke-api/joke-api.py
Executable 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()
|
||||||
Reference in New Issue
Block a user