@jclind/ingredient-parser 1.0.0 → 1.0.1
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/index.js +33 -20
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -3,7 +3,7 @@ const dotenv = require('dotenv')
|
|
|
3
3
|
dotenv.config()
|
|
4
4
|
const { parse } = require('recipe-ingredient-parser-v3')
|
|
5
5
|
|
|
6
|
-
const ingredientParser = async ingrString => {
|
|
6
|
+
const ingredientParser = async (ingrString, spoonacularAPIKey) => {
|
|
7
7
|
const parsedIngr = parse(ingrString, 'eng')
|
|
8
8
|
const formattedIngr = parsedIngr.ingredient
|
|
9
9
|
.trim()
|
|
@@ -13,25 +13,23 @@ const ingredientParser = async ingrString => {
|
|
|
13
13
|
.replace(/^(fl )/, '')
|
|
14
14
|
.replace(/^(oz )/, '')
|
|
15
15
|
.trim()
|
|
16
|
-
console.log(formattedIngr)
|
|
17
|
-
console.log(parsedIngr)
|
|
18
16
|
if (formattedIngr) {
|
|
19
|
-
const ingrData = await getIngredientInfo(formattedIngr)
|
|
17
|
+
const ingrData = await getIngredientInfo(formattedIngr, spoonacularAPIKey)
|
|
20
18
|
return ingrData
|
|
21
19
|
} else {
|
|
22
20
|
return null
|
|
23
21
|
}
|
|
24
22
|
}
|
|
25
23
|
|
|
26
|
-
async function getIngredientInfo(ingrName) {
|
|
24
|
+
async function getIngredientInfo(ingrName, spoonacularAPIKey) {
|
|
27
25
|
const ingrNameLower = ingrName.toLowerCase()
|
|
28
|
-
console.log(ingrNameLower)
|
|
29
26
|
if (!ingrNameLower) return
|
|
30
27
|
const mongoIngrData = await checkIngredient(ingrNameLower)
|
|
31
|
-
console.log(mongoIngrData)
|
|
32
28
|
if (!mongoIngrData.data) {
|
|
33
|
-
const ingrData = await getSpoonacularIngrData(
|
|
34
|
-
|
|
29
|
+
const ingrData = await getSpoonacularIngrData(
|
|
30
|
+
ingrNameLower,
|
|
31
|
+
spoonacularAPIKey
|
|
32
|
+
)
|
|
35
33
|
return ingrData
|
|
36
34
|
} else {
|
|
37
35
|
return mongoIngrData.data
|
|
@@ -41,29 +39,44 @@ async function getIngredientInfo(ingrName) {
|
|
|
41
39
|
async function checkIngredient(name) {
|
|
42
40
|
return await mongoHttp.get(`checkIngredient?name=${name}`)
|
|
43
41
|
}
|
|
44
|
-
async function getSpoonacularIngrData(name) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
42
|
+
async function getSpoonacularIngrData(name, spoonacularAPIKey) {
|
|
43
|
+
let searchedIngr = null
|
|
44
|
+
try {
|
|
45
|
+
searchedIngr = await spoonacularHttp.get(
|
|
46
|
+
`search?query=${name}&number=1&apiKey=${spoonacularAPIKey}`
|
|
47
|
+
)
|
|
48
|
+
} catch (error) {
|
|
49
|
+
const res = error.response.data
|
|
50
|
+
if (res.code === 401) {
|
|
51
|
+
return { errorMsg: 'API Key Not Valid', error }
|
|
52
|
+
} else {
|
|
53
|
+
return { errorMsg: 'Error Occurred, Please Try Again', error }
|
|
54
|
+
}
|
|
55
|
+
}
|
|
50
56
|
|
|
51
57
|
if (
|
|
52
58
|
!searchedIngr.data ||
|
|
53
59
|
!searchedIngr.data.results ||
|
|
54
60
|
searchedIngr.data.results.length <= 0
|
|
55
61
|
) {
|
|
56
|
-
// console.log('no searchedIngr data')
|
|
57
62
|
return null
|
|
58
63
|
}
|
|
59
64
|
|
|
60
65
|
const ingrId = searchedIngr.data.results[0].id
|
|
61
66
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
67
|
+
let ingrData = null
|
|
68
|
+
try {
|
|
69
|
+
ingrData = await spoonacularHttp.get(
|
|
70
|
+
`${ingrId}/information?amount=1&apiKey=${process.env.SPOONACULAR_API_KEY}`
|
|
71
|
+
)
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (res.code === 401) {
|
|
74
|
+
return { errorMsg: 'API Key Not Valid', error }
|
|
75
|
+
} else {
|
|
76
|
+
return { errorMsg: 'Error Occurred, Please Try Again', error }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
65
79
|
const mongoDBIngrData = { ...ingrData.data, name }
|
|
66
|
-
// console.log(mongoDBIngrData)
|
|
67
80
|
setMongoDBIngrData(mongoDBIngrData)
|
|
68
81
|
return mongoDBIngrData
|
|
69
82
|
}
|
package/package.json
CHANGED