Skip to content

Commit 26fbab5

Browse files
Merge pull request #108 from PdxCodeGuild/jonpan-lab09-quotes-api
jonpan submitting lab09
2 parents 918b59a + 866f428 commit 26fbab5

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

code/jonpan/lab09_v1.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#Lab09: Quotes API
2+
3+
import requests
4+
import pprint
5+
6+
response = requests.get("https://favqs.com/api/qotd", headers={'Accept': 'application/json'})
7+
data = response.json()
8+
9+
pprint.pprint(data)
10+
# print(type(data))
11+
12+
# print(f"\nAuthor: {data['quote']['author']}")
13+
# print(f"\nQuote: {data['quote']['body']}")
14+
15+
quotes = {
16+
'Author': data['quote']['author'],
17+
'Quote': data['quote']['body']
18+
}
19+
20+
print(f"\nAuthor: {quotes['Author']}")
21+
print(f"\nQuote: {quotes['Quote']}")

code/jonpan/lab09_v2.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import requests
2+
import pprint
3+
4+
def print_results(p_num, search_term):
5+
response = requests.get(f"https://favqs.com/api/quotes?page=<page>&filter={search_term}", params={'page':{p_num}},
6+
headers={'Accept': 'application/json', 'Authorization': 'Token token="855df50978dc9afd6bf86579913c9f8b"'})
7+
data = response.json()
8+
9+
for i in range(len(data['quotes'])):
10+
print(f"\nAuthor: {data['quotes'][i]['author']}")
11+
print(f"\nQuote: {data['quotes'][i]['body']}")
12+
13+
search_term = input("enter a keyword to search for quotes: ")
14+
p_num = 1
15+
16+
# search_term and p_num are defined outside the function so need to be included in the function(here)
17+
18+
while True:
19+
print_results(p_num, search_term)
20+
cont = input("enter 'next page' or 'done' ")
21+
if cont == 'done':
22+
print("Goodbye")
23+
break
24+
else:
25+
p_num += 1
26+
27+
28+
# pprint.pprint(data)
29+
# print(f"\nAuthor: {data['quotes'][0]['author']}")

0 commit comments

Comments
 (0)