feat: podpora salátů z Pizza Chefie
ci/woodpecker/push/workflow Pipeline failed

This commit is contained in:
2026-04-02 10:51:46 +02:00
parent e9696f722c
commit d6729388ab
7 changed files with 246 additions and 52 deletions
+42 -3
View File
@@ -1,6 +1,7 @@
import axios from 'axios';
import { load } from 'cheerio';
import { getPizzaListMock } from './mock';
import { getPizzaListMock, getSalatListMock } from './mock';
import { Salat } from '../../types/gen/types.gen';
// TODO přesunout do types
type PizzaSize = {
@@ -20,7 +21,8 @@ type Pizza = {
// TODO mělo by být konfigurovatelné proměnnou z prostředí s tímhle jako default
const baseUrl = 'https://www.pizzachefie.cz';
const pizzyUrl = `${baseUrl}/pizzy.html?pobocka=plzen`;
const pizzyUrl = `${baseUrl}/pizzy.html`;
const salayUrl = `${baseUrl}/salaty.html`;
const buildPizzaUrl = (pizzaUrl: string) => {
return `${baseUrl}/${pizzaUrl}`;
@@ -34,9 +36,12 @@ const boxPrices: { [key: string]: number } = {
"50cm": 25
}
// Cena obalu pro salát
const SALAT_BOX_PRICE = 13;
/**
* Stáhne a scrapne aktuální pizzy ze stránek Pizza Chefie.
*
*
* @param mock zda vrátit pouze mock data
*/
export async function downloadPizzy(mock: boolean): Promise<Pizza[]> {
@@ -84,4 +89,38 @@ export async function downloadPizzy(mock: boolean): Promise<Pizza[]> {
});
}
return result;
}
/**
* Stáhne a scrapne aktuální saláty ze stránek Pizza Chefie.
* Příplatek za obal je pro každý salát pevně 13 Kč.
*
* @param mock zda vrátit pouze mock data
*/
export async function downloadSalaty(mock: boolean): Promise<Salat[]> {
if (mock) {
return new Promise((resolve) => setTimeout(() => resolve(getSalatListMock()), 1000));
}
const html = await axios.get(salayUrl).then(res => res.data);
const $ = load(html);
const links = $('.vypisproduktu > div > h4 > a');
const urls = [];
for (const element of links) {
if (element.name === 'a' && element.attribs?.href) {
urls.push(buildPizzaUrl(element.attribs.href));
}
}
const result: Salat[] = [];
for (const url of urls) {
const salatHtml = await axios.get(url).then(res => res.data);
const name = $('.produkt > h2', salatHtml).first().text().trim();
const ingredients: string[] = [];
$('.prisady > li', salatHtml).each((i, elm) => {
ingredients.push($(elm).text());
});
const priceText = $('.cena > span', salatHtml).first().text().trim();
const price = Number.parseInt(priceText.split(' Kč')[0]);
result.push({ name, ingredients, price: price + SALAT_BOX_PRICE });
}
return result;
}