Skip to content

Instantly share code, notes, and snippets.

@daniel-schroeder-dev
Created July 2, 2022 19:48
Show Gist options
  • Select an option

  • Save daniel-schroeder-dev/1df1c6babccdc51cb89683d60461e12e to your computer and use it in GitHub Desktop.

Select an option

Save daniel-schroeder-dev/1df1c6babccdc51cb89683d60461e12e to your computer and use it in GitHub Desktop.
P12 - Lesson 12 - Exercise 2
"""
###################################################################################
Harvard Art Museum API
- Get API Key: https://docs.google.com/forms/d/1Fe1H4nOhFkrLpaeBpLAnSrIMYvcAxnYWm0IU9a6IkFA/viewform
- API Docs: https://github.com/harvardartmuseums/api-docs
- Base URL: https://api.harvardartmuseums.org
- Image route: /image
- Docs: https://github.com/harvardartmuseums/api-docs/blob/master/sections/image.md
Example request: https://api.harvardartmuseums.org/image?apikey=API_KEY&q=cat&size=3
###################################################################################
Gutendex Book API
- Docs: https://gutendex.com/
Example Request: https://gutendex.com/books/?search=dickens
###################################################################################
News API
- Register: https://newsapi.org/register
- Get Started: https://newsapi.org/docs/get-started
- Base API URL: https://newsapi.org/v2
- Search route: /everything
Example request: https://newsapi.org/v2/everything?q=bitcoin&apiKey=API_KEY
###################################################################################
"""
from urllib.request import urlopen
from urllib.parse import urlencode
from json import loads, load
def get_artworks(search_term, max_results):
BASE_API_URL = "https://api.harvardartmuseums.org"
endpoint = "/image"
API_KEY = "f83b19c6-9ac2-4e90-a2fa-f49e17f4f257"
query_params = {
"apikey": API_KEY,
"q": search_term,
"size": max_results,
}
encoded_query_params = urlencode(query_params)
request_url = f"{BASE_API_URL}{endpoint}?{encoded_query_params}"
with urlopen(request_url) as response:
response_data = loads(response.read())
return response_data["records"]
def get_articles(search_term, max_results):
BASE_API_URL = "https://newsapi.org/v2"
endpoint = "/everything"
API_KEY = "91cf9a43020d4980abf16073fc1ff0d3"
query_params = {
"apikey": API_KEY,
"q": search_term,
"pageSize": max_results,
}
encoded_query_params = urlencode(query_params)
request_url = f"{BASE_API_URL}{endpoint}?{encoded_query_params}"
with urlopen(request_url) as response:
response_data = loads(response.read())
return response_data["articles"]
def get_books(search_term, max_results):
BASE_API_URL = "https://gutendex.com"
endpoint = "/books"
query_params = {
"search": search_term,
}
encoded_query_params = urlencode(query_params)
request_url = f"{BASE_API_URL}{endpoint}?{encoded_query_params}"
with urlopen(request_url) as response:
books_json = loads(response.read())
return books_json["results"][:max_results]
def display_articles(articles):
print("\n************ Article results ************\n")
for article in articles:
print(f"\nTitle: {article['title']}")
print(f"Author: {article['author']}")
print(f"Description: {article['description']}")
print(f"URL: {article['url']}")
print("\n**********************************************\n")
def display_books(books):
print("\n************ Book results ************\n")
for book in books:
print(f"\nTitle: {book['title']}")
print(f"Author(s): {book['authors']}")
print(f"Subjects: {book['subjects']}")
print("\n**********************************************\n")
def display_artworks(artworks):
print("\n********** Artwork results **********\n")
for artwork in artworks:
print(f"\nDescription: {artwork['description']}")
print(f"URL: {artwork['baseimageurl']}")
print("\n*********************************************\n")
def display_welcome_banner():
welcome_banner = """
Welcome to your personal Search Engine!
This app lets you provide a search term and then
returns book/art/news results based on that
search term.
You can save selections that you enjoy to browse later!
"""
print(welcome_banner)
def save_search_results():
search_results = load_search_results()
print(search_results)
def load_search_results():
with open("search-results.json", mode="rt") as json_file:
search_results = load(json_file)
return search_results
options = """
1.) Search 2.) Save Search Results 3.) View Saved Search Results 4.) Exit
"""
SEARCH = 1
SAVE = 2
VIEW = 3
EXIT = 4
display_welcome_banner()
while True:
user_choice = int(input(options))
if user_choice == SEARCH:
search_term = input("Enter your search term: ")
max_results = int(input("How many results do you want? "))
artworks = get_artworks(search_term, max_results)
books = get_books(search_term, max_results)
articles = get_articles(search_term, max_results)
display_artworks(artworks)
display_books(books)
display_articles(articles)
elif user_choice == SAVE:
save_search_results()
elif user_choice == VIEW:
pass
elif user_choice == EXIT:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment