Esempio n. 1
0
def enter_recipe():
    """ Add a new recipe in DB """

    title = request.form.get("title")
    description = request.form.get("description")
    source = request.form.get("source")
    cat_code = request.form.get("category")
    cuisine = request.form.get("cuisine")
    servings = request.form.get("servings")
    cooktime = request.form.get("cookTime")
    skillLevel = request.form.get("level")
    step1 = request.form.get("step1")
    step2 = request.form.get("step2")
    step3 = request.form.get("step3")
    steps = [step1, step2, step3]

    # json.loads(request.form.get("listIngr"))
    ingredients = json.loads(request.form.get("listIngr"))

    img_file = ''
    user = ''

    if 'User' in session:

        user = session['User']

    if 'Image' in request.files:

        img_file = request.files['Image']
        print "IMAGE FILE", img_file

        if img_file and allowed_file(img_file.filename):
            filename = secure_filename(img_file.filename)
            print filename
            img_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

    value = helpFunctions.addRecipe(img_file, title, description, cat_code, servings,
             cooktime, skillLevel, cuisine, ingredients, steps, user)
    
    if  type(value) == int:

        message = {  'msg': "Recipe successfully added",
                     'recipeid': value }
    else:

        message = { 'msg': value }

    return jsonify(message)
Esempio n. 2
0
def url_scraper(url, user):


########    SCRAP DATA FROM URL   #########


    r = requests.get(url)

    data = r.text

    soup = BeautifulSoup(data, 'html.parser')

    title = soup.header.h1.contents[0].strip()

    description = ''

    if soup.find('div', 'topnote'):

        description = soup.find('div', 'topnote').p.contents[0]

    time = ''

    servings = ''

    img_url = ''

    cat_code = ''

    cuisine = ''

    skillLevel = ''

    if soup.find('span', 'icon icon-clock'):

        time = soup.find('span', 'icon icon-clock').next_sibling

    if soup.find('span', 'icon icon-person'):

        servings = soup.find('span', 'icon icon-person').next_sibling.contents[0]

    if soup.find('div', 'media-container '):

        if soup.find('div', 'media-container ').find('img'):

            img_url = soup.find('div', 'media-container ').find('img')['src']

    elif soup.find('div', 'video-container'):

        img_url = soup.find('div', 'video-container').\
                find_all(attrs={"itemprop" : "image"})[0]['content']

    elif soup.find('div', 'media-container vertical'):   

        if soup.find('div', 'media-container vertical').find('img'): 

            img_url = soup.find('div', 'media-container vertical').find('img')['src']                   
    

    text = soup.find('div', 'recipe grid-wrap').get_text(strip=True)

    text = set(text.split())

    cat_code = calculateCategory(text)

    cuisine = calculateCuisine(text)

    print "CAT", cat_code

    ingredients = []

    passi = []

    unit = ''

    # FIND ALL THE INGREDIENTS

    for ingr_item in soup.find_all('ul', 'recipe-ingredients'):

        for li in ingr_item.find_all('li'):

            quantity = li.find('span', 'quantity')

            if quantity:

                qty = quantity.next

            print "QUANTITY ", qty

            more_info = li.find('span','ingredient-name')

            ingredient = ''

            if more_info:

                info = more_info.text

                print "INFO", info

                unit = getUnit(info)

                if more_info.span:


                    if isinstance(more_info.span.next, unicode):
                    #     ingredient = unicode(ingredient)
                    #     print "SCRAPED INGR", ingredient
                    #     ingredient = ingredient.encode('utf-8','ignore')

                        ingredient = unicodedata.normalize('NFKD', more_info.span.next).encode('ascii','ignore')
                    
                    else:
                         ingredient = more_info.span.next

                    print "INGREDIENT", ingredient
            if ingredient:

                ingredient = cleanIngredient(ingredient)

                ingredients.append( {'name': ingredient,
                                 'qty': qty,
                                 'unit': unit})

    # FIND ALL THE STEPS

    for steps in soup.find_all('ol', 'recipe-steps'):

        for li in steps.find_all('li'):

            step = li.text

            passi.append(step)

            print "STEP", step


########    ADD TO DATABASE   #########

    # Missing info: CUISINE

    title = title.strip()
    time = time.strip()
    print "IMAGE_URL", img_url

    message = helpFunctions.addRecipe(img_url, title, description, cat_code, servings,
             time, skillLevel, cuisine, ingredients, passi, user)


    print "MESSAGE", message
    return message
Esempio n. 3
0
def url_scraper(url, user):

    ########    SCRAP DATA FROM URL   #########

    r = requests.get(url)

    data = r.text

    soup = BeautifulSoup(data, 'html.parser')

    title = soup.header.h1.contents[0].strip()

    description = ''

    if soup.find('div', 'topnote'):

        description = soup.find('div', 'topnote').p.contents[0]

    time = ''

    servings = ''

    img_url = ''

    cat_code = ''

    cuisine = ''

    skillLevel = ''

    if soup.find('span', 'icon icon-clock'):

        time = soup.find('span', 'icon icon-clock').next_sibling

    if soup.find('span', 'icon icon-person'):

        servings = soup.find('span',
                             'icon icon-person').next_sibling.contents[0]

    if soup.find('div', 'media-container '):

        if soup.find('div', 'media-container ').find('img'):

            img_url = soup.find('div', 'media-container ').find('img')['src']

    elif soup.find('div', 'video-container'):

        img_url = soup.find('div', 'video-container').\
                find_all(attrs={"itemprop" : "image"})[0]['content']

    elif soup.find('div', 'media-container vertical'):

        if soup.find('div', 'media-container vertical').find('img'):

            img_url = soup.find('div',
                                'media-container vertical').find('img')['src']

    text = soup.find('div', 'recipe grid-wrap').get_text(strip=True)

    text = set(text.split())

    cat_code = calculateCategory(text)

    cuisine = calculateCuisine(text)

    print "CAT", cat_code

    ingredients = []

    passi = []

    unit = ''

    # FIND ALL THE INGREDIENTS

    for ingr_item in soup.find_all('ul', 'recipe-ingredients'):

        for li in ingr_item.find_all('li'):

            quantity = li.find('span', 'quantity')

            if quantity:

                qty = quantity.next

            print "QUANTITY ", qty

            more_info = li.find('span', 'ingredient-name')

            ingredient = ''

            if more_info:

                info = more_info.text

                print "INFO", info

                unit = getUnit(info)

                if more_info.span:

                    if isinstance(more_info.span.next, unicode):
                        #     ingredient = unicode(ingredient)
                        #     print "SCRAPED INGR", ingredient
                        #     ingredient = ingredient.encode('utf-8','ignore')

                        ingredient = unicodedata.normalize(
                            'NFKD',
                            more_info.span.next).encode('ascii', 'ignore')

                    else:
                        ingredient = more_info.span.next

                    print "INGREDIENT", ingredient
            if ingredient:

                ingredient = cleanIngredient(ingredient)

                ingredients.append({
                    'name': ingredient,
                    'qty': qty,
                    'unit': unit
                })

    # FIND ALL THE STEPS

    for steps in soup.find_all('ol', 'recipe-steps'):

        for li in steps.find_all('li'):

            step = li.text

            passi.append(step)

            print "STEP", step

########    ADD TO DATABASE   #########

# Missing info: CUISINE

    title = title.strip()
    time = time.strip()
    print "IMAGE_URL", img_url

    message = helpFunctions.addRecipe(img_url, title, description, cat_code,
                                      servings, time, skillLevel, cuisine,
                                      ingredients, passi, user)

    print "MESSAGE", message
    return message