Skip to content

Commit 901d7d0

Browse files
committed
lab8 finished
1 parent 94ef454 commit 901d7d0

File tree

1 file changed

+56
-22
lines changed

1 file changed

+56
-22
lines changed

code/daniel/lab08-dad_api/lab08-dad_api.py

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,69 @@
1616
# response = requests.get(f"https://icanhazdadjoke.com/search?term=${search_term}", headers={
1717
# 'Accept': 'application/json'
1818
# })
19-
# ======================================================================================================
2019

21-
import time
22-
import requests
20+
#====================================
2321

24-
response = requests.get("https://icanhazdadjoke.com", headers={'Accept': 'application/json'})
25-
joke = response.json()
22+
# print(response.text) # {"ip":"76.105.187.182"} - raw json response
23+
# data = response.json() # turn raw json into a python dictionary
24+
# print(data) # {'ip': '76.105.187.182'} - python dictionary
25+
# print(data['ip']) # 76.105.187.182
2626

27-
# print(response.url)
28-
# print(response.status_code)
29-
# print(response.encoding)
30-
# print(response.headers)
31-
# print(response.text)
32-
# print(response.json())
3327

34-
print(joke)
28+
#=======================================================
29+
# Version1
30+
#=======================================================
3531

36-
data = response.json()
37-
# print(data[0]['title'])
38-
for film in data:
39-
print(film['title'])
40-
print(film['release_date'])
41-
print(film['description'])
42-
print('-'*10)
32+
# import requests
33+
# import time
4334

44-
#========================================
35+
# response = requests.get("https://icanhazdadjoke.com", headers={'Accept': 'application/json'})
4536

46-
# Add the ability to "search" for jokes using another endpoint. Create a REPL that allows one to enter a search term and go through jokes one at a time.
4737

38+
# # print(response.text)
39+
# joke = response.json()
40+
# # print(joke)
41+
42+
# dad_joke = input("Do you wank to hear a dad joke? y or n?: ")
4843

49-
# response = requests.get(f"https://icanhazdadjoke.com/search?term=${search_term}", headers={'Accept': 'application/json', params=query)})
44+
# while True:
45+
# if dad_joke == "y" or dad_joke == "yes":
46+
# print(joke['joke'])
47+
# break
48+
# elif dad_joke == "n" or dad_joke == "no":
49+
# print("goodbye")
50+
# break
51+
# else:
52+
# print("Not a valid answer")
53+
# break
5054

55+
56+
57+
#=======================================================
58+
# Version2
59+
#=======================================================
60+
61+
import requests
62+
import time
63+
64+
65+
search_joke = input("Enter a search term to find a related dad joke: ")
66+
# print(search_joke)
67+
68+
response = requests.get(f"https://icanhazdadjoke.com/search?term=${search_joke}", headers={'Accept': 'application/json'})
69+
# print(response)
70+
71+
72+
73+
joke = response.json()
74+
jokes = joke["results"]
75+
76+
for joke in jokes:
77+
print(joke["joke"])
78+
more_jokes = input("Do you want to hear another dad joke? yes/no: ")
79+
if more_jokes == "yes" or more_jokes == "y":
80+
print(joke["joke"])
81+
elif more_jokes == "no" or more_jokes == "n":
82+
break
83+
else:
84+
print("Not a valid answer")

0 commit comments

Comments
 (0)