diff --git a/books.db b/books.db index fa36490..018d634 100644 Binary files a/books.db and b/books.db differ diff --git a/index.js b/index.js index 35f0b58..a1479d9 100644 --- a/index.js +++ b/index.js @@ -61,14 +61,28 @@ app.get('/book/:isbn', async (req, res) => { // First try Open Library const openLibraryResponse = await axios.get(`https://openlibrary.org/api/books?bibkeys=ISBN:${isbn}&format=json&jscmd=data`); const bookData = openLibraryResponse.data[`ISBN:${isbn}`]; + //Check for timeout or rate limit: + if(openLibraryResponse.status === 429){ + console.log('Rate limit exceeded'); + res.status(429).json({ error: 'Rate limit exceeded' }); + } + + if(openLibraryResponse.status === 408){ + console.log('Request Timeout'); + res.status(408).json({ error: 'Request Timeout' }); + } if (bookData) { console.log('Book data found in Open Library'); res.json(formatOpenLibraryData(bookData)); } else { + console.log('Book not found in Open Library'); + console.log('Trying Internet Archive...'); + // Fallback to Internet Archive if no data from Open Library const archiveResponse = await axios.get(`https://archive.org/advancedsearch.php?q=isbn:${isbn}&output=json`); const archiveData = archiveResponse.data; + console.log(archiveData); if (archiveData.response.numFound > 0) { res.json(formatArchiveData(archiveData.response.docs[0])); @@ -84,6 +98,39 @@ app.get('/book/:isbn', async (req, res) => { +// Endpoint to search for book by title +app.get('/search-title', async (req, res) => { + const { title } = req.query; + console.log(`Searching for book by title: ${title}`); + + try { + // Search Open Library by title, limit to 5 results + const openLibraryResponse = await axios.get(`https://openlibrary.org/search.json?q=${encodeURIComponent(title)}&limit=7`); + const searchResults = openLibraryResponse.data.docs; + console.log(searchResults); + + if (searchResults.length > 0) { + console.log('Books found by title'); + const bookData = searchResults.map(result => ({ + title: result.title, + authors: result.author_name || [], + publish_date: result.first_publish_year, + isbn: result.isbn ? result.isbn[0] : '', + publisher: result.publisher ? result.publisher[0] : '', + key: result.key // Unique key to fetch more detailed data later if needed + })); + console.log(bookData); + res.json({ results: bookData }); + } else { + res.status(404).json({ error: 'No books found with that title.' }); + } + } catch (error) { + console.error(error); + res.status(500).json({ error: 'Failed to search for book by title' }); + } +}); + + // Endpoint to store book in the database // Endpoint to store book in the database // Endpoint to store book in the database diff --git a/public/index.html b/public/index.html index 22b5eaa..2ce849a 100644 --- a/public/index.html +++ b/public/index.html @@ -3,20 +3,20 @@
-Title: ${book.title}
+Author(s): ${book.authors.join(', ')}
+Published: ${book.publish_date || 'N/A'}
+ISBN: ${book.isbn}
+Publisher: ${book.publisher || 'N/A'}
+ + `; + bookInfoDiv.appendChild(bookElement); + }); + + // Store the results so that we can reference them when the user makes a selection + window.searchResults = results; +} + +function selectBook(index) { + const selectedBook = window.searchResults[index]; + console.log('Selected book:', selectedBook); + + // You can now fetch more details using the unique key if needed, or directly store this in your database + const isbn = selectedBook.isbn; + + if (isbn) { + fetchBookInfo(isbn); + } else { + promptUserWithBook(selectedBook); // You might have to adapt this if there's no ISBN + } +} + + async function storeBookInDatabase(bookData) { try { const response = await fetch('/store-book', { @@ -164,7 +229,7 @@ async function storeBookInDatabase(bookData) { // Start the scanner when the start button is clicked -document.getElementById('start-scanner').addEventListener('click', startScanner); +//document.getElementById('start-scanner').addEventListener('click', startScanner); // Get cameras on page load window.onload = getCameras; diff --git a/public/styles.css b/public/styles.css index 8496d2f..2cd6791 100644 --- a/public/styles.css +++ b/public/styles.css @@ -20,10 +20,6 @@ canvas.drawing, canvas.drawingBuffer { margin-top: 20px; } -#title-input { - display: none; -} - #prompt-message { font-weight: bold; }