forked from jgrahamc/barebones_inkplate
/
queryAndReplace.sh
executable file
·104 lines (76 loc) · 2.89 KB
/
queryAndReplace.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
set -e
RECIPE_TITLE='%%%RECIPE_TITLE%%%'
INGREDIENT_POINTER='%%%INGREDIENT_POINTER%%%'
METHOD_POINTER='%%%METHOD_POINTER%%%'
if [ -z "$SPOONACULAR_API" ]; then
echo "Please set the SPOONACULAR_API environment variable"
exit 1
fi
QUERY="$1"
if [ -z "$QUERY" ]; then
echo "Please provide a query"
exit 1
fi
recipe=$(curl "https://api.spoonacular.com/recipes/complexSearch?query=$QUERY&diet=vegetarian&addRecipeInformation=true&fillIngredients=true&number=1" -H "x-api-key: $SPOONACULAR_API" | jq '.results[0]')
if [ "$recipe" == "null" ]; then
echo "No recipe found for query: $QUERY"
echo $recipe
exit 1
fi
# recipe=$(cat ./oneQuery.json)
title=$(echo $recipe | jq -r '.title')
echo "[T] $title"
INGREDIENTS_LATEX="" # \ingredient{1 tbsp olive oil}
METHODS_LATEX="" # \method{Heat oil in a pan}
# Ingredient formatted like "2 cups of chickpeas (cooked, drained)"
for i in $(seq 1 $(echo $recipe | jq '.extendedIngredients | length')); do
# Get this ingredient's meta
rawMeta="$(echo $recipe | jq -r ".extendedIngredients[$i-1].meta")"
# loop through this ingredients "meta" array
meta="("
for j in $(seq 1 $(echo $rawMeta | jq '. | length')); do
m=$(echo $rawMeta | jq -r ".[$j-1]")
# If this is not the first meta, add a comma
if [ $j -gt 1 ]; then
meta="$meta, "
fi
# Append
meta="$meta$m"
done
meta="$meta)"
# Get this ingredient
ingredient=$(echo $recipe | jq -r ".extendedIngredients[$i-1].amount, .extendedIngredients[$i-1].unit, .extendedIngredients[$i-1].name" | tr '\n' ' ')
if [ "$meta" != "()" ]; then
ingredient="$ingredient $meta"
fi
echo "[I] $ingredient"
# Add to latex
INGREDIENTS_LATEX="$INGREDIENTS_LATEX\n\\ingredient{$ingredient}"
done
# Extract instructions
for i in $(seq 1 $(echo $recipe | jq '.analyzedInstructions[0].steps | length')); do
echo "[R] $(echo $recipe | jq -r ".analyzedInstructions[0].steps[$i-1].step")"
# Add to latex
METHODS_LATEX="$METHODS_LATEX\n\\method{$(echo $recipe | jq -r ".analyzedInstructions[0].steps[$i-1].step")}"
done
I_LATEX=$(echo -e "$INGREDIENTS_LATEX")
M_LATEX=$(echo -e "$METHODS_LATEX")
# Filter out any characters that will break latex
I_LATEX=$(echo "$I_LATEX" | sed -e 's/[^a-zA-Z0-9\.\,\(\)\{\}\-\_\n ]//g')
M_LATEX=$(echo "$M_LATEX" | sed -e 's/[^a-zA-Z0-9\.\,\(\)\{\}\-\_\n ]//g')
echo
echo "I_LATEX: $I_LATEX"
echo
echo "M_LATEX: $M_LATEX"
echo
# Escape the FIRST, LEADING backslash per line, that's it
I_LATEX=$(echo "$I_LATEX" | sed -e 's/\\/\\\\/g')
M_LATEX=$(echo "$M_LATEX" | sed -e 's/\\/\\\\/g')
# Handle newlines
I_LATEX=$(echo "$I_LATEX" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g')
M_LATEX=$(echo "$M_LATEX" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g')
# Replace pointers
sed -i "s/$RECIPE_TITLE/$title/g" /output/template.tex
sed -i "s/$INGREDIENT_POINTER/$I_LATEX/g" /output/template.tex
sed -i "s/$METHOD_POINTER/$M_LATEX/g" /output/template.tex