Skip to content

Commit

Permalink
split to api and lib
Browse files Browse the repository at this point in the history
  • Loading branch information
philippdormann committed Nov 1, 2022
1 parent d9055b7 commit 02901b1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 51 deletions.
55 changes: 4 additions & 51 deletions api.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const axios = require('axios');
const Fastify = require('fastify')
const mercurius = require('mercurius')
const cheerio = require('cheerio');
const sources = require("./inetmenue-sources.json")

const mercurius = require('mercurius');
const { getFoodItems } = require('./lib');
const app = Fastify()

const schema = `
Expand All @@ -23,56 +20,12 @@ const schema = `
allergeneInfo: [Allergen]
}
type Query {
food(kw: String, source: String): [FoodItem] @auth(requires: USER)
food(kw: String, source: String): [FoodItem]
}
`;
const resolvers = {
Query: {
food: async (_, { kw = "", source = "" }) => {
if (!kw) {
return [];
}
if (!sources.includes(source)) {
return [];
}
const data = (await axios.request({
method: 'GET',
url: `https://${source}.inetmenue.de/fs/menu/week/${kw}`,
headers: {
Accept: '*/*'
}
})).data.replace(/>\s+</g, '><');
//
const out = [];
const $ = cheerio.load(data);
const days = [];
const daykeys = ["mo", "di", "mi", "do", "fr", "sa", "so"];
$(".day-header a.day").each((index, element) => {
const dayShort = $(element).find(".short").html()?.replace("\n ", "")?.replace("<br><small>", ", ")?.replace("</small>", "");
const dayLong = $(element).find(".long").html()?.replace("\n ", "")?.replace("<br><small>", ", ")?.replace("</small>", "");
days.push({ dayLong, dayShort })
});
let c = 0;
$("article.menu").each((index, element) => {
const category = $(element).find("header i").attr("title")
const image = $(element).find("div.product div.image").css("background-image")?.replace("url(", "")?.replace(")", "");
const title = $(element).find("h4").text()?.replace("\n ", "")?.replace(" ", "");
const description = $(element).find("p.description").text()?.replace("\n ", "")?.replace(" ", "");
const price = $(element).find("span.price").text();
const allergens = $(element).find("span.allergens").text()?.replace("\n ", "",)?.replace(" ", "").split(",").filter(a => a.trim());
const allergensLong = $(element).find("span.allergens").attr("title")?.replace("\n ", "",)?.replace(" ", "").split(", ").filter(a => a.trim());
const day = days[c];
const allergeneInfo = [];
let cc = 0
allergens.forEach(a => {
allergeneInfo.push({ short: a, long: allergensLong[cc] });
cc++;
});
out.push({ category, image, title, description, price, allergeneInfo, dayKey: daykeys[c], dayLong: day?.dayLong, dayShort: day?.dayShort });
c++;
});
return out
},
food: getFoodItems,
}
}

Expand Down
49 changes: 49 additions & 0 deletions lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const axios = require('axios');
const cheerio = require('cheerio');
const sources = require("./inetmenue-sources.json")
const getFoodItems = async (_, { kw = "", source = "" }) => {
if (!kw) {
return [];
}
if (!sources.includes(source)) {
return [];
}
const data = (await axios.request({
method: 'GET',
url: `https://${source}.inetmenue.de/fs/menu/week/${kw}`,
headers: {
Accept: '*/*'
}
})).data.replace(/>\s+</g, '><');
//
const out = [];
const $ = cheerio.load(data);
const days = [];
const daykeys = ["mo", "di", "mi", "do", "fr", "sa", "so"];
$(".day-header a.day").each((index, element) => {
const dayShort = $(element).find(".short").html()?.replace("\n ", "")?.replace("<br><small>", ", ")?.replace("</small>", "");
const dayLong = $(element).find(".long").html()?.replace("\n ", "")?.replace("<br><small>", ", ")?.replace("</small>", "");
days.push({ dayLong, dayShort })
});
let c = 0;
$("article.menu").each((index, element) => {
const category = $(element).find("header i").attr("title")
const image = $(element).find("div.product div.image").css("background-image")?.replace("url(", "")?.replace(")", "");
const title = $(element).find("h4").text()?.replace("\n ", "")?.replace(" ", "");
const description = $(element).find("p.description").text()?.replace("\n ", "")?.replace(" ", "");
const price = $(element).find("span.price").text();
const allergens = $(element).find("span.allergens").text()?.replace("\n ", "",)?.replace(" ", "").split(",").filter(a => a.trim());
const allergensLong = $(element).find("span.allergens").attr("title")?.replace("\n ", "",)?.replace(" ", "").split(", ").filter(a => a.trim());
const day = days[c];
const allergeneInfo = [];
let cc = 0
allergens.forEach(a => {
allergeneInfo.push({ short: a, long: allergensLong[cc] });
cc++;
});
out.push({ category, image, title, description, price, allergeneInfo, dayKey: daykeys[c], dayLong: day?.dayLong, dayShort: day?.dayShort });
c++;
});
return out
}
exports.getFoodItems = getFoodItems
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { getFoodItems } = require('./lib');
getFoodItems(undefined, { kw: "2022W45", source: "schmecktsdannbassts" }).then((foodItems) => {
console.log(foodItems);
})

0 comments on commit 02901b1

Please sign in to comment.