-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoodAPI.py
More file actions
115 lines (84 loc) · 3.46 KB
/
foodAPI.py
File metadata and controls
115 lines (84 loc) · 3.46 KB
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
105
106
107
108
109
110
111
112
113
114
115
import requests
from gtts import gTTS
import os
class FoodAPI():
def __init__(self, userInput: str):
self.foundMeal = False
for char in userInput: # replace space with _
if char == ' ':
char = '_'
ingredient = "filter.php?i=" + userInput
self.getAllMeals(ingredient)
def __str__(self):
if self.foundMeal == True:
result = ""
result += f"Meal: {self.meal_name}\n"
result += f"Category: {self.meal_category}\n"
result += f"Area: {self.meal_area}\n"
result += f"Ingredients: \n"
for ingredients in self.meal_ingredient:
result += ingredients
result += "\n"
result += f"Instructions: {self.meal_instructions}\n"
result += f"Image URL: {self.meal_image}\n"
return result
else:
return "No meals found. Try again"
def getAllMeals(self, ingredient: str):
url = "https://www.themealdb.com/api/json/v1/1/" + ingredient # searches the web with specific ingredient
response = requests.get(url)
data = response.json()
if data['meals'] != None: # add all meals and id's in a list
meal = data['meals'][0]
self.meal_name = []
self.meal_id = []
for meal in data['meals']:
self.meal_name.append(meal['strMeal'])
self.meal_id.append(meal['idMeal'])
count = 0 # print all recipes
for food in self.meal_name:
foodName = str(count) + ". " + food
print(foodName)
count += 1
while (self.foundMeal == False):
recipe = input("Please choose the number for the recipe you want to see. \n-->")
if int(recipe) < len(self.meal_id):
foodID = self.meal_id[int(recipe)]
self.getMeal(foodID) # get the specific meal that the user chose
self.foundMeal = True
else:
print("No recipe found. Try again.")
else:
self.foundMeal = False
return
def getMeal(self, foodID: str):
url1 = "https://www.themealdb.com/api/json/v1/1/lookup.php?i=" + foodID
response = requests.get(url1)
data = response.json()
meal = data['meals'][0]
self.meal_name = meal['strMeal']
self.meal_category = meal['strCategory']
self.meal_area = meal['strArea']
self.meal_instructions = meal['strInstructions']
self.meal_image = meal['strMealThumb']
self.meal_ingredient = []
for i in range(1, 21):
ingredient = meal[f'strIngredient{i}']
if ingredient and ingredient.strip() != "":
self.meal_ingredient.append(ingredient)
def getInstructions(self):
return f"Instructions: {self.meal_instructions}\n"
def getFoundMeal(self):
return self.foundMeal
def textToSpeech(food: FoodAPI, foodInstruction: str):
if food.getFoundMeal() == True:
language = 'en'
myobj = gTTS(text = str(foodInstruction), lang = language, slow = False)
myobj.save("foodInstruction.mp3")
os.system("open FoodInstruction.mp3")
else:
print("Instructions cannot be played")
userInput = input("What ingredient do you have?\n-->")
food = FoodAPI(userInput)
print(food)
textToSpeech(food, food.getInstructions())