Skip to content

Commit 872ee97

Browse files
Merge pull request #107 from PdxCodeGuild/zach-lab09-quotes-api
Zach lab09 quotes api
2 parents 4c0c0b4 + ea8992e commit 872ee97

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

code/zach/lab09-quotes-api.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import requests
2+
3+
def version1():
4+
response = requests.get('https://favqs.com/api/qotd', headers={'Accept': 'application/json'})
5+
6+
quote = response.json()['quote']['body']
7+
author = response.json()['quote']['author']
8+
9+
return print(f'"{quote}" - {author}')
10+
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+
40+
def main():
41+
#version1()
42+
version2()
43+
44+
main()

0 commit comments

Comments
 (0)