From 175ea4961cc0ed81cb0607165013e4a0ea13302e Mon Sep 17 00:00:00 2001 From: Zachary Christman Date: Wed, 20 Jul 2022 00:17:47 -0400 Subject: [PATCH 1/3] Setting up new branch and file for lab 9. --- code/zach/lab09-quotes-api.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 code/zach/lab09-quotes-api.py diff --git a/code/zach/lab09-quotes-api.py b/code/zach/lab09-quotes-api.py new file mode 100644 index 00000000..e69de29b From deb1e6ec56506963a56c180eb5c56ed8be86a1ea Mon Sep 17 00:00:00 2001 From: Zachary Christman Date: Wed, 20 Jul 2022 22:08:23 -0400 Subject: [PATCH 2/3] Completed version 1 of the lab. --- code/zach/lab09-quotes-api.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/code/zach/lab09-quotes-api.py b/code/zach/lab09-quotes-api.py index e69de29b..4cf162db 100644 --- a/code/zach/lab09-quotes-api.py +++ b/code/zach/lab09-quotes-api.py @@ -0,0 +1,15 @@ +import requests + +def version1(): + response = requests.get('https://favqs.com/api/qotd', headers={ + 'Accept': 'application/json'}) + + quote = response.json()['quote']['body'] + author = response.json()['quote']['author'] + + return print(f'"{quote}" - {author}') + +def main(): + version1() + +main() \ No newline at end of file From ea8992e21197a92543b529efbbd10293e8b8e1d7 Mon Sep 17 00:00:00 2001 From: Zachary Christman Date: Thu, 21 Jul 2022 23:23:39 -0400 Subject: [PATCH 3/3] Committing changes; completed version 2. --- code/zach/lab09-quotes-api.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/code/zach/lab09-quotes-api.py b/code/zach/lab09-quotes-api.py index 4cf162db..e36990b0 100644 --- a/code/zach/lab09-quotes-api.py +++ b/code/zach/lab09-quotes-api.py @@ -1,15 +1,44 @@ import requests def version1(): - response = requests.get('https://favqs.com/api/qotd', headers={ - 'Accept': 'application/json'}) + response = requests.get('https://favqs.com/api/qotd', headers={'Accept': 'application/json'}) quote = response.json()['quote']['body'] author = response.json()['quote']['author'] return print(f'"{quote}" - {author}') +def version2(): + page = 1 + index = 0 + search_term = input('Enter a keyword to search for quotes: ') + + while True: + response = requests.get(f'https://favqs.com/api/quotes?page={page}&filter={search_term}', headers={ + 'Accept': 'application/json', + 'Authorization': 'Token token="855df50978dc9afd6bf86579913c9f8b"'}) + last_page = response.json()['last_page'] + quotes = response.json()['quotes'] + num_quotes = len(quotes) + + if last_page == False: + print(f'{num_quotes} quotes associated with {search_term} - page {page}') + + for quote in quotes: + print(f"'{response.json()['quotes'][index]['body']}' - {response.json()['quotes'][index]['author']}") + index += 1 + + answer = input("Enter 'next page' or 'done': ") + + if answer == 'done': + return + elif answer == 'next page': + page += 1 + index = 0 + + def main(): - version1() + #version1() + version2() main() \ No newline at end of file