File size: 2,511 Bytes
b32a878
 
2c7a8ea
58dbb9a
46232d4
b32a878
46232d4
2c7a8ea
b32a878
46232d4
2c7a8ea
90e92e8
 
 
389903a
ffea045
b32a878
389903a
46232d4
389903a
 
46232d4
 
 
389903a
46232d4
 
 
389903a
 
46232d4
 
389903a
 
 
 
 
 
 
 
 
46232d4
389903a
46232d4
2c7a8ea
58dbb9a
 
2c7a8ea
46232d4
d58b538
46232d4
389903a
ffea045
46232d4
 
ffea045
2c7a8ea
46232d4
 
2c7a8ea
46232d4
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import streamlit as st
import requests
import pandas as pd
import time
import urllib.parse

# Base API URL for Open Library
BASE_API_URL = "https://openlibrary.org/search.json"

# List of subjects for selection
SUBJECTS = [
    "Classic Literature", "Modern Literature", "Postmodern Literature", "Romanticism",
    "Realism", "Naturalism", "Existentialism", "Absurdism", "Gothic Fiction", "Science Fiction",
    "Fantasy Literature", "Historical Fiction", "Mystery and Detective Fiction", "Horror Literature",
    "Adventure Novels", "Bildungsroman", "Epistolary Novels", "Satire", "Tragedy", "Comedy"
]

def fetch_books(subjects, time_facet):
    # Join the selected subjects into one search query
    query_subject = " ".join(subjects)
    
    # URL-encode the query components
    query_subject_encoded = urllib.parse.quote_plus(query_subject)
    time_facet_encoded = urllib.parse.quote_plus(time_facet)
    
    # Build the full URL with the mode and time_facet parameters
    query_url = f"{BASE_API_URL}?q={query_subject_encoded}&mode=everything&time_facet={time_facet_encoded}"
    st.write(f"πŸ”— Query URL: {query_url}")  # Optional: Display the URL for debugging purposes
    
    response = requests.get(query_url)
    books = []
    
    if response.status_code == 200:
        data = response.json()
        for book in data.get("docs", []):
            books.append({
                "title": book.get("title", "N/A"),
                "author": ", ".join(book.get("author_name", ["N/A"])),
                "first_publish_year": book.get("first_publish_year", "N/A")
            })
    else:
        st.error("Error fetching data from Open Library.")
    
    time.sleep(1)  # A short delay to be respectful to the API server
    return books

# Streamlit UI
st.title("πŸ“š Open Library Book Finder")
st.write("Search for books by subject and a custom time facet.")

# Let the user select one or more subjects
selected_subjects = st.multiselect("Select subject(s)", SUBJECTS, default=["Classic Literature"])

# Let the user enter a custom time facet
time_facet = st.text_input("Enter a time facet (e.g., 20th century)")

if st.button("Find Books"):
    if not time_facet:
        st.error("Please enter a time facet!")
    else:
        st.write("πŸ” Searching books...")
        books = fetch_books(selected_subjects, time_facet)
        if books:
            df = pd.DataFrame(books)
            st.write(df)
        else:
            st.write("❌ No books found for the selected criteria.")