generated from tpl/obsidian-sample-plugin
			Clean up goodreads search function
This commit is contained in:
		
							parent
							
								
									f2ac5736b2
								
							
						
					
					
						commit
						785ca0e884
					
				| 
						 | 
				
			
			@ -206,41 +206,44 @@ export async function searchBooks(q: string): Promise<SearchResult[]> {
 | 
			
		|||
	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<SearchResult>((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<SearchResult[]> {
 | 
			
		|||
			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,
 | 
			
		||||
		};
 | 
			
		||||
	});
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue