Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Predefined list of top 100 subjects (you can expand or modify this list)
|
| 6 |
top_subjects = [
|
|
@@ -33,7 +35,8 @@ OPENLIBRARY_SUBJECTS_API_URL = "https://openlibrary.org/subjects"
|
|
| 33 |
OPENLIBRARY_BOOK_SEARCH_API_URL = "https://openlibrary.org/search.json"
|
| 34 |
|
| 35 |
# Function to fetch books by subject and time frame
|
| 36 |
-
|
|
|
|
| 37 |
books = []
|
| 38 |
for subject in subjects:
|
| 39 |
params = {
|
|
@@ -49,7 +52,8 @@ def fetch_books_by_subject_and_timeframe(subjects, start_year, end_year):
|
|
| 49 |
return books
|
| 50 |
|
| 51 |
# Function to fetch book details using Book Search API
|
| 52 |
-
|
|
|
|
| 53 |
response = requests.get(f"https://openlibrary.org{book_key}.json")
|
| 54 |
if response.status_code == 200:
|
| 55 |
book_data = response.json()
|
|
@@ -82,7 +86,8 @@ def fetch_book_details(book_key):
|
|
| 82 |
return None
|
| 83 |
|
| 84 |
# Function to scrape book description from OpenLibrary
|
| 85 |
-
|
|
|
|
| 86 |
response = requests.get(f"https://openlibrary.org{book_key}")
|
| 87 |
if response.status_code == 200:
|
| 88 |
soup = BeautifulSoup(response.text, 'html.parser')
|
|
@@ -112,13 +117,23 @@ def main():
|
|
| 112 |
st.error("Please select at least one subject.")
|
| 113 |
else:
|
| 114 |
with st.spinner("Fetching books..."):
|
|
|
|
| 115 |
books = fetch_books_by_subject_and_timeframe(selected_subjects, start_year, end_year)
|
| 116 |
if books:
|
| 117 |
-
st.success(f"Found {len(books)} books!")
|
| 118 |
for book in books:
|
| 119 |
book_key = book.get("key", "")
|
| 120 |
if book_key:
|
| 121 |
book_details = fetch_book_details(book_key)
|
| 122 |
if book_details:
|
| 123 |
st.subheader(book_details["Title"])
|
| 124 |
-
st.write(f"**First Published Year:** {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
+
from typing import List, Dict
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
# Predefined list of top 100 subjects (you can expand or modify this list)
|
| 8 |
top_subjects = [
|
|
|
|
| 35 |
OPENLIBRARY_BOOK_SEARCH_API_URL = "https://openlibrary.org/search.json"
|
| 36 |
|
| 37 |
# Function to fetch books by subject and time frame
|
| 38 |
+
@st.cache_data(ttl=3600)
|
| 39 |
+
def fetch_books_by_subject_and_timeframe(subjects: List[str], start_year: int, end_year: int) -> List[Dict]:
|
| 40 |
books = []
|
| 41 |
for subject in subjects:
|
| 42 |
params = {
|
|
|
|
| 52 |
return books
|
| 53 |
|
| 54 |
# Function to fetch book details using Book Search API
|
| 55 |
+
@st.cache_data(ttl=3600)
|
| 56 |
+
def fetch_book_details(book_key: str) -> Dict:
|
| 57 |
response = requests.get(f"https://openlibrary.org{book_key}.json")
|
| 58 |
if response.status_code == 200:
|
| 59 |
book_data = response.json()
|
|
|
|
| 86 |
return None
|
| 87 |
|
| 88 |
# Function to scrape book description from OpenLibrary
|
| 89 |
+
@st.cache_data(ttl=3600)
|
| 90 |
+
def scrape_book_description(book_key: str) -> str:
|
| 91 |
response = requests.get(f"https://openlibrary.org{book_key}")
|
| 92 |
if response.status_code == 200:
|
| 93 |
soup = BeautifulSoup(response.text, 'html.parser')
|
|
|
|
| 117 |
st.error("Please select at least one subject.")
|
| 118 |
else:
|
| 119 |
with st.spinner("Fetching books..."):
|
| 120 |
+
start_time = time.time()
|
| 121 |
books = fetch_books_by_subject_and_timeframe(selected_subjects, start_year, end_year)
|
| 122 |
if books:
|
| 123 |
+
st.success(f"Found {len(books)} books in {time.time() - start_time:.2f} seconds!")
|
| 124 |
for book in books:
|
| 125 |
book_key = book.get("key", "")
|
| 126 |
if book_key:
|
| 127 |
book_details = fetch_book_details(book_key)
|
| 128 |
if book_details:
|
| 129 |
st.subheader(book_details["Title"])
|
| 130 |
+
st.write(f"**First Published Year:** {book_details['First Published Year']}")
|
| 131 |
+
st.write(f"**Authors:** {book_details['Authors']}")
|
| 132 |
+
st.write(f"**Description:** {book_details['Description']}")
|
| 133 |
+
if book_details["Book Cover"] != "N/A":
|
| 134 |
+
st.image(book_details["Book Cover"])
|
| 135 |
+
else:
|
| 136 |
+
st.warning("No books found.")
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
main()
|