Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,51 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
| 3 |
import time
|
| 4 |
|
| 5 |
-
# Open Library API
|
| 6 |
-
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
"Classic Literature", "Modern Literature", "Postmodern Literature",
|
| 11 |
-
"
|
| 12 |
-
"
|
| 13 |
-
"
|
| 14 |
-
"Bildungsroman", "Satire", "Tragedy", "Comedy", "Dystopian Literature",
|
| 15 |
-
"Utopian Literature", "Magical Realism", "Stream of Consciousness",
|
| 16 |
-
"Metafiction", "Allegory", "Mythology", "Folklore", "Fairy Tales",
|
| 17 |
-
"Poetry", "Epic Poetry", "Lyric Poetry", "Sonnet", "Haiku", "Free Verse",
|
| 18 |
-
"Drama", "Tragicomedy", "Theater of the Absurd", "Shakespearean Plays",
|
| 19 |
-
"Greek Tragedy", "Modern Drama", "Surrealism in Literature", "Symbolism",
|
| 20 |
-
"Transcendentalism", "Stoicism", "Epicureanism", "Nihilism",
|
| 21 |
-
"Feminist Literature", "Postcolonial Literature", "Marxist Literary Criticism",
|
| 22 |
-
"Psychoanalytic Criticism", "Formalism", "New Criticism", "Cultural Studies",
|
| 23 |
-
"Comparative Literature", "World Literature", "African Literature",
|
| 24 |
-
"Asian Literature", "Latin American Literature", "European Literature",
|
| 25 |
-
"American Literature", "British Literature", "Russian Literature",
|
| 26 |
-
"French Literature", "German Literature", "Italian Literature",
|
| 27 |
-
"Spanish Literature", "Ancient Literature", "Medieval Literature",
|
| 28 |
-
"Renaissance Literature", "Enlightenment Literature", "Victorian Literature",
|
| 29 |
-
"Modernist Literature", "Postmodernist Literature", "Contemporary Literature"
|
| 30 |
]
|
| 31 |
|
| 32 |
-
# Function to fetch books
|
| 33 |
def fetch_books(subjects, year_range):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 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 |
-
#
|
| 68 |
-
selected_subjects = st.multiselect("Select
|
| 69 |
|
| 70 |
-
# Year range
|
| 71 |
-
year_range = st.slider("Select
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 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.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
import time
|
| 5 |
|
| 6 |
+
# Open Library API URL
|
| 7 |
+
BASE_API_URL = "https://openlibrary.org/search.json"
|
| 8 |
|
| 9 |
+
# Subject list
|
| 10 |
+
SUBJECTS = [
|
| 11 |
+
"Classic Literature", "Modern Literature", "Postmodern Literature", "Romanticism", "Realism",
|
| 12 |
+
"Science Fiction", "Fantasy Literature", "Historical Fiction", "Mystery and Detective Fiction",
|
| 13 |
+
"Horror Literature", "Adventure Novels", "Dystopian Literature", "Utopian Literature",
|
| 14 |
+
"Magical Realism", "Mythology", "Folklore", "Poetry", "Drama", "Tragedy", "Comedy"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
]
|
| 16 |
|
| 17 |
+
# Function to fetch books based on subject and year range
|
| 18 |
def fetch_books(subjects, year_range):
|
| 19 |
+
books = []
|
| 20 |
+
for subject in subjects:
|
| 21 |
+
query = f"{BASE_API_URL}?subject={subject.replace(' ', '+')}&publish_year=[{year_range[0]}+TO+{year_range[1]}]"
|
| 22 |
+
response = requests.get(query)
|
| 23 |
+
if response.status_code == 200:
|
| 24 |
+
data = response.json()
|
| 25 |
+
for book in data.get("docs", []):
|
| 26 |
+
books.append({
|
| 27 |
+
"title": book.get("title", "N/A"),
|
| 28 |
+
"author": ", ".join(book.get("author_name", ["N/A"])),
|
| 29 |
+
"first_publish_year": book.get("first_publish_year", "N/A")
|
| 30 |
+
})
|
| 31 |
+
time.sleep(1) # Rate limit handling
|
| 32 |
+
return books
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Streamlit UI
|
| 35 |
+
st.title("π Open Library Book Finder")
|
| 36 |
+
st.write("Search books by subject and publication year.")
|
| 37 |
|
| 38 |
+
# Subject selection
|
| 39 |
+
selected_subjects = st.multiselect("Select subjects", SUBJECTS, default=["Classic Literature"])
|
| 40 |
|
| 41 |
+
# Year range selection
|
| 42 |
+
year_range = st.slider("Select publication year range", min_value=1500, max_value=2025, value=(1900, 2025))
|
| 43 |
|
| 44 |
+
if st.button("Find Books"):
|
| 45 |
+
st.write("π Searching books...")
|
| 46 |
+
books = fetch_books(selected_subjects, year_range)
|
| 47 |
+
if books:
|
| 48 |
+
df = pd.DataFrame(books)
|
| 49 |
+
st.write(df)
|
| 50 |
+
else:
|
| 51 |
+
st.write("β No books found for the selected criteria.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|