Преглед изворни кода

Get LitK ingredients (no parsing, only strings)

Tati пре 7 година
родитељ
комит
e092c2d62e
2 измењених фајлова са 42 додато и 14 уклоњено
  1. 18 6
      ktchn/src/recipes/index.ts
  2. 24 8
      ktchn/src/recipes/scrape.ts

+ 18 - 6
ktchn/src/recipes/index.ts

@@ -3,11 +3,18 @@ interface QtyMetric {
   unit: string;
 }
 
-interface Ingredient {
+export interface Ingredient {
   name: string;
-  qtyMetric: QtyMetric;
-  qtyCups: number;
+  quantity: number;
+  unit: string;
+  state?: string;
   note: string;
+  _original: string;
+}
+
+export interface ComposedIngredients {
+  name: string;
+  ingredients: string[]; //Ingredient[]
 }
 
 interface Temperature {
@@ -25,7 +32,7 @@ export class RecipeDetails {
     prepTime:number|string = 0,
     cookingTime:number|string = 0,
     servings:number|string = 0) {
-      this.preparationTime = Number(prepTime);
+      this.preparationTime = Number( );
       this.cookingTime = Number(cookingTime);
       this.servings = Number(servings);
     }
@@ -34,12 +41,17 @@ export class RecipeDetails {
 export class Recipe {
   public name: string;
   tags!: string[];
-  ingredients!: Ingredient[];
+  ingredients!: ComposedIngredients[];
   details!: RecipeDetails;
   directions!: string[];
 
-  constructor(name:string, details?:RecipeDetails) {
+  constructor(
+    name: string,
+    details?: RecipeDetails,
+    ingredients?: ComposedIngredients[]
+  ) {
     this.name = name;
     this.details = details || new RecipeDetails();
+    this.ingredients = ingredients || [];
   }
 }

+ 24 - 8
ktchn/src/recipes/scrape.ts

@@ -1,11 +1,12 @@
 import RequestPromise from "request-promise";
 import Cheerio from "cheerio";
-import { Recipe, RecipeDetails } from './index';
+import { Recipe, RecipeDetails, Ingredient, ComposedIngredients } from './index';
 import { json } from "body-parser";
 import { stringify } from "querystring";
 import { UriOptions } from "request";
-import { any } from "bluebird";
+import { any, reduce } from "bluebird";
 import { Transform } from "stream";
+import { access } from "fs";
 
 type RegexMutator = (s: RegExpMatchArray) => number;
 
@@ -43,10 +44,25 @@ const LAURA_MAP: {
   }
 };
 
-function getIngredients($:CheerioSelector): Recipe {
-  let r = new Recipe('');
+function getIngredients($:CheerioSelector): ComposedIngredients[] {
   const ingredientsScrape = $('.cs-ingredients-check-list > ul').children();
-  return r;
+  const isNewIngredientList = (tagName: string): boolean => tagName === 'span';
+  
+  let ingredients: ComposedIngredients[] = [{
+    name: '',
+    ingredients: [],
+  }];
+
+  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: []});
+    } else {
+      list[last].ingredients = list[last].ingredients.concat(text);
+      return list;
+    }
+  }, ingredients);
 }
 
 function getRecipeDetails($:CheerioSelector):RecipeDetails {
@@ -75,10 +91,10 @@ function getRecipeName($:CheerioSelector): string {
 function lauraInTheKitchen($:CheerioSelector):Recipe {
   let recipe = new Recipe(
     getRecipeName($),
-    getRecipeDetails($)
+    getRecipeDetails($),
+    getIngredients($),
   );
-  console.log(recipe);
-  getIngredients($);
+  console.log(JSON.stringify(recipe));
   return recipe;
 }