-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (112 loc) · 4.39 KB
/
main.py
File metadata and controls
134 lines (112 loc) · 4.39 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
Command-line interface for the Library Management System.
Provides an interactive menu-driven interface for managing books,
including integration with the OpenLibrary API for fetching book metadata.
"""
import httpx
import json
from library import Library, Book
def display_menu():
"""Displays the main menu of the library application."""
print("\n--- Library Menu ---")
print("1. Add a new book")
print("2. Remove a book")
print("3. List all books")
print("4. Find a book by ISBN")
print("5. Update a book")
print("6. Add book from OpenLibrary")
print("7. Exit")
def get_book_details_from_openlibrary(isbn: str):
"""
Fetches book details from OpenLibrary using the most reliable search API.
"""
search_api_url = f"https://openlibrary.org/search.json?isbn={isbn}"
try:
with httpx.Client() as client:
response = client.get(search_api_url, follow_redirects=True)
response.raise_for_status()
data = response.json()
# Check if any documents were found
if data.get('numFound', 0) == 0 or not data.get('docs'):
print(f"No book found with ISBN {isbn} on OpenLibrary.")
return None
# Use the first result
book_data = data['docs'][0]
title = book_data.get('title', 'N/A')
author_names = ", ".join(book_data.get('author_name', ['N/A']))
# The year is often the first published year
year = book_data.get('first_publish_year', 0)
return {
'title': title,
'author': author_names,
'year': year,
'isbn': isbn
}
except (httpx.RequestError, httpx.HTTPStatusError, json.JSONDecodeError) as e:
print(f"Failed to fetch or parse data from OpenLibrary: {e}")
return None
except Exception as e:
print(f"An unexpected error occurred: {e}")
return None
def main():
"""The main function to run the library application."""
library = Library()
while True:
display_menu()
choice = input("Enter your choice: ")
if choice == '1':
title = input("Enter title: ")
author = input("Enter author: ")
isbn = input("Enter ISBN: ")
try:
year = int(input("Enter publication year: "))
book = Book(title, author, isbn, year)
library.add_book(book)
except ValueError:
print("Invalid year. Please enter a number.")
elif choice == '2':
isbn = input("Enter ISBN of the book to remove: ")
library.remove_book(isbn)
elif choice == '3':
library.list_books()
elif choice == '4':
isbn = input("Enter ISBN of the book to find: ")
book = library.find_book(isbn)
if book:
print(f"Found Book: {book.title} by {book.author}")
else:
print("Book not found.")
elif choice == '5':
isbn = input("Enter ISBN of the book to update: ")
if library.find_book(isbn):
print("Enter new details (leave blank to keep current value):")
title = input(f"Title: ")
author = input(f"Author: ")
year_str = input(f"Year: ")
update_data = {}
if title:
update_data['title'] = title
if author:
update_data['author'] = author
if year_str:
try:
update_data['year'] = int(year_str)
except ValueError:
print("Invalid year format.")
if update_data:
library.update_book(isbn, **update_data)
else:
print("Book not found.")
elif choice == '6':
isbn = input("Enter ISBN to fetch from OpenLibrary: ")
book_data = get_book_details_from_openlibrary(isbn)
if book_data:
book = Book(**book_data)
library.add_book(book)
elif choice == '7':
print("Exiting the application.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()