Ukládání dat výhradně do DB

This commit is contained in:
2023-08-06 21:45:27 +02:00
parent 18cb172e06
commit c4b14bdf6b
7 changed files with 85 additions and 37 deletions

View File

@@ -1,9 +1,5 @@
import axios from "axios";
import os from "os";
import path from "path";
import fs from "fs";
import { load } from 'cheerio';
import { formatDate } from "./utils";
import { Food } from "../../types";
// Fráze v názvech jídel, které naznačují že se jedná o polévku
@@ -51,22 +47,15 @@ const getDayOfWeekIndex = (date: Date) => {
}
/**
* Stáhne (v případě potřeby) a vrátí HTML z dané URL pro předané datum.
* Pokud je pro dané datum již staženo, vrátí jeho obsah ze souboru.
* Stáhne a vrátí aktuální HTML z dané URL.
*
* @param url URL pro stažení
* @param prefix prefix pro uložení do souboru
* @param date datum ke kterému stáhnout HTML
* @returns stažené HTML, nebo HTML ze souborové cache
* @returns stažené HTML
*/
const getHtml = async (url: string, prefix: string, date: Date): Promise<Buffer> => {
const fileName = path.join(os.tmpdir(), `${prefix}_${formatDate(date)}.html`);
if (!fs.existsSync(fileName)) {
await axios.get(url).then(res => res.data).then(content => {
fs.writeFileSync(fileName, content);
});
}
return fs.readFileSync(fileName);
const getHtml = async (url: string): Promise<any> => {
await axios.get(url).then(res => res.data).then(content => {
return content
});
}
/**
@@ -109,7 +98,7 @@ export const getMenuSladovnicka = async (date: Date = new Date(), mock: boolean
if (todayDayIndex == 5 || todayDayIndex == 6) { // Víkend
return [];
}
const html = await getHtml(SLADOVNICKA_URL, 'sladovnicka', date);
const html = await getHtml(SLADOVNICKA_URL);
const $ = load(html);
// Najdeme index pro vstupní datum (např. při svátcích bude posunutý)
// TODO validovat, že vstupní datum je v aktuálním týdnu
@@ -230,7 +219,7 @@ export const getMenuUMotliku = async (date: Date = new Date(), mock: boolean = f
if (todayDayIndex == 5 || todayDayIndex == 6) { // Víkend
return [];
}
const html = await getHtml(U_MOTLIKU_URL, 'umotliku', date);
const html = await getHtml(U_MOTLIKU_URL);
const $ = load(html);
const table = $('table.table.table-hover.Xtable-striped').first();
const body = table.children().first();
@@ -313,7 +302,7 @@ export const getMenuTechTower = async (date: Date = new Date(), mock: boolean =
if (todayDayIndex == 5 || todayDayIndex == 6) { // Víkend
return [];
}
const html = await getHtml(TECHTOWER_URL, 'techtower', date);
const html = await getHtml(TECHTOWER_URL);
const $ = load(html);
const fonts = $('font.wsw-41');
let font = undefined;