<jupyter_start><jupyter_text>Migros API to extract Food DataThis is for extracting the data from the Migros API to get food/logistic data. There will be several categories of this speicified later on.<jupyter_code>hackzurich2020
uhSyJ08KexKn4ZFS
import requests
import json
#Products API, with verbosity = id and limit=2000, then go through the page ids of all products
with open('ingredients_de.txt', 'r', encoding='utf-8') as file:
    ingredients_de = file.readlines()

    
ingredients_de = [x.strip() for x in ingredients_de] 

print(ingredients_de) 
recipes = []
for ingredient in ingredients_de:
    print(ingredient)
    response_code = 100
    size = 100
    
    while(response_code != 200):
        url = f"https://hackzurich-api.migros.ch/hack/recipe/recipes_de/_search?q={ingredient}&size={size}"
        response = requests.get(url, auth=('hackzurich2020', 'uhSyJ08KexKn4ZFS'))
        if(size == 20):
            size = 0
            break
        if(response.status_code != 200):
            size = 20
            print(ingredient)
        response_code = response.status_code
    if(size != 0):
        response_json = response.json()
        recipe = {}
        for hit in response_json['hits']['hits']:
            recipe['id'] = hit['_id']
            recipe['title'] = hit['_source']['title']
            for taxonomy in hit['_source']['taxonomies']:
                if taxonomy['name'] == 'Vegetarisch':
                    recipe['Veg'] = True
                elif taxonomy['name'] == 'Vegan':
                    recipe['Vegan'] = True
            if 'Veg' not in recipe:
                recipe['Veg'] = False
                recipe['Vegan'] = False
            elif 'Vegan' not in recipe:
                recipe['Veg'] = False
            recipe['url'] = hit['_source']['urls'][0]['url']

            size = hit['_source']['sizes'][0]

            quanity = size['quantity']
            ingredients = size['ingredient_blocks'][0]['ingredients']
            recipe['ingredients'] = ingredients
            recipes.append(recipe)
    #For each ingredient, what do we want to keep the recipes found for 2 person
    

f = open("Migros_recipes.json","w")
json.dump(recipes, f)
f.close()
import json
import requests
import json
import pandas as pd
import math
import re
from requests.auth import HTTPBasicAuth

YOUR_KEY = "<KEY>"
AUTH = HTTPBasicAuth(YOUR_KEY, "")
BASE_URL = "https://co2.eaternity.ch"

if YOUR_KEY == "CHANGEME":
    raise RuntimeError("Please change your api key!!")

def create_kitchen(name, kitchen_id, location):
    url = f"{BASE_URL}/api/kitchens/{kitchen_id}"

    body = {
        "kitchen": {
            "name": name,
            "location": location
        }
    }

    response = requests.put(url, json=body, auth=AUTH)
    if response.status_code not in [200, 201, 202]:
        print(f"ERROR: Failed PUTting kitchen {kitchen_id} with status {response.status_code}: '{response.text}'")
    else:
        print(f"SUCCESS: PUT kitchen {kitchen_id}")
        return response.json()

    
def put_recipe_migros(recipe_id, recipe_name, kitchen_id, ingredients, bad_ingr):
    url = f"{BASE_URL}/api/kitchens/{kitchen_id}/recipes/{recipe_id}"

    ingredient_list = []
    
    print(ingredients)
    for ingredient in ingredients:
        ingredient_txt = ingredient['text'].lower().strip()
        if ingredient_txt not in bad_ingr and ingredient_txt:
            print(ingredient_txt)
            if ingredient['amount']['unit'] == 'g':
                ingred_dict = {
                    "id": ingredient_txt,
                    "names": [{"language": "de", "value": ingredient_txt}],
                    "amount": ingredient['amount']['quantity'],
                    "unit": "gram"}
                ingredient_list.append(ingred_dict)
        
    body = {
        "recipe": {
            "titles": [
                {
                    "language": "de",
                    "value": recipe_name
                }
            ],
            "date": "2020-09-19",
            "location": "Schweiz",
            "servings": 1,
            "ingredients": ingredient_list
        }
    }

    response = requests.put(url, json=body, auth=AUTH)
    
    if response.status_code in [601, 602]:
        print(response.text)
        bad_ingredient = re.findall(r"FoodService(.*)\\", response.text)[0].lower().strip()
        bad_ingr.append(bad_ingredient)
        print(bad_ingr)
        return put_recipe_migros(recipe_id, recipe_name, kitchen_id, ingredients, bad_ingr)
    
    elif response.status_code not in [200, 201, 202]:
        print(f"ERROR: Failed PUTting recipe {recipe_id} with status {response.status_code}: '{response.text}'")
    else:
        print(f"SUCCESS: PUT recipe {recipe_id}")
        print(response.json())
        print(bad_ingr)
        return response.json()

kitchen_id = "our_name_id"
create_kitchen("My First Kitchen", kitchen_id, "Switzerland")
bad_ingr = []
returns = []
for recipe in recipes:
    ingredients = recipe['ingredients']
    recipe_return = put_recipe_migros(recipe['id'], recipe['title'], kitchen_id, ingredients, bad_ingr)
    recipe_return['Veg'] = recipe['Veg']
    if recipe_return['Veg']:
        recipe_return['Vegan'] = recipe['Vegan']
    returns.append(recipe_return)
for recipe in recipes:
    ingredients<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1274564851207187, 'fraction_numerical': 0.014224218603780647, 'mean_word_length': 2.5157894736842104, 'pattern_counts': {'":': 18, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '221120', 'license': 'MIT', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1636, 'n_words': 375, 'path': 'migrosData/MigrosAPI.ipynb', 'repo_name': 'jgonsior/hackzurich', 'size': '5370'}