AnnNaser commited on
Commit
2c7a8ea
Β·
verified Β·
1 Parent(s): 58dbb9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -80
app.py CHANGED
@@ -1,93 +1,51 @@
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"
7
 
8
- # List of subjects
9
- subjects_list = [
10
- "Classic Literature", "Modern Literature", "Postmodern Literature",
11
- "Romanticism", "Realism", "Naturalism", "Existentialism", "Absurdism",
12
- "Gothic Fiction", "Science Fiction", "Fantasy Literature", "Historical Fiction",
13
- "Mystery and Detective Fiction", "Horror Literature", "Adventure Novels",
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 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
68
- selected_subjects = st.multiselect("Select Literary Subjects", subjects_list, default=["Classic Literature"])
69
 
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.")
 
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.")