Skip to content

Commit ea8992e

Browse files
Committing changes; completed version 2.
1 parent deb1e6e commit ea8992e

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

code/zach/lab09-quotes-api.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
11
import requests
22

33
def version1():
4-
response = requests.get('https://favqs.com/api/qotd', headers={
5-
'Accept': 'application/json'})
4+
response = requests.get('https://favqs.com/api/qotd', headers={'Accept': 'application/json'})
65

76
quote = response.json()['quote']['body']
87
author = response.json()['quote']['author']
98

109
return print(f'"{quote}" - {author}')
1110

11+
def version2():
12+
page = 1
13+
index = 0
14+
search_term = input('Enter a keyword to search for quotes: ')
15+
16+
while True:
17+
response = requests.get(f'https://favqs.com/api/quotes?page={page}&filter={search_term}', headers={
18+
'Accept': 'application/json',
19+
'Authorization': 'Token token="855df50978dc9afd6bf86579913c9f8b"'})
20+
last_page = response.json()['last_page']
21+
quotes = response.json()['quotes']
22+
num_quotes = len(quotes)
23+
24+
if last_page == False:
25+
print(f'{num_quotes} quotes associated with {search_term} - page {page}')
26+
27+
for quote in quotes:
28+
print(f"'{response.json()['quotes'][index]['body']}' - {response.json()['quotes'][index]['author']}")
29+
index += 1
30+
31+
answer = input("Enter 'next page' or 'done': ")
32+
33+
if answer == 'done':
34+
return
35+
elif answer == 'next page':
36+
page += 1
37+
index = 0
38+
39+
1240
def main():
13-
version1()
41+
#version1()
42+
version2()
1443

1544
main()

0 commit comments

Comments
 (0)