-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
400 lines (358 loc) · 14.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
var JSSoup = require('jssoup').default;
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
var fs = require('fs');
class Category {
constructor(name, url) {
this.name = name;
this.url = url;
}
getName() {
return this.name;
}
getUrl() {
return this.url;
}
toString() {
return this.name;
}
}
class Ingredient {
constructor(name, amount) {
this.name = name;
this.amount = amount;
}
getName() {
return this.name;
}
getAmount() {
return this.amount;
}
toString() {
return this.name;
}
}
class Tag {
constructor(name, url) {
this.name = name;
this.url = url;
}
getName() {
return this.name;
}
getUrl() {
return this.url;
}
toString() {
return this.name;
}
}
class Recipe {
constructor(name, url, ingredients, category, tags = []) {
this.name = name;
this.url = url;
this.ingredients = ingredients;
this.category = category;
this.tags = tags;
}
getName() {
return this.name;
}
getUrl() {
return this.url;
}
getIngredients() {
return this.ingredients;
}
getCategory() {
return this.category;
}
getTags() {
return this.tags;
}
toString() {
return this.name;
}
}
class ChefkochAPI {
constructor() {
this.baseURL = "https://www.chefkoch.de";
}
async getCategories() {
const response = await fetch(this.baseURL + "/rezepte/kategorien/");
const html = await response.text();
const soup = new JSSoup(html);
let categories = [];
soup.findAll("div", {"class": "category-column"}).forEach(category_column => {
category_column.findAll("a").forEach(category_link => {
if(category_link.attrs.href === "#" || category_link.attrs.href === "" || category_link.attrs.href == null) return;
categories.push(new Category(category_link.text, category_link.attrs.href));
});
});
return categories;
}
async getRecipes(category, endIndex = 5, startIndex = 0) {
let index = startIndex;
let recipes = [];
let tags = [];
while(index <= endIndex) {
category.url = category.url.replace("/s0/", `/s${index}/`);
const response = await fetch(this.baseURL + category.url);
const html = await response.text();
const soup = new JSSoup(html);
soup.findAll("div", {"class": "ds-recipe-card"}).forEach(async recipe_card => {
let recipeName = this.beautifyText(recipe_card.find("h3").text);
let recipeURL = recipe_card.find("a").attrs.href.split("#")[0];
let ingredient_list = [];
const response2 = await fetch(recipeURL);
const html2 = await response2.text();
const soup2 = new JSSoup(html2);
let ingredientTable = soup2.find("table", {"class": "ingredients"});
if(ingredientTable != null) {
ingredientTable = ingredientTable.find("tbody");
ingredientTable.findAll("tr").forEach(ingredient_row => {
let ingredient_name = ingredient_row.find("td", {"class": "td-right"}).find("span");
if(ingredient_name != null) {
if(ingredient_name.find("a") != null) {
ingredient_name = this.beautifyText(ingredient_name.find("a").text);
} else {
ingredient_name = this.beautifyText(ingredient_name.text);
}
} else {
ingredient_name = "No ingredient name found";
}
let ingredient_amount = ingredient_row.find("td", {"class": "td-left"}).find("span");
if(ingredient_amount != null) {
if(ingredient_amount.find("a") != null) {
ingredient_amount = this.beautifyText(ingredient_amount.find("a").text);
} else {
ingredient_amount = this.beautifyText(ingredient_amount.text);
}
} else {
ingredient_amount = "No ingredient amount found";
}
ingredient_list.push(new Ingredient(ingredient_name, ingredient_amount));
});
} else {
ingredient_list.push(new Ingredient("No ingredients found", "none"));
}
let tagElement = soup2.find("div", {"class": "recipe-tags"});
if(tagElement != null) {
tagElement.findAll("a").forEach(tagElement => {
tags.push(new Tag(tagElement.text, tagElement.attrs.href));
});
} else {
tags.push(new Tag("No tags found", "none"));
}
let recipe = new Recipe(recipeName, recipeURL, ingredient_list, category, tags);
recipes.push(recipe);
tags = [];
});
index++;
}
return recipes;
}
beautifyText(text) {
// remove newlines and tabs
text = text.replace(/(\r\n|\n|\r|\t)/gm, "");
// remove multiple whitespaces
text = text.replace(/\s\s+/g, ' ');
// remove whitespaces at the beginning and end of the string
text = text.trim();
return text;
}
async getAllRecipes(endIndex = 5, startIndex = 0) {
let recipes = [];
let categories = await this.getCategories();
for(let category of categories) {
let category_recipes = await this.getRecipes(category, endIndex, startIndex);
recipes.push(...category_recipes);
}
return recipes;
}
async searchRecipes(query, endIndex = 5, startIndex = 0) {
let index = startIndex;
let recipes = [];
let tags = [];
while(index <= endIndex) {
const response = await fetch(`${this.baseURL}/rs/s${index}/${query}/Rezepte.html`);
const html = await response.text();
const soup = new JSSoup(html);
soup.findAll("div", {"class": "ds-recipe-card"}).forEach(async recipe_card => {
let recipeName = this.beautifyText(recipe_card.find("h3").text);
let recipeURL = recipe_card.find("a").attrs.href.split("#")[0];
let ingredient_list = [];
const response2 = await fetch(recipeURL);
const html2 = await response2.text();
const soup2 = new JSSoup(html2);
let ingredientTable = soup2.find("table", {"class": "ingredients"});
if(ingredientTable != null) {
ingredientTable = ingredientTable.find("tbody");
ingredientTable.findAll("tr").forEach(ingredient_row => {
let ingredient_name = ingredient_row.find("td", {"class": "td-right"}).find("span");
if(ingredient_name != null) {
if(ingredient_name.find("a") != null) {
ingredient_name = this.beautifyText(ingredient_name.find("a").text);
} else {
ingredient_name = this.beautifyText(ingredient_name.text);
}
} else {
ingredient_name = "No ingredient name found";
}
let ingredient_amount = ingredient_row.find("td", {"class": "td-left"}).find("span");
if(ingredient_amount != null) {
if(ingredient_amount.find("a") != null) {
ingredient_amount = this.beautifyText(ingredient_amount.find("a").text);
} else {
ingredient_amount = this.beautifyText(ingredient_amount.text);
}
} else {
ingredient_amount = "No ingredient amount found";
}
ingredient_list.push(new Ingredient(ingredient_name, ingredient_amount));
});
} else {
ingredient_list.push(new Ingredient("No ingredients found", "none"));
}
let category = null;
let categoryURL = soup2.find("ol", {"class": "ds-col-12"});
if(categoryURL != null) {
// 4th element is the category
categoryURL = categoryURL.findAll("li")[3].find("a").attrs.href;
category = await this.getCategory(categoryURL);
}
let tagElement = soup2.find("div", {"class": "recipe-tags"});
if(tagElement != null) {
tagElement.findAll("a").forEach(tagElement => {
tags.push(new Tag(tagElement.text, tagElement.attrs.href));
});
} else {
tags.push(new Tag("No tags found", "none"));
}
let recipe = new Recipe(recipeName, recipeURL, ingredient_list, category, tags);
recipes.push(recipe);
tags = [];
});
index++;
}
return recipes;
}
async getRecipe(recipeSubURL) {
const response = await fetch(this.baseURL + recipeSubURL);
const html = await response.text();
const soup = new JSSoup(html);
let recipeName = soup.find("h1").text;
let tags = [];
let ingredient_list = [];
const ingredientTable = soup.find("table", {"class": "ingredients"});
if(ingredientTable != null) {
ingredientTable.findAll("tr").forEach(ingredient_row => {
let ingredient_name = this.beautifyText(ingredient_row.find("td", {"class": "td-right"}).text);
let ingredient_amount = this.beautifyText(ingredient_row.find("td", {"class": "td-left"}).text);
ingredient_list.push(new Ingredient(ingredient_name, ingredient_amount));
});
}
let category = null;
let categoryURL = soup.find("ol", {"class": "ds-col-12"});
if(categoryURL != null) {
// 4th element is the category
categoryURL = categoryURL.findAll("li")[3].find("a").attrs.href;
category = await this.getCategory(categoryURL);
}
let tagElement = soup.find("div", {"class": "recipe-tags"});
if(tagElement != null) {
tagElement.findAll("a").forEach(tagElement => {
tags.push(new Tag(tagElement.text, tagElement.attrs.href));
});
} else {
tags.push(new Tag("No tags found", "none"));
}
let recipe = new Recipe(recipeName, recipeSubURL, ingredient_list, category, tags);
return recipe;
}
async getCategory(categorySubURL) {
const response = await fetch(this.baseURL + categorySubURL);
const html = await response.text();
const soup = new JSSoup(html);
let categoryName = this.beautifyText(soup.find("h1").text).split(" Rezepte")[0];
let category = new Category(categoryName, categorySubURL);
return category;
}
}
class DataParser {
async writeFile(fileName, data) {
fs.writeFile(fileName, data, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
}
async readFile(fileName) {
return new Promise((resolve, reject) => {
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) reject(err);
resolve(data);
});
});
}
async writeRecipesToJson(recipes, fileName) {
let json = JSON.stringify(recipes);
await this.writeFile(fileName, json);
}
async writeCategoriesToJson(categories, fileName) {
let json = JSON.stringify(categories);
await this.writeFile(fileName, json);
}
async writeRecipesToCSV(recipes, fileName) {
let csv = "";
for(let recipe of recipes) {
csv += recipe.getName() + "," + recipe.getUrl() + "," + recipe.getCategory() + "\n";
}
await this.writeFile(fileName, csv);
}
async writeCategoriesToCSV(categories, fileName) {
let csv = "";
for(let category of categories) {
csv += category.getName() + "," + category.getUrl() + "\n";
}
await this.writeFile(fileName, csv);
}
async loadRecipesFromJson(fileName) {
let json = await this.readFile(fileName).then((data) => {return data}).catch((err) => {console.log(err)});
let recipes = JSON.parse(json);
return recipes;
}
async loadCategoriesFromJson(fileName) {
let json = await this.readFile(fileName).then((data) => {return data}).catch((err) => {console.log(err)});
let categories = JSON.parse(json);
return categories;
}
async loadRecipesFromCSV(fileName) {
let csv = await this.readFile(fileName).then((data) => {return data}).catch((err) => {console.log(err)});
let recipes = [];
let lines = csv.split("\n");
for(let line of lines) {
let data = line.split(",");
let recipe = new Recipe(data[0], data[1], data[2]);
recipes.push(recipe);
}
return recipes;
}
async loadCategoriesFromCSV(fileName) {
let csv = await this.readFile(fileName).then((data) => {return data}).catch((err) => {console.log(err)});
let categories = [];
let lines = csv.split("\n");
for(let line of lines) {
let data = line.split(",");
let category = new Category(data[0], data[1]);
categories.push(category);
}
return categories;
}
}
var chefkochAPI = new ChefkochAPI();
module.exports.chefkochAPI = chefkochAPI;
module.exports.DataParser = DataParser;
module.exports.Tag = Tag;
module.exports.Recipe = Recipe;
module.exports.Category = Category;
module.exports.Ingredient = Ingredient;
module.exports.ChefkochAPI = ChefkochAPI;