Yummly API的Python库:https://developer.yummly.com
yumml的Python项目详细描述
注意:此库及其作者与Yummly无关。
安装
pip install yummly
依赖关系
- 请求>;=1.1.0
用法
使用yummly.Client创建一个客户端对象以与美味api交互。
客户端接受api_id、api_key和timeout作为初始参数:
fromyummlyimportClient# default option valuesTIMEOUT=5.0RETRIES=0client=Client(api_id=YOUR_API_ID,api_key=YOUR_API_KEY,timeout=TIMEOUT,retries=RETRIES)search=client.search('green eggs and ham')match=search.matches[0]recipe=client.recipe(match.id)
搜索食谱
API终结点:^{TT5}$
搜索符合特定条件的食谱:
results=yummly.search('bacon')print('Total Matches:',results.totalMatchCount)formatchinresults.matches:print('Recipe ID:',match.id)print('Recipe:',match.recipeName)print('Rating:',match.rating)print('Total Time (mins):',match.totalTimeInSeconds/60.0)print('----------------------------------------------------')<>将你的结果限制在最大值:
# return the first 10 resultsresults=yummly.search('chicken marsala',maxResults=10)
偏移分页结果:
# return 2nd page of resultsresults=yummly.search('pulled pork',maxResults=10,start=10)
提供搜索参数:
params={'q':'pork chops','start':0,'maxResult':40,'requirePicutres':True,'allowedIngredient[]':['salt','pepper'],'excludedIngredient[]':['cumin','paprika'],'maxTotalTimeInSeconds':3600,'facetField[]':['ingredient','diet'],'flavor.meaty.min':0.5,'flavor.meaty.max':1,'flavor.sweet.min':0,'flavor.sweet.max':0.5,'nutrition.FAT.min':0,'nutrition.FAT.max':15}results=yummly.search(**params)
有关支持的搜索参数的完整列表,请参见部分搜索配方调用位于:https://developer.yummly.com/intro
搜索响应示例:https://developer.yummly.com/wiki/search-recipes-response-sample
获取配方
API终结点:^{TT6}$
按配方ID获取配方:
recipe=yummly.recipe(recipe_id)print('Recipe ID:',recipe.id)print('Recipe:',recipe.name)print('Rating:',recipe.rating)print('Total Time:',recipe.totalTime)print('Yields:',recipe.yields)print('Ingredients:')foringredinrecipe.ingredientLines:print(ingred)
配方响应示例:https://developer.yummly.com/wiki/get-recipe-response-sample
注意:yummly的get recipe响应包含作为字段名的yield。但是,yield是python中的一个关键字,因此它被重命名为yields。
搜索元数据
API终结点:^{TT10}$
yummly提供了一个元数据端点,该端点返回允许/排除的成分、饮食、过敏和其他搜索参数的可能值:
METADATA_KEYS=['ingredient','holiday','diet','allergy','technique','cuisine','course','source','brand','restriction']ingredients=client.metadata('ingredient')diets=client.metadata('diet')sources=client.metadata('source')
注意:yummly的原始api将此数据作为jsonp响应返回,yummly.py解析掉,然后转换为包含相应元数据类实例的list。
API模型类
所有底层api模型类都在yummly/models.py中。用于所有模型的基类是一个具有属性样式访问的修改过的dict类(即obj.foo和obj['foo']都是有效的访问器方法)。
选择了派生的dict类来适应到json的无痛转换,这是使用yummly.py作为api代理来为应用程序(例如,在服务器上运行yummly.py的web应用程序,而不是直接在前端使用yummly api)提供数据时非常常见的要求。
测试
测试位于tests/。它们可以使用pytest从根目录中使用makefile或pytest执行。
# using makefile make test # using pytest directly py.test yummly
注意:运行测试套件将使用真正的api调用,这将计入您的调用限制。目前,在运行测试时会发出22个api调用。
测试配置文件
运行测试需要测试配置文件。使用以下属性创建tests/config.json:
{"api_id":"YOUR_API_ID","api_key":"YOUR_API_KEY"}
测试运行时将自动加载此文件。
许可证
这个软件是根据麻省理工学院的许可证授权的。
待办事项
- 为营养、口味和元数据等复杂的搜索参数提供帮助