@jclind/ingredient-parser 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
Binary file
package/http-common.ts ADDED
@@ -0,0 +1,16 @@
1
+ const axios = require('axios')
2
+
3
+ const mongoHttp = axios.create({
4
+ baseURL:
5
+ 'https://us-east-1.aws.data.mongodb-api.com/app/prepify-ixumn/endpoint',
6
+ headers: {
7
+ 'Content-type': 'application/json',
8
+ },
9
+ })
10
+ const spoonacularHttp = axios.create({
11
+ baseURL: 'https://api.spoonacular.com/food/ingredients/',
12
+ headers: {
13
+ 'Content-type': 'application/json',
14
+ },
15
+ })
16
+ module.exports = { mongoHttp, spoonacularHttp }
package/index.js ADDED
@@ -0,0 +1,76 @@
1
+ const { mongoHttp, spoonacularHttp } = require('./http-common.ts')
2
+ const dotenv = require('dotenv')
3
+ dotenv.config()
4
+ const { parse } = require('recipe-ingredient-parser-v3')
5
+
6
+ const ingredientParser = async ingrString => {
7
+ const parsedIngr = parse(ingrString, 'eng')
8
+ const formattedIngr = parsedIngr.ingredient
9
+ .trim()
10
+ .replace(/\s{2,}/g, ' ')
11
+ .replace(/,/g, '')
12
+ .replace(/^(fluid )/, '')
13
+ .replace(/^(fl )/, '')
14
+ .replace(/^(oz )/, '')
15
+ .trim()
16
+ console.log(formattedIngr)
17
+ console.log(parsedIngr)
18
+ if (formattedIngr) {
19
+ const ingrData = await getIngredientInfo(formattedIngr)
20
+ return ingrData
21
+ } else {
22
+ return null
23
+ }
24
+ }
25
+
26
+ async function getIngredientInfo(ingrName) {
27
+ const ingrNameLower = ingrName.toLowerCase()
28
+ console.log(ingrNameLower)
29
+ if (!ingrNameLower) return
30
+ const mongoIngrData = await checkIngredient(ingrNameLower)
31
+ console.log(mongoIngrData)
32
+ if (!mongoIngrData.data) {
33
+ const ingrData = await getSpoonacularIngrData(ingrNameLower)
34
+ console.log(ingrData, 'test')
35
+ return ingrData
36
+ } else {
37
+ return mongoIngrData.data
38
+ }
39
+ }
40
+
41
+ async function checkIngredient(name) {
42
+ return await mongoHttp.get(`checkIngredient?name=${name}`)
43
+ }
44
+ async function getSpoonacularIngrData(name) {
45
+ // console.log(process.env.SPOONACULAR_API_KEY)
46
+ const searchedIngr = await spoonacularHttp.get(
47
+ `search?query=${name}&number=1&apiKey=${process.env.SPOONACULAR_API_KEY}`
48
+ )
49
+ console.log(searchedIngr, 'test2')
50
+
51
+ if (
52
+ !searchedIngr.data ||
53
+ !searchedIngr.data.results ||
54
+ searchedIngr.data.results.length <= 0
55
+ ) {
56
+ // console.log('no searchedIngr data')
57
+ return null
58
+ }
59
+
60
+ const ingrId = searchedIngr.data.results[0].id
61
+
62
+ const ingrData = await spoonacularHttp.get(
63
+ `${ingrId}/information?amount=1&apiKey=${process.env.SPOONACULAR_API_KEY}`
64
+ )
65
+ const mongoDBIngrData = { ...ingrData.data, name }
66
+ // console.log(mongoDBIngrData)
67
+ setMongoDBIngrData(mongoDBIngrData)
68
+ return mongoDBIngrData
69
+ }
70
+
71
+ async function setMongoDBIngrData(data) {
72
+ await mongoHttp.post(`addIngredient`, data)
73
+ return data
74
+ }
75
+
76
+ module.exports = ingredientParser
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@jclind/ingredient-parser",
3
+ "version": "1.0.0",
4
+ "description": "Parses given sentence including ingredient information and attemps to return quantity, measurement and ingredient",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/jclind/ingredient-parser.git"
12
+ },
13
+ "author": "Jesse Lind",
14
+ "license": "ISC",
15
+ "bugs": {
16
+ "url": "https://github.com/jclind/ingredient-parser/issues"
17
+ },
18
+ "homepage": "https://github.com/jclind/ingredient-parser#readme",
19
+ "dependencies": {
20
+ "dotenv": "^16.0.3",
21
+ "parse-ingredient": "^0.6.0",
22
+ "recipe-ingredient-parser-v3": "^1.2.18"
23
+ }
24
+ }