UNPKG

migros-api-wrapper

Version:

Making the api of migros more accessible to the public.

87 lines 3.6 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRecipeInfos = void 0; const cheerio = __importStar(require("cheerio")); const requests_1 = require("../../utils/requests"); const apiPaths_1 = require("../apiPaths"); const Language_1 = require("../enums/Language"); const url = apiPaths_1.migrosApiPaths.migusto.recipeDetails; const defaultRecipeInfosOptions = { slug: "", language: Language_1.Language.DE, }; async function getRecipeInfosRequest(url, options) { const necessaryHeaders = { ["Accept"]: "*/*", }; url += `/${options.language}/${options.language === Language_1.Language.IT ? "ricette" : options.language === Language_1.Language.FR ? "recettes" : "rezepte"}/${options.slug}`; const response = await (0, requests_1.getRequest)(url, {}, necessaryHeaders); const $ = cheerio.load(await response.text()); const header = $("[class='r-header']").first(); let id = ""; if (header.children() && header.children()[0]) { const headerData = header .children()[0] .attributes.find((h) => h.name === "data-js-options"); if (headerData === null || headerData === void 0 ? void 0 : headerData.value) { const headerJSON = JSON.parse(headerData.value); id = headerJSON["url"].split("?ratingId=")[1]; } } const recipeData = $('[type="application/ld+json"]') .toArray() .map((i) => { return JSON.parse($(i).text()); }) .find((x) => x["@type"] === "Recipe"); return { id: id, name: recipeData.name, keywords: recipeData.keywords, author: recipeData.author, aggregateRating: { ratingValue: recipeData.aggregateRating.ratingValue, reviewCount: recipeData.aggregateRating.reviewCount, }, images: recipeData.image, totalTime: recipeData.totalTime, recipeCategory: recipeData.recipeCategory, recipeCuisine: recipeData.recipeCuisine, recipeInstructions: recipeData.recipeInstructions.map((x) => x.text), recipeYield: recipeData.recipeYield, recipeIngredients: recipeData.recipeIngredient, datePublished: new Date(recipeData.datePublished), }; } async function getRecipeInfos(recipeDetailOptions) { recipeDetailOptions = { ...defaultRecipeInfosOptions, ...recipeDetailOptions, }; return getRecipeInfosRequest(url, recipeDetailOptions); } exports.getRecipeInfos = getRecipeInfos; //# sourceMappingURL=recipe-info.js.map