Created
March 15, 2023 01:38
-
-
Save jasonraimondi/b8c0ed0d105d7bfb30f6dcf161f34df1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { load } from "cheerio"; | |
import cuid from "cuid"; | |
import type { Wish } from "@prisma/client"; | |
import { summarizeProductTitle } from "$lib/server/services/openai/summarize_title"; | |
export type Test = Omit<Wish, "wishlistId">; | |
export type Result = { nextLink?: string; items: Test[] }; | |
export async function fetchTitle(listId: string) { | |
const result = await fetch(`https://www.amazon.com/hz/wishlist/ls/${listId}`); | |
const text = await result.text(); | |
const $ = load(text); | |
return $("#profile-list-name").text(); | |
} | |
export async function fetchItems(url: string): Promise<Result> { | |
console.log("scraping", url) | |
const result = await fetch(url); | |
const text = await result.text(); | |
const $ = load(text); | |
const nextLink = $("a.wl-see-more").first().attr("href"); | |
const items: Test[] = []; | |
$(".g-item-sortable").each((i, el) => { | |
const title = $(el).find("img").attr("alt") ?? "Untitled"; | |
items.push({ | |
id: cuid(), | |
title, | |
image: $(el).find("img").attr("src") ?? null, | |
price: null, | |
description: null, | |
// price: parseInt(`${$(el).find(".a-price-whole").text().trim()}${$(el).find(".a-price-fraction").text()}`), | |
url: "https://www.amazon.com" + $(el).find(".a-link-normal").attr("href"), | |
createdAt: new Date(), | |
updatedAt: null, | |
}); | |
}); | |
return { | |
nextLink: nextLink ? "https://amazon.com" + nextLink : undefined, | |
items, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment