Prvotní nástřel pizza parseru

This commit is contained in:
Martin Berka 2023-04-15 08:05:46 +02:00
parent 81f48ebbe2
commit bf379e13ed
13 changed files with 10876 additions and 29175 deletions

View File

@ -1,46 +1,7 @@
# Getting Started with Create React App
# Pizza Day
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
Zatím to nemá dokumentaci.
Klient je v tomto adresáři, server v adresáři /server, obojí lze spustit pomocí:
## Available Scripts
In the project directory, you can run:
### `npm start`
Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
The page will reload if you make edits.\
You will also see any lint errors in the console.
### `npm test`
Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `npm run build`
Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `npm run eject`
**Note: this is a one-way operation. Once you `eject`, you cant go back!**
If you arent satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point youre on your own.
You dont have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldnt feel obligated to use this feature. However we understand that this tool wouldnt be useful if you couldnt customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
### `yarn`
### `yarn start`

29105
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -10,7 +10,9 @@
"@types/node": "^16.18.23",
"@types/react": "^18.0.33",
"@types/react-dom": "^18.0.11",
"bootstrap": "^5.2.3",
"react": "^18.2.0",
"react-bootstrap": "^2.7.2",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"typescript": "^4.9.5",

1
server/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/node_modules

22
server/package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "luncher-server",
"version": "1.0.0",
"main": "src/index.ts",
"license": "MIT",
"scripts": {
"start": "ts-node src/index.ts"
},
"devDependencies": {
"@types/request-promise": "^4.1.48",
"ts-node": "^10.9.1",
"typescript": "^5.0.2"
},
"dependencies": {
"cheerio": "^1.0.0-rc.12",
"cors": "^2.8.5",
"express": "^4.18.2",
"request": "^2.88.2",
"request-promise": "^4.2.6",
"socket.io": "^4.6.1"
}
}

106
server/src/chefie.ts Normal file
View File

@ -0,0 +1,106 @@
import $ from 'cheerio';
import rp from 'request-promise';
import os from 'os';
import path from 'path';
import fs from 'fs';
type PizzaSize = {
size: string,
pizzaPrice: string,
boxPrice: number,
price: number
}
type Pizza = {
name: string,
ingredients: string[],
sizes: PizzaSize[],
}
// 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 buildPizzaUrl = (pizzaUrl: string) => {
return `${baseUrl}/${pizzaUrl}`;
}
// Ceny krabic dle velikosti
const boxPrices = {
"30cm": 13,
"35cm": 15,
"40cm": 18,
"50cm": 25
}
/**
* Stáhne a scrapne aktuální pizzy ze stránek Pizza Chefie.
*/
const downloadPizzy = async () => {
// Získáme seznam pizz
const html = await rp(pizzyUrl);
const links = $('.vypisproduktu > div > h4 > a', html)
const urls = [];
for (let i = 0; i < links.length; i++) {
if (links[i].name === 'a' && links[i].attribs?.href) {
const pizzaUrl = links[i].attribs?.href;
urls.push(buildPizzaUrl(pizzaUrl));
}
}
// Scrapneme jednotlivé pizzy
const result: Pizza[] = [];
for (let i = 0; i < urls.length; i++) {
const pizzaUrl = urls[i];
const pizzaHtml = await rp(pizzaUrl);
// Název
const name = $('.produkt > h2', pizzaHtml).first().text()
// Přísady
const ingredients = []
const ingredientsHtml = $('.prisady > li', pizzaHtml);
ingredientsHtml.each((i, elm) => {
ingredients.push($(elm).text());
})
// Velikosti
const sizes = [];
const a = $('.varianty > li > a', pizzaHtml);
a.each((i, elm) => {
const size = $('span', elm).text();
const priceKc = $(elm).text().split(size).pop().trim();
const price = Number.parseInt(priceKc.split(" Kč")[0]);
sizes.push({ size: size, pizzaPrice: price, boxPrice: boxPrices[size], price: price + boxPrices[size] });
})
result.push({
name: name,
ingredients: ingredients,
sizes: sizes,
});
}
return result;
}
/**
* Vrátí pizzy z tempu, nebo čerstvě stažené, pokud v tempu nejsou.
*/
export const fetchPizzy = async () => {
const tmpDir = os.tmpdir();
const date_ob = new Date();
const date = ("0" + date_ob.getDate()).slice(-2);
const month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
const year = date_ob.getFullYear();
const dateStr = year + "-" + month + "-" + date;
const dataPath = path.join(tmpDir, `chefie-${dateStr}.json`);
if (fs.existsSync(dataPath)) {
console.log(`Soubor pro ${dataPath} již existuje, bude použit.`);
const rawdata = fs.readFileSync(dataPath);
return JSON.parse(rawdata.toString());
} else {
console.log(`Soubor pro ${dataPath} neexistuje, stahuji...`);
const pizzy = await downloadPizzy();
fs.writeFileSync(dataPath, JSON.stringify(pizzy));
console.log(`Zapsán ${dataPath}`);
return pizzy;
}
}

66
server/src/index.ts Normal file
View File

@ -0,0 +1,66 @@
import express from "express";
import { Server } from "socket.io";
import bodyParser from "body-parser";
import { fetchPizzy } from "./chefie";
const app = express();
const server = require("http").createServer(app);
const io = new Server(server, {
cors: {
origin: "*",
},
});
// Body-parser middleware for parsing JSON
app.use(bodyParser.json());
const cors = require('cors');
app.use(cors({
origin: '*'
}));
app.get("/api/pizza", (req, res) => {
fetchPizzy().then(pizzaList => {
console.log("Výsledek", pizzaList);
res.status(200).json(pizzaList);
});
});
app.post("/api/zprava", (req, res) => {
const { username, message } = req.body;
if (
typeof username !== "string" ||
typeof message !== "string" ||
username.length > 500 ||
message.length > 500
) {
return res.status(400).json({ error: "Invalid input" });
}
console.log(`posílám ${username} ${message} `);
io.emit("externalMessage", `${username} -> ${message}`);
res.status(200).json({ success: "Message sent" });
});
io.on("connection", (socket) => {
console.log(`New client connected: ${socket.id}`);
socket.on("message", (message) => {
io.emit("message", message);
});
socket.on("jduKafe", ({ username, timeString }) => {
console.log(`Received message: ${username}`);
socket.broadcast.emit("jduKafe", `${timeString}: ${username} -> jdu Kafe`);
});
socket.on("disconnect", () => {
console.log(`Client disconnected: ${socket.id}`);
});
});
const PORT = process.env.PORT || 3001;
server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});

8
server/tsconfig.json Normal file
View File

@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "ES2016",
"module": "CommonJS",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
}
}

1114
server/yarn.lock Normal file

File diff suppressed because it is too large Load Diff

39
src/Api.ts Normal file
View File

@ -0,0 +1,39 @@
type Pizza = {
name: string;
// TODO ingredience
sizes: [
size: number,
price: number,
];
}
const getBaseUrl = (): string => {
if (process.env.PUBLIC_URL) {
return process.env.PUBLIC_URL;
}
return 'http://localhost:3001';
}
async function request<TResponse>(
url: string,
config: RequestInit = {}
): Promise<TResponse> {
console.log("Calling fetch on", getBaseUrl() + url);
return fetch(getBaseUrl() + url, config).then(response => {
if (!response.ok) {
console.log("Response", response.status)
throw new Error(response.statusText);
}
console.log("response", response);
return response.json() as TResponse;
});
}
const api = {
get: <TResponse>(url: string) => request<TResponse>(url),
post: <TBody extends BodyInit, TResponse>(url: string, body: TBody) => request<TResponse>(url, { method: 'POST', body }),
}
export const getPizzy = async () => {
return await api.get<any>('/api/pizza');
}

View File

@ -1,9 +0,0 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import App from './App';
test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});

View File

@ -1,24 +1,24 @@
import React from 'react';
import logo from './logo.svg';
import React, { useEffect, useState } from 'react';
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Button } from 'react-bootstrap';
import { getPizzy } from './Api';
function App() {
const [pizzy, setPizzy] = useState();
useEffect(() => {
getPizzy().then(pizzy => {
setPizzy(pizzy);
});
}, []);
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
<div>
<Button onClick={async () => {
const pizzy = await getPizzy();
console.log("Výsledek", pizzy);
}}>Získat pizzy</Button>
</div>
);
}

9496
yarn.lock Normal file

File diff suppressed because it is too large Load Diff