From 39d1af98df079aa5186e4a7cf8a0f2c20b875cab Mon Sep 17 00:00:00 2001 From: Carter Mintey Date: Sat, 20 Jan 2024 12:43:57 -0600 Subject: [PATCH 1/2] make requests use desktop and show error message when captcha response --- src/lib/components/wishlists/ItemForm.svelte | 13 ++++++++++++- src/routes/api/product/+server.ts | 12 ++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/lib/components/wishlists/ItemForm.svelte b/src/lib/components/wishlists/ItemForm.svelte index 41770bc..cec4f6f 100644 --- a/src/lib/components/wishlists/ItemForm.svelte +++ b/src/lib/components/wishlists/ItemForm.svelte @@ -3,6 +3,7 @@ import type { Item } from "@prisma/client"; import Backdrop from "$lib/components/Backdrop.svelte"; import { env } from "$env/dynamic/public"; + import { getToastStore } from "@skeletonlabs/skeleton"; export let data: Item; export let buttonText: string; @@ -10,6 +11,7 @@ $: form = $page.form; let loading = false; let urlChanged = false; + const toastStore = getToastStore(); const formatPrice = (price: number | null, currency: string | null) => { if (!price) return null; @@ -28,6 +30,15 @@ return null; }; + const triggerToast = () => { + toastStore.trigger({ + message: `Unable to find product information. You can still fill in the details manually.`, + background: "variant-filled-warning", + autohide: true, + timeout: 5000 + }); + }; + const getInfo = async () => { if (data.url && urlChanged) { loading = true; @@ -40,7 +51,7 @@ data.image_url = productData.image; data.price = formatPrice(productData.price, productData.currency); } else { - console.log("invalid url"); + triggerToast(); } loading = false; urlChanged = false; diff --git a/src/routes/api/product/+server.ts b/src/routes/api/product/+server.ts index 8332e5d..1c9f5a9 100644 --- a/src/routes/api/product/+server.ts +++ b/src/routes/api/product/+server.ts @@ -18,8 +18,13 @@ const scraper = metascraper([ ]); const goShopping = async (targetUrl: string) => { - const { body: html, url } = await gotScraping(targetUrl); - const metadata = await scraper({ html, url }); + const resp = await gotScraping({ + url: targetUrl, + headerGeneratorOptions: { + devices: ["desktop"] + } + }); + const metadata = await scraper({ html: resp.body, url: resp.url }); return metadata; }; @@ -44,6 +49,9 @@ export const GET: RequestHandler = async ({ request }) => { // retry with the resolved URL metadata = await goShopping(metadata.url); } + if (isCaptchaResponse(metadata)) { + error(424, "product information not available") + } return new Response(JSON.stringify(metadata)); } else { From 8381708534283b7c7d5d66a5920f431ca4af3c47 Mon Sep 17 00:00:00 2001 From: Carter Mintey Date: Sat, 20 Jan 2024 12:55:28 -0600 Subject: [PATCH 2/2] format --- src/routes/api/product/+server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/api/product/+server.ts b/src/routes/api/product/+server.ts index 1c9f5a9..4d29b5d 100644 --- a/src/routes/api/product/+server.ts +++ b/src/routes/api/product/+server.ts @@ -50,7 +50,7 @@ export const GET: RequestHandler = async ({ request }) => { metadata = await goShopping(metadata.url); } if (isCaptchaResponse(metadata)) { - error(424, "product information not available") + error(424, "product information not available"); } return new Response(JSON.stringify(metadata));