-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookAPI.js
More file actions
144 lines (124 loc) · 5.52 KB
/
Copy pathbookAPI.js
File metadata and controls
144 lines (124 loc) · 5.52 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
135
136
137
138
139
140
141
142
143
144
const axios = require('axios');
class BookAPI {
constructor() {
this.openLibraryAPI = 'https://openlibrary.org';
this.googleBooksAPI = 'https://www.googleapis.com/books/v1/volumes';
}
async searchBook(query) {
try {
const response = await axios.get(`${this.googleBooksAPI}?q=${encodeURIComponent(query)}&maxResults=10`);
if (response.data.items) {
return response.data.items.map(item => this.parseGoogleBook(item));
}
return [];
} catch (error) {
console.error('Error searching books:', error);
return [];
}
}
async getBookByISBN(isbn) {
try {
const cleanISBN = isbn.replace(/[-\s]/g, '');
const googleResponse = await axios.get(`${this.googleBooksAPI}?q=isbn:${cleanISBN}`);
if (googleResponse.data.items && googleResponse.data.items.length > 0) {
return this.parseGoogleBook(googleResponse.data.items[0]);
}
const openLibResponse = await axios.get(`${this.openLibraryAPI}/isbn/${cleanISBN}.json`);
if (openLibResponse.data) {
return await this.parseOpenLibraryBook(openLibResponse.data, cleanISBN);
}
return null;
} catch (error) {
console.error('Error fetching book by ISBN:', error);
try {
const cleanISBN = isbn.replace(/[-\s]/g, '');
const openLibResponse = await axios.get(`${this.openLibraryAPI}/isbn/${cleanISBN}.json`);
if (openLibResponse.data) {
return await this.parseOpenLibraryBook(openLibResponse.data, cleanISBN);
}
} catch (secondError) {
console.error('Secondary fetch attempt failed:', secondError);
}
return null;
}
}
parseGoogleBook(item) {
const volumeInfo = item.volumeInfo || {};
const imageLinks = volumeInfo.imageLinks || {};
return {
title: volumeInfo.title || '',
author: volumeInfo.authors ? volumeInfo.authors.join(', ') : '',
publisher: volumeInfo.publisher || '',
publication_year: volumeInfo.publishedDate ? new Date(volumeInfo.publishedDate).getFullYear() : null,
isbn: volumeInfo.industryIdentifiers ?
volumeInfo.industryIdentifiers.find(id => id.type === 'ISBN_13' || id.type === 'ISBN_10')?.identifier : '',
page_count: volumeInfo.pageCount || null,
genre: volumeInfo.categories ? volumeInfo.categories[0] : '',
cover_url: imageLinks.thumbnail || imageLinks.smallThumbnail || '',
description: volumeInfo.description || ''
};
}
async parseOpenLibraryBook(bookData, isbn) {
let coverUrl = '';
try {
coverUrl = `https://covers.openlibrary.org/b/isbn/${isbn}-L.jpg`;
const checkCover = await axios.head(coverUrl).catch(() => null);
if (!checkCover || checkCover.status !== 200) {
coverUrl = bookData.covers && bookData.covers.length > 0 ?
`https://covers.openlibrary.org/b/id/${bookData.covers[0]}-L.jpg` : '';
}
} catch (error) {
coverUrl = '';
}
let authorName = '';
if (bookData.authors && bookData.authors.length > 0) {
try {
const authorKey = bookData.authors[0].key || bookData.authors[0];
const authorResponse = await axios.get(`${this.openLibraryAPI}${authorKey}.json`);
authorName = authorResponse.data.name || '';
} catch (error) {
console.error('Error fetching author:', error);
}
}
let publisherName = '';
let publicationYear = null;
if (bookData.publishers && bookData.publishers.length > 0) {
publisherName = bookData.publishers[0];
}
if (bookData.publish_date) {
const year = bookData.publish_date.match(/\d{4}/);
if (year) {
publicationYear = parseInt(year[0]);
}
}
return {
title: bookData.title || '',
author: authorName,
publisher: publisherName,
publication_year: publicationYear,
isbn: isbn,
page_count: bookData.number_of_pages || null,
genre: bookData.subjects ? bookData.subjects[0] : '',
cover_url: coverUrl,
description: bookData.description ?
(typeof bookData.description === 'string' ? bookData.description : bookData.description.value || '') : ''
};
}
async getCoverFromISBN(isbn) {
try {
const cleanISBN = isbn.replace(/[-\s]/g, '');
const googleResponse = await axios.get(`${this.googleBooksAPI}?q=isbn:${cleanISBN}`);
if (googleResponse.data.items && googleResponse.data.items.length > 0) {
const imageLinks = googleResponse.data.items[0].volumeInfo.imageLinks;
if (imageLinks) {
return imageLinks.thumbnail || imageLinks.smallThumbnail || '';
}
}
return `https://covers.openlibrary.org/b/isbn/${cleanISBN}-L.jpg`;
} catch (error) {
console.error('Error fetching cover:', error);
return '';
}
}
}
module.exports = BookAPI;