Skip to content

Commit 35392d1

Browse files
committed
added search term and ability to change the page
1 parent 2ab931f commit 35392d1

File tree

1 file changed

+91
-9
lines changed

1 file changed

+91
-9
lines changed

Code/matthew/python/Labs/lab14.py

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Dad joke API
33
"""
4+
45
import requests
56
import time
67
'''
@@ -16,18 +17,44 @@
1617
'''
1718

1819
# --- Version 2
19-
20-
response = requests.get('https://icanhazdadjoke.com/search', headers={
21-
'Accept': 'application/json'
22-
})
23-
24-
dad_jokes_list = response.json()
20+
# """
21+
# response = requests.get('https://icanhazdadjoke.com/search?term=&page=1', headers={
22+
# 'Accept': 'application/json',
23+
# })
24+
# dad_jokes_list = response.json()
25+
# print(dad_jokes_list)
2526

2627
# print(dad_jokes_list['results'][1]['joke'])
28+
2729
counter = 0
28-
joke = dad_jokes_list['results'][counter]['joke']
2930
break_loop = ['no', 'stop', 'quit', 'q', 'exit']
31+
yes = ['yes','yeah','yea','ya','y']
32+
3033
while True:
34+
35+
if counter == 0:
36+
response = requests.get(f'https://icanhazdadjoke.com/search?term=&page=1', headers={
37+
'Accept': 'application/json',
38+
})
39+
elif counter >= 1:
40+
change_pg = input('Would you like to change the page your on?: \n')
41+
if change_pg in yes:
42+
page = input('What page do you want to start on? \n')
43+
for i in page:
44+
while True:
45+
if i.isdigit() == False:
46+
page = input('What page do you want to start on? \n')
47+
else:
48+
break
49+
response = requests.get(f'https://icanhazdadjoke.com/search?term=&page={page}', headers={
50+
'Accept': 'application/json',
51+
})
52+
53+
dad_jokes_list = response.json()
54+
joke = dad_jokes_list['results'][counter]['joke']
55+
56+
# print(dad_jokes_list['current_page'])
57+
3158
print('Joke incoming')
3259
time.sleep(.5)
3360
print('In three')
@@ -36,8 +63,63 @@
3663
time.sleep(1)
3764
print('one')
3865
time.sleep(1.25)
39-
print(f"\njoke")
66+
print(f"\n{joke}")
67+
counter += 1
68+
again = input('\nWould you like to hear another one?\n')
69+
if again.lower() in break_loop :
70+
break
71+
72+
# """
73+
# --- Term search
74+
'''
75+
76+
break_loop = ['no', 'stop', 'quit', 'q', 'exit']
77+
yes = ['yes','yeah','yea','ya','y']
78+
counter = 0
79+
while True:
80+
look_up_wrd = input('What type of jokes do you want to hear? (enter a keyword):\n')
81+
82+
url = f'https://icanhazdadjoke.com/search?term={look_up_wrd}'
83+
response = requests.get(url, headers={
84+
'Accept': 'application/json'
85+
})
86+
dad_jokes_list = response.json()
87+
88+
joke = dad_jokes_list['results'][counter]['joke']
89+
print('Joke incoming')
90+
time.sleep(.5)
91+
print('In three')
92+
time.sleep(.75)
93+
print('two')
94+
time.sleep(1)
95+
print('one')
96+
time.sleep(1.25)
97+
print(f"\n{joke}")
4098
counter += 1
4199
again = input('\nWould you like to hear another one?\n')
42100
if again.lower() in break_loop :
43-
break
101+
break
102+
different_word = input("would you like to look for a different kind of joke?: \n")
103+
if different_word.lower() in yes:
104+
look_up_wrd = input('What type of jokes do you want to hear? (enter a keyword):\n')
105+
# '''
106+
107+
# # from PIL import Image
108+
# from io import BytesIO
109+
# # import urllib
110+
# response = requests.get("https://icanhazdadjoke.com/j/<joke_id>.png")
111+
112+
# # print(dir(response))
113+
# print(response.url)
114+
# # print(response.raw) # <urllib3.response.HTTPResponse object at 0x0000020FB1511BD0>
115+
116+
# joke_image = response.url
117+
118+
# # with open('sample_image.png', 'wb') as file:
119+
# # file.write(joke_image)
120+
# # file.close()
121+
122+
123+
# urllib.request.urlretrieve(joke_image,'C:\Users\wmatt\Pictures')
124+
125+

0 commit comments

Comments
 (0)