AnnNaser commited on
Commit
46232d4
Β·
verified Β·
1 Parent(s): 389903a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -33
app.py CHANGED
@@ -2,11 +2,12 @@ import streamlit as st
2
  import requests
3
  import pandas as pd
4
  import time
 
5
 
6
- # Open Library API base URL (using JSON endpoint for easy parsing)
7
  BASE_API_URL = "https://openlibrary.org/search.json"
8
 
9
- # List of available subjects (feel free to modify)
10
  SUBJECTS = [
11
  "Classic Literature", "Modern Literature", "Postmodern Literature", "Romanticism",
12
  "Realism", "Naturalism", "Existentialism", "Absurdism", "Gothic Fiction", "Science Fiction",
@@ -14,30 +15,21 @@ SUBJECTS = [
14
  "Adventure Novels", "Bildungsroman", "Epistolary Novels", "Satire", "Tragedy", "Comedy"
15
  ]
16
 
17
- # List of time facets available (as centuries)
18
- TIME_FACETS = [
19
- "16th century", "17th century", "18th century", "19th century", "20th century", "21st century"
20
- ]
21
-
22
- # Function to fetch books based on subjects and selected time facet
23
  def fetch_books(subjects, time_facet):
24
- books = []
25
- # Build the query string for subjects. If more than one subject is selected,
26
- # they will be joined with a space (which becomes a '+' in the URL).
27
  query_subject = " ".join(subjects)
28
 
29
- # Replace spaces with '+' for the URL
30
- query_subject_url = query_subject.replace(" ", "+")
31
-
32
- # Format the time facet by replacing spaces with '+'
33
- time_facet_url = "/" + time_facet.replace(" ", "+")
34
 
35
- # Build the full URL with the mode and time_facet parameters.
36
- query_url = f"{BASE_API_URL}?q={query_subject_url}&mode=everything&time_facet={time_facet_url}"
37
-
38
- st.write(f"πŸ”— Query URL: {query_url}") # Optional: show the query URL for debugging
39
 
40
  response = requests.get(query_url)
 
 
41
  if response.status_code == 200:
42
  data = response.json()
43
  for book in data.get("docs", []):
@@ -47,27 +39,29 @@ def fetch_books(subjects, time_facet):
47
  "first_publish_year": book.get("first_publish_year", "N/A")
48
  })
49
  else:
50
- st.error("Failed to fetch data from Open Library.")
51
 
52
- # Optional: small delay to be polite to the API server.
53
- time.sleep(1)
54
  return books
55
 
56
  # Streamlit UI
57
  st.title("πŸ“š Open Library Book Finder")
58
- st.write("Search books by subject and time facet.")
59
 
60
- # Subject selection: allow the user to choose one or more subjects.
61
  selected_subjects = st.multiselect("Select subject(s)", SUBJECTS, default=["Classic Literature"])
62
 
63
- # Time facet selection: choose a century.
64
- selected_time_facet = st.selectbox("Select time facet", TIME_FACETS)
65
 
66
  if st.button("Find Books"):
67
- st.write("πŸ” Searching books...")
68
- books = fetch_books(selected_subjects, selected_time_facet)
69
- if books:
70
- df = pd.DataFrame(books)
71
- st.write(df)
72
  else:
73
- st.write("❌ No books found for the selected criteria.")
 
 
 
 
 
 
 
2
  import requests
3
  import pandas as pd
4
  import time
5
+ import urllib.parse
6
 
7
+ # Base API URL for Open Library
8
  BASE_API_URL = "https://openlibrary.org/search.json"
9
 
10
+ # List of subjects for selection
11
  SUBJECTS = [
12
  "Classic Literature", "Modern Literature", "Postmodern Literature", "Romanticism",
13
  "Realism", "Naturalism", "Existentialism", "Absurdism", "Gothic Fiction", "Science Fiction",
 
15
  "Adventure Novels", "Bildungsroman", "Epistolary Novels", "Satire", "Tragedy", "Comedy"
16
  ]
17
 
 
 
 
 
 
 
18
  def fetch_books(subjects, time_facet):
19
+ # Join the selected subjects into one search query
 
 
20
  query_subject = " ".join(subjects)
21
 
22
+ # URL-encode the query components
23
+ query_subject_encoded = urllib.parse.quote_plus(query_subject)
24
+ time_facet_encoded = urllib.parse.quote_plus(time_facet)
 
 
25
 
26
+ # Build the full URL with the mode and time_facet parameters
27
+ query_url = f"{BASE_API_URL}?q={query_subject_encoded}&mode=everything&time_facet={time_facet_encoded}"
28
+ st.write(f"πŸ”— Query URL: {query_url}") # Optional: Display the URL for debugging purposes
 
29
 
30
  response = requests.get(query_url)
31
+ books = []
32
+
33
  if response.status_code == 200:
34
  data = response.json()
35
  for book in data.get("docs", []):
 
39
  "first_publish_year": book.get("first_publish_year", "N/A")
40
  })
41
  else:
42
+ st.error("Error fetching data from Open Library.")
43
 
44
+ time.sleep(1) # A short delay to be respectful to the API server
 
45
  return books
46
 
47
  # Streamlit UI
48
  st.title("πŸ“š Open Library Book Finder")
49
+ st.write("Search for books by subject and a custom time facet.")
50
 
51
+ # Let the user select one or more subjects
52
  selected_subjects = st.multiselect("Select subject(s)", SUBJECTS, default=["Classic Literature"])
53
 
54
+ # Let the user enter a custom time facet
55
+ time_facet = st.text_input("Enter a time facet (e.g., 20th century)")
56
 
57
  if st.button("Find Books"):
58
+ if not time_facet:
59
+ st.error("Please enter a time facet!")
 
 
 
60
  else:
61
+ st.write("πŸ” Searching books...")
62
+ books = fetch_books(selected_subjects, time_facet)
63
+ if books:
64
+ df = pd.DataFrame(books)
65
+ st.write(df)
66
+ else:
67
+ st.write("❌ No books found for the selected criteria.")