Explorar o código

Include recipe-ingredient-parser to give format to recipe's ingredients

Tati %!s(int64=7) %!d(string=hai) anos
pai
achega
6d113c08f6
Modificáronse 3 ficheiros con 23 adicións e 16 borrados
  1. 6 0
      ktchn/package.json
  2. 3 3
      ktchn/src/recipes/index.ts
  3. 14 13
      ktchn/src/recipes/scrape.ts

+ 6 - 0
ktchn/package.json

@@ -9,17 +9,23 @@
     "build:live": "nodemon --watch 'src/**/*.ts' --exec ts-node src/server.ts"
   },
   "dependencies": {
+    "@types/chai": "^4.1.7",
     "@types/cheerio": "^0.22.9",
     "@types/cookie-parser": "^1.4.1",
     "@types/express": "^4.16.0",
+    "@types/jest": "^23.3.12",
+    "@types/natural": "^0.2.33",
     "@types/request": "^2.48.0",
     "@types/request-promise": "^4.1.42",
+    "chai": "^4.2.0",
     "cheerio": "^1.0.0-rc.2",
     "cookie-parser": "~1.4.3",
     "debug": "~2.6.9",
     "express": "~4.16.0",
     "morgan": "~1.9.0",
     "nano": "^7.1.0",
+    "natural": "^0.6.3",
+    "recipe-ingredient-parser": "file:../../recipe-parser",
     "request": "^2.88.0",
     "request-promise": "^4.2.2"
   },

+ 3 - 3
ktchn/src/recipes/index.ts

@@ -8,13 +8,13 @@ export interface Ingredient {
   quantity: number;
   unit: string;
   state?: string;
-  note: string;
-  _original: string;
+  note?: string;
+  _original?: string;
 }
 
 export interface ComposedIngredients {
   name: string;
-  ingredients: string[]; //Ingredient[]
+  ingredients: Ingredient[];
 }
 
 interface Temperature {

+ 14 - 13
ktchn/src/recipes/scrape.ts

@@ -1,12 +1,7 @@
 import RequestPromise from "request-promise";
 import Cheerio from "cheerio";
 import { Recipe, RecipeDetails, Ingredient, ComposedIngredients } from './index';
-import { json } from "body-parser";
-import { stringify } from "querystring";
-import { UriOptions } from "request";
-import { any, reduce } from "bluebird";
-import { Transform } from "stream";
-import { access } from "fs";
+import { parse } from "recipe-ingredient-parser";
 
 type RegexMutator = (s: RegExpMatchArray) => number;
 
@@ -44,22 +39,29 @@ const LAURA_MAP: {
   }
 };
 
+function parseIngredients(rawIngredient: string): Ingredient {
+  let parsed = parse(rawIngredient);
+  return {
+    name: parsed.ingredient,
+    quantity: Number(parsed.quantity) || 0,
+    unit: parsed.unit || '',
+    _original: rawIngredient,
+  };
+}
+
 function getIngredients($:CheerioSelector): ComposedIngredients[] {
   const ingredientsScrape = $('.cs-ingredients-check-list > ul').children();
   const isNewIngredientList = (tagName: string): boolean => tagName === 'span';
   
-  let ingredients: ComposedIngredients[] = [{
-    name: '',
-    ingredients: [],
-  }];
+  let ingredients: ComposedIngredients[] = [];
 
   return ingredientsScrape.toArray().reduce((list, rawIngredient)=>{
     let last = list.length - 1;
     let text = $(rawIngredient).text();
     if (isNewIngredientList(rawIngredient.tagName)) {
-      return list.concat({name: text, ingredients: []});
+      return list.concat([{name: text, ingredients: []}]);
     } else {
-      list[last].ingredients = list[last].ingredients.concat(text);
+      list[last].ingredients = list[last].ingredients.concat([parseIngredients(text)]);
       return list;
     }
   }, ingredients);
@@ -94,7 +96,6 @@ function lauraInTheKitchen($:CheerioSelector):Recipe {
     getRecipeDetails($),
     getIngredients($),
   );
-  console.log(JSON.stringify(recipe));
   return recipe;
 }