MCPcopy Create free account
hub / github.com/BrasilAPI/BrasilAPI / searchInOpenLibrary

Function searchInOpenLibrary

services/isbn/openLibrary.js:54–128  ·  view source on GitHub ↗
(isbn)

Source from the content-addressed store, hash-verified

52 * @returns The book details if found.
53 */
54export default async function searchInOpenLibrary(isbn) {
55 try {
56 const bibKey = `ISBN:${isbn}`;
57
58 const response = await axios.get(API_SEARCH_URL, {
59 params: {
60 bibkeys: bibKey,
61 jscmd: 'data',
62 format: 'json',
63 },
64 headers: { Accept: 'application/json' },
65 timeout: 5000,
66 });
67
68 if (Object.keys(response.data).length === 0 || !response.data[bibKey]) {
69 throw new NotFoundError({ message: 'ISBN não encontrado' });
70 }
71
72 const olBook = response.data[bibKey];
73
74 const { data: details } = await axios.get(
75 `${API_BASE_URL}/isbn/${isbn}.json`,
76 {
77 headers: {
78 Accept: 'application/json',
79 },
80 timeout: 5000,
81 }
82 );
83
84 return {
85 isbn,
86 title: olBook.title,
87 subtitle: olBook.subtitle,
88 authors: olBook.authors.map((contributor) => contributor.name),
89 publisher: olBook.publishers
90 .map((publisher) => publisher.name)
91 .join(' & '),
92 synopsis: details.description && details.description.value,
93 dimensions: parseDimensions(details.physical_dimensions),
94 year: parseYear(olBook.publish_date),
95 format: 'PHYSICAL',
96 page_count: olBook.number_of_pages,
97 subjects: (olBook.subjects || []).map((subject) => subject.name),
98 location:
99 olBook.publish_places &&
100 olBook.publish_places[0] &&
101 olBook.publish_places[0].name &&
102 olBook.publish_places[0].name.replace('Brazil', 'Brasil'),
103 retail_price: null,
104 cover_url: olBook.cover && olBook.cover.large,
105 provider: 'open-library',
106 };
107 } catch (error) {
108 // Log the error for debugging
109 // eslint-disable-next-line no-console
110 console.error('[open-library] Error fetching ISBN:', {
111 isbn,

Callers

nothing calls this directly

Calls 2

parseYearFunction · 0.85
parseDimensionsFunction · 0.70

Tested by

no test coverage detected