Skip to content

Commit 769c350

Browse files
authored
Product scraping Captcha handling (#95)
* make requests use desktop and show error message when captcha response * format
1 parent 8f0af35 commit 769c350

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/lib/components/wishlists/ItemForm.svelte

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import type { Item } from "@prisma/client";
44
import Backdrop from "$lib/components/Backdrop.svelte";
55
import { env } from "$env/dynamic/public";
6+
import { getToastStore } from "@skeletonlabs/skeleton";
67
78
export let data: Item;
89
export let buttonText: string;
910
1011
$: form = $page.form;
1112
let loading = false;
1213
let urlChanged = false;
14+
const toastStore = getToastStore();
1315
1416
const formatPrice = (price: number | null, currency: string | null) => {
1517
if (!price) return null;
@@ -28,6 +30,15 @@
2830
return null;
2931
};
3032
33+
const triggerToast = () => {
34+
toastStore.trigger({
35+
message: `Unable to find product information. You can still fill in the details manually.`,
36+
background: "variant-filled-warning",
37+
autohide: true,
38+
timeout: 5000
39+
});
40+
};
41+
3142
const getInfo = async () => {
3243
if (data.url && urlChanged) {
3344
loading = true;
@@ -40,7 +51,7 @@
4051
data.image_url = productData.image;
4152
data.price = formatPrice(productData.price, productData.currency);
4253
} else {
43-
console.log("invalid url");
54+
triggerToast();
4455
}
4556
loading = false;
4657
urlChanged = false;

src/routes/api/product/+server.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ const scraper = metascraper([
1818
]);
1919

2020
const goShopping = async (targetUrl: string) => {
21-
const { body: html, url } = await gotScraping(targetUrl);
22-
const metadata = await scraper({ html, url });
21+
const resp = await gotScraping({
22+
url: targetUrl,
23+
headerGeneratorOptions: {
24+
devices: ["desktop"]
25+
}
26+
});
27+
const metadata = await scraper({ html: resp.body, url: resp.url });
2328
return metadata;
2429
};
2530

@@ -44,6 +49,9 @@ export const GET: RequestHandler = async ({ request }) => {
4449
// retry with the resolved URL
4550
metadata = await goShopping(metadata.url);
4651
}
52+
if (isCaptchaResponse(metadata)) {
53+
error(424, "product information not available");
54+
}
4755

4856
return new Response(JSON.stringify(metadata));
4957
} else {

0 commit comments

Comments
 (0)