Skip to content

Commit

Permalink
Product scraping Captcha handling (#95)
Browse files Browse the repository at this point in the history
* make requests use desktop and show error message when captcha response

* format
  • Loading branch information
cmintey authored Jan 20, 2024
1 parent 8f0af35 commit 769c350
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/lib/components/wishlists/ItemForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
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;
$: form = $page.form;
let loading = false;
let urlChanged = false;
const toastStore = getToastStore();
const formatPrice = (price: number | null, currency: string | null) => {
if (!price) return null;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions src/routes/api/product/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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 {
Expand Down

0 comments on commit 769c350

Please sign in to comment.