-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchproject.py
85 lines (71 loc) · 3.17 KB
/
searchproject.py
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
import requests
import config
import math
# Register to get an APP ID and key https://developer.edamam.com/
app_id = config.application_id
app_key = config.application_key
includeAppId = "app_id={}".format(app_id)
includeAppKey = "app_key={}".format(app_key)
startPagination = "0"
endPagination = "10"
CuisineType_array = {"American", "British", "Caribbean", "Chinese", "French", "Italian", "Japanese", "Kosher",
"Mediterranean", "Mexican"}
# ask user to enter ingredient(s)
inputIngredient = input("Please enter one or more ingredients to search for: ")
# return invalid response if user enters nothing or only spaces
while inputIngredient == "" or inputIngredient.isspace():
inputIngredient = input("Invalid Response. Please enter at least one or more ingredients. Try again: ")
# prints out choose ingredient/s
# print("You have chosen these ingredients: " + inputIngredient)
# ask user to enter cuisine preference
inputCuisineType = input(
f"Please enter choose a preferred cuisine from the following options below - \n{CuisineType_array}: ")
while inputCuisineType.capitalize() not in CuisineType_array:
inputCuisineType = input(
f"Invalid Response. Please enter choose a preferred cuisine from the following options below or type 'N' if none - \n{CuisineType_array}: ")
# print("You have chosen: " + inputCuisineType)
print("----")
print(f'You have searched for {inputCuisineType} recipes using {inputIngredient}')
with open('recipes.txt', 'w') as f:
rangeURL = f"https://api.edamam.com/search?q={inputIngredient}&cuisineType={inputCuisineType}&{includeAppId}&{includeAppKey}&from={startPagination}&to={endPagination}"
rangeR = requests.get(rangeURL)
if rangeR.status_code == 200:
print("----")
print("Your request was successful")
else:
print("----")
print("Error: " + str(rangeR))
data = rangeR.json()
rangeCount = int(math.ceil(data['count'] / 10))
# print(rangeCount)
print("----")
print(f"{data['count']} recipes found")
for i in range(1, rangeCount):
print("----")
endPagination = i * 10
startPagination = endPagination - 10
url = f"https://api.edamam.com/search?q={inputIngredient}&cuisineType={inputCuisineType}&{includeAppId}&{includeAppKey}&from={startPagination}&to={endPagination}"
print(f"Showing recipe results from {startPagination} to {endPagination}")
r = requests.get(url)
data = r.json()
results = data['hits']
# count = data['count']
more = data['more']
for result in results:
recipe = result['recipe']
print("----")
print(recipe['label'])
print(recipe['url'])
f.write('%s\n' % recipe['label'])
f.write('%s\n' % recipe['url'])
if not more:
print("----")
print("That's all the recipes!")
break
print("----")
moreRecipes = input("Do you want ten more recipes? Yes/No ")
if moreRecipes.capitalize() == "No":
print("----")
print("Your search results have now been saved in a txt doc")
if moreRecipes.capitalize() != "Yes":
break