forked from i4c0ni99/FoodAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreqest.py
More file actions
98 lines (70 loc) · 2.96 KB
/
reqest.py
File metadata and controls
98 lines (70 loc) · 2.96 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
import json
import time
from fatsecret import Fatsecret
consumer_key = "ae8ad889881b4aa49998a8d293c99f7c"
consumer_secret = "64d1a26c5afb48c3a2c7bc06be526d33"
# Sostituisci questi con le tue chiavi API di FatSecret
# Crea un'istanza dell'API di FatSecret
fs = Fatsecret(consumer_key, consumer_secret)
category=['Beans & Legumes','Beverages','Breads & Cereals','Cheese',' Milk & Dairy','Eggs','Fish & Seafood','Fruit','Meat','Nuts & Seeds','Pasta', 'Rice & Noodles','Salads', 'Sauces', 'Spices & Spreads','Snacks','Soups','Sweets', 'Candy & Desserts','Vegetables','Other']
all_foods = []
for food_category in category :
pagenuber = 0
print(food_category)
while pagenuber <=100:
food_data = fs.foods_search(food_category,page_number=pagenuber,max_results=50,region='IT',language='it')
print(pagenuber)
if food_data :
print(food_data[2])
all_foods.append(food_data)
time.sleep(0.5)
pagenuber +=1# Aggiungi un ritardo per evitare di fare troppe richieste in poco tempo
# Salva tutti i dati raccolti in un file JSON
with open('foods_data.json', 'w') as json_file:
json.dump(all_foods, json_file, indent=4)
print("Dati degli alimenti salvati in 'foods_data.json'")
"""
if food_data:
all_foods.append(food_data)
time.sleep(1) # Aggiungi un ritardo per evitare di fare troppe richieste in poco tempo
# Salva tutti i dati raccolti in un file JSON
with open('foods_data.json', 'w') as json_file:
json.dump(all_foods, json_file, indent=4)
print("Dati degli alimenti salvati in 'foods_data.json'")
"""
# Funzione per cercare i cibi e paginare i risultati
"""def search_all_foods():
food_id = 1641
global all_foods
while food_id <= 1700:
print('food_id =',food_id)
try:
foods = fs.food_get(food_id=food_id)
# Verifica se la risposta contiene cibi
all_foods.append(foods)
except KeyError as e:
print(f"Errore chiave non trovata: {e}")
food_id += 1
except Exception as e:
print(f"Errore durante la richiesta: {e}")
food_id += 1
# Scarica tutti i cibi
all_foods=search_all_foods()
# Stampa il numero totale di cibi scaricati
# Salva i risultati in un file JSON
print(all_foods)
import json
with open('all_foods.json', 'w') as f:
json.dump(all_foods, f, indent=4)
"""
"""
import requests
response = requests.get("https://api.spoonacular.com/food/search?query=ingredients&number=600000&apiKey=475c377d00914f7f84740521a327a178&includeNutrition=true%22")
if response.status_code == 200:
data = response.json() # Ottenere i dati in formato JSON
import json
with open('def.json', 'w') as f:
json.dump(data, f, indent=4)
print(data)
else:
print(f'Error: {response.status_code}') """