Skip to content

Commit

Permalink
fix: Made imports more failure tolerant
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Groß <mail@gross-johannes.de>
  • Loading branch information
jo-gross committed Feb 21, 2024
1 parent d7536df commit a5f372c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
5 changes: 3 additions & 2 deletions components/ingredients/IngredientForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export function IngredientForm(props: IngredientFormProps) {
name={'link'}
/>
<button
className={`btn btn-primary join-item ${values.fetchingExternalData ? 'loading' : ''}`}
className={`btn btn-primary join-item`}
type={'button'}
disabled={
!(
Expand All @@ -304,7 +304,7 @@ export function IngredientForm(props: IngredientFormProps) {
values.link.includes('metro.de') ||
values.link.includes('rumundco.de') ||
values.link.includes('delicando.com')
)
) || values.fetchingExternalData
}
onClick={async () => {
await setFieldValue('fetchingExternalData', true);
Expand All @@ -330,6 +330,7 @@ export function IngredientForm(props: IngredientFormProps) {
});
}}
>
{values.fetchingExternalData ? <span className={'loading loading-spinner'}></span> : <></>}
<FaSyncAlt />
</button>
</div>
Expand Down
39 changes: 16 additions & 23 deletions pages/api/scraper/ingredient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse)
});

const image = imageResponse != undefined ? 'data:image/jpg;base64,' + Buffer.from(await imageResponse.arrayBuffer()).toString('base64') : undefined;
const name = soup.find('h1', 'product--title').text.replace('\n', '').trim();
const price = soup.find('meta', { itemprop: 'price' }).attrs.content;

const volume =
Number(soup.find('div', 'conheadtabel').contents[0].contents[1].contents[1].contents[1].text.replace('\n', '').replace(',', '.').split(' ')[0].trim()) *
100;
const name = soup.find('h1', 'product--title')?.text?.replace('\n', '')?.trim() ?? '';
const price = soup.find('meta', { itemprop: 'price' })?.attrs?.content ?? 0;
const volume = (soup.find('div', ['product--price', 'price--unit'])?.contents?.[1]?.['_text']?.split(' ')?.[0] ?? 0) * 1000;

const result: ResponseBody = {
name: name,
Expand All @@ -59,13 +56,9 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse)

const image = imageResponse != undefined ? 'data:image/jpg;base64,' + Buffer.from(await imageResponse.arrayBuffer()).toString('base64') : undefined;

const name = soup.find('h1', 'item-detail__headline').text.replace('\n', '').trim();

let price = soup.find('div', 'undiscountedPrice').contents[0]._text.trim().replace(',', '.').replace('€', '').trim();
if (price == undefined) {
price = 0;
}
const volume = Number(name.split('Vol. ')[1].split(' ')[0].replace(',', '.').replace('l', '').replace('ml', '')) * 100;
const name = soup.find('h1', 'item-detail__headline')?.text?.replace('\n', '')?.trim() ?? '';
const price = soup.find('div', 'undiscountedPrice')?.contents?.[0]?._text?.trim()?.replace(',', '.')?.replace('€', '')?.trim() ?? 0;
const volume = (name.split('Vol. ')?.[1]?.split(' ')?.[0]?.replace(',', '.')?.replace('l', '')?.replace('ml', '') ?? 0) * 100;

return res.json({
name: name,
Expand All @@ -83,11 +76,16 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse)
await page.goto(req.query.url);

const imageUrl = await page.locator('#mainImage').first().getAttribute('src');
const name = await page.locator('div.mfcss_article-detail--title > h2 > span').first().innerText();
let price = await page
.locator('div.mfcss_article-detail--price-container > div.row > div.text-right > div > span.mfcss_article-detail--price-breakdown > span > span')
.allInnerTexts();
price = price[1].replace(',', '.').replace('€', '').trim();
const name = (await page.locator('div.mfcss_article-detail--title > h2 > span')?.first()?.innerText()) ?? '';
const price =
(
await page
.locator('div.mfcss_article-detail--price-container > div.row > div.text-right > div > span.mfcss_article-detail--price-breakdown > span > span')
?.allInnerTexts()
)?.[1]
?.replace(',', '.')
?.replace('€', '')
?.trim() ?? 0;

await browser.close();

Expand All @@ -99,12 +97,7 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse)

const image = imageResponse != undefined ? 'data:image/jpg;base64,' + Buffer.from(await imageResponse.arrayBuffer()).toString('base64') : undefined;

if (price == undefined) {
price = 0;
}
const volume = 0;
// // Number(name.split('Vol. ')[1].split(' ')[0].replace(',', '.').replace('l', '').replace('ml', '')) * 100;
//
return res.json({
name: name,
image: image,
Expand Down

0 comments on commit a5f372c

Please sign in to comment.