diff --git a/src/data-sources/goodreads.ts b/src/data-sources/goodreads.ts index 7f12148..701429f 100644 --- a/src/data-sources/goodreads.ts +++ b/src/data-sources/goodreads.ts @@ -206,41 +206,44 @@ export async function searchBooks(q: string): Promise { const url = "https://www.goodreads.com/search?q=" + encodeURIComponent(q); const res = await requestUrl({ url }); const doc = new DOMParser().parseFromString(res.text, "text/html"); - const bookElements = doc.querySelectorAll( - "table.tableList tr[itemtype='http://schema.org/Book']" - ); - const searchResults: SearchResult[] = []; - - bookElements.forEach((el) => { + return Array.from( + doc.querySelectorAll( + "table.tableList tr[itemtype='http://schema.org/Book']" + ) + ).map((el) => { const legacyId = parseInt( el.querySelector("div.u-anchorTarget")?.id ?? "", 10 ); - const title = el.querySelector("a.bookTitle")?.textContent?.trim(); - const authors = Array.from(el.querySelectorAll("a.authorName")) - .map((a) => a.textContent?.trim() || "") - .join(", "); - const coverImageUrl = el - .querySelector("img.bookCover") - ?.getAttribute("src"); + + const title = + el.querySelector("a.bookTitle")?.textContent?.trim() || ""; + + const authors = Array.from(el.querySelectorAll("a.authorName")).map( + (a) => a.textContent?.trim() || "" + ); + const avgRating = parseFloat( el .querySelector("span.minirating") ?.textContent?.match(/(\d+\.\d+) avg rating/)?.[1] ?? "0" ); + const ratingCount = parseInt( el .querySelector("span.minirating") ?.textContent?.match(/(\d[\d,]*) ratings/)?.[1] ?? "0", 10 ); + const publicationYear = parseInt( el .querySelector("span.greyText") ?.textContent?.match(/published (\d{4})/)?.[1] ?? "0", 10 ); + const editionCount = parseInt( el .querySelector("span.greyText") @@ -248,28 +251,18 @@ export async function searchBooks(q: string): Promise { 10 ); - if ( - !isNaN(legacyId) && - title && - authors && - !isNaN(avgRating) && - !isNaN(ratingCount) && - !isNaN(publicationYear) && - !isNaN(editionCount) && - coverImageUrl - ) { - searchResults.push({ - legacyId, - title, - authors: authors.split(", "), - avgRating, - ratingCount, - publicationYear, - editionCount, - coverImageUrl, - }); - } - }); + const coverImageUrl = + el.querySelector("img.bookCover")?.getAttribute("src") || ""; - return searchResults; + return { + legacyId, + title, + authors, + avgRating, + ratingCount, + publicationYear, + editionCount, + coverImageUrl, + }; + }); }