Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
# Open Library API base URL
|
| 5 |
OPEN_LIBRARY_API = "https://openlibrary.org/search.json"
|
|
@@ -28,32 +29,39 @@ subjects_list = [
|
|
| 28 |
"Modernist Literature", "Postmodernist Literature", "Contemporary Literature"
|
| 29 |
]
|
| 30 |
|
| 31 |
-
# Function to fetch books
|
| 32 |
def fetch_books(subjects, year_range):
|
| 33 |
query = " AND ".join([f"subject:{sub.replace(' ', '+')}" for sub in subjects])
|
| 34 |
year_query = f"first_publish_year:[{year_range[0]} TO {year_range[1]}]"
|
| 35 |
|
| 36 |
-
api_url = f"{OPEN_LIBRARY_API}?{query} AND {year_query}&limit=
|
| 37 |
response = requests.get(api_url)
|
| 38 |
|
| 39 |
if response.status_code == 200:
|
| 40 |
data = response.json()
|
| 41 |
-
books = []
|
| 42 |
-
|
| 43 |
-
for doc in data.get("docs", []):
|
| 44 |
-
book = {
|
| 45 |
-
"Title": doc.get("title", "N/A"),
|
| 46 |
-
"Author": ", ".join(doc.get("author_name", ["N/A"])),
|
| 47 |
-
"First Published Year": doc.get("first_publish_year", "N/A"),
|
| 48 |
-
}
|
| 49 |
-
books.append(book)
|
| 50 |
-
|
| 51 |
return books
|
| 52 |
else:
|
| 53 |
-
st.error("Failed to fetch
|
| 54 |
return []
|
| 55 |
|
| 56 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
st.title("π Book Finder")
|
| 58 |
|
| 59 |
# Multi-select for subjects
|
|
@@ -62,13 +70,24 @@ selected_subjects = st.multiselect("Select Literary Subjects", subjects_list, de
|
|
| 62 |
# Year range slider
|
| 63 |
year_range = st.slider("Select Year Range", min_value=1800, max_value=2025, value=(1900, 2025))
|
| 64 |
|
| 65 |
-
#
|
| 66 |
if st.button("π Search Books"):
|
| 67 |
with st.spinner("Fetching books..."):
|
| 68 |
-
|
| 69 |
|
| 70 |
-
if
|
| 71 |
-
st.success(f"π Found {len(
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
else:
|
| 74 |
st.warning("No books found for the selected criteria.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
+
import time
|
| 4 |
|
| 5 |
# Open Library API base URL
|
| 6 |
OPEN_LIBRARY_API = "https://openlibrary.org/search.json"
|
|
|
|
| 29 |
"Modernist Literature", "Postmodernist Literature", "Contemporary Literature"
|
| 30 |
]
|
| 31 |
|
| 32 |
+
# Function to fetch books by subjects and year range
|
| 33 |
def fetch_books(subjects, year_range):
|
| 34 |
query = " AND ".join([f"subject:{sub.replace(' ', '+')}" for sub in subjects])
|
| 35 |
year_query = f"first_publish_year:[{year_range[0]} TO {year_range[1]}]"
|
| 36 |
|
| 37 |
+
api_url = f"{OPEN_LIBRARY_API}?{query} AND {year_query}&limit=10"
|
| 38 |
response = requests.get(api_url)
|
| 39 |
|
| 40 |
if response.status_code == 200:
|
| 41 |
data = response.json()
|
| 42 |
+
books = [doc.get("title", "N/A") for doc in data.get("docs", [])]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return books
|
| 44 |
else:
|
| 45 |
+
st.error("Failed to fetch books.")
|
| 46 |
return []
|
| 47 |
|
| 48 |
+
# Function to fetch detailed book info
|
| 49 |
+
def fetch_book_details(book_title):
|
| 50 |
+
api_url = f"{OPEN_LIBRARY_API}?title={book_title.replace(' ', '+')}"
|
| 51 |
+
response = requests.get(api_url)
|
| 52 |
+
|
| 53 |
+
if response.status_code == 200:
|
| 54 |
+
data = response.json()
|
| 55 |
+
if data.get("docs"):
|
| 56 |
+
doc = data["docs"][0]
|
| 57 |
+
return {
|
| 58 |
+
"Title": doc.get("title", "N/A"),
|
| 59 |
+
"Author": ", ".join(doc.get("author_name", ["N/A"])),
|
| 60 |
+
"First Published Year": doc.get("first_publish_year", "N/A")
|
| 61 |
+
}
|
| 62 |
+
return None
|
| 63 |
+
|
| 64 |
+
# Streamlit UI
|
| 65 |
st.title("π Book Finder")
|
| 66 |
|
| 67 |
# Multi-select for subjects
|
|
|
|
| 70 |
# Year range slider
|
| 71 |
year_range = st.slider("Select Year Range", min_value=1800, max_value=2025, value=(1900, 2025))
|
| 72 |
|
| 73 |
+
# Search button
|
| 74 |
if st.button("π Search Books"):
|
| 75 |
with st.spinner("Fetching books..."):
|
| 76 |
+
book_titles = fetch_books(selected_subjects, year_range)
|
| 77 |
|
| 78 |
+
if book_titles:
|
| 79 |
+
st.success(f"π Found {len(book_titles)} books!")
|
| 80 |
+
|
| 81 |
+
book_details_list = []
|
| 82 |
+
for title in book_titles:
|
| 83 |
+
time.sleep(0.5) # Delay to avoid API rate limits
|
| 84 |
+
book_details = fetch_book_details(title)
|
| 85 |
+
if book_details:
|
| 86 |
+
book_details_list.append(book_details)
|
| 87 |
+
|
| 88 |
+
if book_details_list:
|
| 89 |
+
st.table(book_details_list)
|
| 90 |
+
else:
|
| 91 |
+
st.warning("No detailed book data found.")
|
| 92 |
else:
|
| 93 |
st.warning("No books found for the selected criteria.")
|