diff --git a/src/lib/components/wishlists/ItemForm.svelte b/src/lib/components/wishlists/ItemForm.svelte index 031d952..ffd46d5 100644 --- a/src/lib/components/wishlists/ItemForm.svelte +++ b/src/lib/components/wishlists/ItemForm.svelte @@ -19,12 +19,23 @@ }).format(price); }; + const extractUrl = (url: string) => { + const urlRegex = /(https?):\/\/[^\s/$.?#].[^\s]*/; + const matches = url.match(urlRegex); + if (matches) { + return matches[0]; + } + return null; + }; + const getInfo = async () => { if (data.url && urlChanged) { loading = true; - const res = await fetch(`/api/product?url=${data.url}`); + const url = extractUrl(data.url); + const res = await fetch(`/api/product?url=${url}`); if (res.ok) { let productData: ProductData = await res.json(); + data.url = productData.url ? productData.url : url; data.name = productData.name ? productData.name : productData.title || ""; data.image_url = productData.image; data.price = formatPrice(productData.price, productData.currency); diff --git a/src/routes/api/product/+server.ts b/src/routes/api/product/+server.ts index a4a95e8..dfa7672 100644 --- a/src/routes/api/product/+server.ts +++ b/src/routes/api/product/+server.ts @@ -2,7 +2,7 @@ import { error } from "@sveltejs/kit"; import type { RequestHandler } from "./$types"; import { gotScraping } from "got-scraping"; -import metascraper from "metascraper"; +import metascraper, { type Metadata } from "metascraper"; import metascraperTitle from "metascraper-title"; import metascraperImage from "metascraper-image"; import metascraperUrl from "metascraper-url"; @@ -23,6 +23,10 @@ const goShopping = async (targetUrl: string) => { return metadata; }; +const isCaptchaResponse = (metadata: Metadata) => { + return metadata.image.toLocaleLowerCase().indexOf("captcha") >= 0; +}; + export const GET: RequestHandler = async ({ request }) => { const url = new URL(request.url).searchParams.get("url"); let isUrlValid = false; @@ -35,7 +39,12 @@ export const GET: RequestHandler = async ({ request }) => { } if (!isUrlValid) throw error(400, "valid url not provided"); - const metadata = await goShopping(url); + let metadata = await goShopping(url); + if (isCaptchaResponse(metadata)) { + // retry with the resolved URL + metadata = await goShopping(metadata.url); + } + return new Response(JSON.stringify(metadata)); } else { throw error(400, "must specify url in query parameters");