Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product scraping Captcha handling #95

Merged
merged 2 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading