Monday, February 10, 2020

IslandT: Create a project which shows the nutrition and diet data for generic foods, packaged foods, and restaurant meals using python

Hello, nice to be back again, are you people ready for the next python project? In this latest project which will take maybe around half a month to complete, I will develop a python application that will show the nutrition and diet data for generic foods, packaged foods, and restaurant meals to the user. This application will use one of the free APIs from Rapid API to receive all the data that this project ever needs.

First of all, if you have not yet signed up for a free account at Rapid API, then just go ahead and do so. This site offers both the paid API and free API which the application developer can use in his or her own project. After you have signed up and signed into your account, search for this API: Edamam Food and Grocery Database.

About the food API:

Edamam provides access to a food and grocery database with over 750,000 basic foods, restaurant items, and consumer packaged foods – The foods in the Food API can be filtered by Diet and health filters generated by Edamam. All food database data is enriched with diet, allergy and health labeling, as calculated by Edamam based on the food’s ingredients. Peanut Free, Shellfish Free, Gluten-Free, Vegan and Vegetarian are some of the 20+ claims generated automatically. – For basic foods from the food database (flour, eggs, flour, etc.), Edamam returns data for calories, fats, carbohydrates, protein, cholesterol, sodium for a total of 28 nutrients. For UPC foods and fast foods data is return as listed on their nutrition label UPC or Barcode search – The Food Database API provides access to over 550,000 unique UPC codes. Low-cost solution – Edamam provides free Food API access with its basic plan for developers, startups and non-profits alike. – Enterprise customers are charged a very low monthly and per call fee based on usage. Custom packages are also available.

We will use the basic plan from the above-mentioned provider to develop our application for our own use, for those of you who are interested in the Enterprise version, you can ask the API provider for further information. I don’t know how much data will I get from the free version but we will see when we progress together!

In this chapter, I am just going to take it easy by following the python code provides by the API provider on their page to make a request and see what will I get from the API call. I have created a file to keep the API key and the API hostname so the program can read those two items later on.

import requests

# read the host and key from file then insert into a list
key_host = []
# since the file is there therefore no need to use the try and except block
f=open("key.txt", "r")
if f.mode == 'r':
    f_l = f.readlines()
    for line in f_l:
        key_host.append(line)
f.close()

url = "https://edamam-food-and-grocery-database.p.rapidapi.com/parser"

querystring = {"ingr":"orange"}

headers = {
    'x-rapidapi-host': key_host[0][:-1],
    'x-rapidapi-key': key_host[1]
    }

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)

Below is what we receive after the API call.

Part of the data we have received

I have also noticed that there are lots of URIs within the returning data that we can use to make further API call (perhaps?).

The above is just a simple program, I like to go from really easy to complicated, we build a little bit of stuff day by day, and we will get there slowly, don’t worry!



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...