|
22 | 22 | # print(f'The author is {quote_author}\nTheir quote: {quote_body}')
|
23 | 23 |
|
24 | 24 |
|
25 |
| - |
26 |
| - |
27 | 25 | # VERSION 2 #
|
28 | 26 |
|
29 | 27 |
|
30 | 28 |
|
31 |
| -import pprint |
32 |
| - |
33 |
| - |
34 |
| - |
| 29 | +page_number = 1 # Starts page_number at page 1 |
35 | 30 |
|
| 31 | +search_term = input("Enter a keyword to search for quotes: ") # Gives the user an input to search for term |
36 | 32 |
|
37 |
| -page_number = 1 |
38 | 33 |
|
39 |
| -search_term = 'everything' |
| 34 | +def get_response(search_term): # Make a function to make code reusable and keep program clean |
| 35 | + """ Pulls JSON from API and converts it into a dictionary |
40 | 36 |
|
| 37 | + Args: |
| 38 | + search_term (string): Term that the API uses to search with |
| 39 | + |
| 40 | + Returns: |
| 41 | + boolean: True if last page, False if not |
| 42 | + dictionary: API response(big jumbled mess of dictionary key/value pairs) |
| 43 | + """ |
| 44 | + response = requests.get( |
| 45 | + f"https://favqs.com/api/quotes?page={page_number}&filter={search_term}", # F string that builds the API URL |
| 46 | + headers={"Authorization": 'Token token="855df50978dc9afd6bf86579913c9f8b"'}, # API Token which gives access [200]=Success [401]=Unathorized |
| 47 | + ) |
41 | 48 |
|
42 |
| -response = requests.get(f"https://favqs.com/api/quotes?page={page_number}&filter={search_term}", |
43 |
| - headers = {'Authorization': 'Token token="855df50978dc9afd6bf86579913c9f8b"'},) |
| 49 | + json_response = response.json() # creates dictionary from JSON |
44 | 50 |
|
| 51 | + is_last_page = json_response["last_page"] # "last_page is a field inside JSON_response which returns True or False THEN assigns it to is_last_page" |
45 | 52 |
|
46 |
| -json_response = response.json() |
47 |
| -# pprint.pprint(json_response) |
48 |
| -is_last_page = json_response['last_page'] |
| 53 | + return is_last_page, json_response |
49 | 54 |
|
50 | 55 |
|
51 |
| -# is_last_page == |
52 |
| -# pprint.pprint(json_response['quotes']) |
53 |
| -# pprint.pprint(json_response['quotes'][0]) |
| 56 | +is_last_page = False # Assigning False so the loop will run at least once |
| 57 | +quotes_list = [] # Assigning an Empty list to the quotes_list(initiating dictionary) |
| 58 | +while is_last_page == False: # Beginning while loop |
| 59 | + is_last_page, json_response = get_response(search_term) # Calling function and assigning responses |
| 60 | + for i in range(len(json_response["quotes"])): # For loop to loop through all the quotes |
54 | 61 |
|
55 |
| -quotes_list = [] |
| 62 | + author_json = json_response["quotes"][i]["author"] # Get author from quotes for the current quote, i |
| 63 | + quote_json = json_response["quotes"][i]["body"] # Get quote body from quotes for the current quote, i |
56 | 64 |
|
57 |
| -for i in range(len(json_response['quotes'])): |
58 |
| - |
59 |
| - author_json = json_response['quotes'][i]['author'] |
60 |
| - quote_json = json_response['quotes'][i]['body'] |
61 |
| - |
62 |
| - quote_dictionary = { |
63 |
| - 'author': author_json, |
64 |
| - 'quote': quote_json |
65 |
| - } |
66 |
| - quotes_list.append(quote_dictionary) |
67 |
| -pprint.pprint(quotes_list) |
68 |
| -# print(len(json_response['quotes'])) |
| 65 | + print(f'"{quote_json}"\nBy {author_json}\n') # Print formatted string to display the quote and author |
| 66 | + user_is_done = input("enter 'next page' or 'done': ").lower().strip() # Ask the user if they want to continue |
| 67 | + page_number += 1 # Incrementing page_number by 1 each loop |
| 68 | + if user_is_done == "done": # Taking user inputs "done" and breaking the loop ending the program |
| 69 | + break |
0 commit comments