Exemplo n.º 1
0
def handle_coffee(request_body, text):
    coffees = text.count(u'\u2615')
    if coffees == 0:
        return ''

    name = myutils.extract_user_first_name(request_body)
    uid = myutils.extract_user_id(request_body)
    date = myutils.extract_date(request_body)

    smilies = [u'\ud83d\ude43',u'\ud83d\ude0f',u'\ud83d\ude31',u'\ud83d\ude21']
    key = getCoffeeKey(uid, date)
    queryCoffee = getCoffeeAmount(key)

    logging.info('key: ' + key)
    logging.info('queryCoffee.timesDrank: ' + str(queryCoffee))
    logging.info('Coffees to add: {}'.format(coffees))

    amount_drank = coffees
    if queryCoffee is not None:
        amount_drank += queryCoffee.timesDrank

    coffeeDrank = CoffeeStore(key=ndb.Key('CoffeeStore', key),timesDrank=amount_drank)
    coffeeDrank.put()

    if amount_drank == 3:
        str_to_reply = name + ' drank ' + str(amount_drank) + ' coffee out of ' + str(3) + '\nIt\'s your last one !'
    elif amount_drank > 3:
        str_to_reply = name + ' drank ' + str(amount_drank) + ' coffee out of ' + str(3) + '\nPlease don\'t drink anymore...'
    else:
        str_to_reply = name + ' drank ' + str(amount_drank) + ' coffee out of ' + str(3) + '.'

    str_to_reply += ' ' + smilies[min(amount_drank-1,len(smilies)-1)]
    logging.info('reply: ' + str_to_reply)
    return str_to_reply
Exemplo n.º 2
0
def add_food(request_body, params, is_user=False):
    cmd_name = '/add_food_default' if not is_user else '/add_food'
    if len(params) != 2:
        logging.info('Params: {}'.format(params))
        return u'Didn\'t fully understand. Should be like: {} \ud83c\udf6a 100'.format(cmd_name)

    food_name = params[0]
    calories = params[1]
    if not myutils.is_number(calories) or int(calories) < 1:
        return 'Invalid calories ! Should be a number greater than 0.'

    key = food_name + ':' + str(myutils.extract_user_id(request_body)) if is_user else food_name
    user_id = 0 if not is_user else myutils.extract_user_id(request_body)
    food = FoodCalorieValues(key=ndb.Key('FoodCalorieValues',key), calories=int(calories), user_id=user_id)
    food.put()

    return 'Got it ! '+food_name+'={}'.format(calories)
Exemplo n.º 3
0
def reply_max_calories(request_body):
    uid = myutils.extract_user_id(request_body)
    name = myutils.extract_user_first_name(request_body)
    user_obj = get_max_calories(uid)
    if user_obj is None:
        return '{}, sorry but you didn\'t set max calories.'.format(name)
    else:
        return '{}, your max calories is {}'.format(name,
                                                    user_obj.max_calories)
Exemplo n.º 4
0
def show_foods(request_body):
    default_foods = get_foods(0)  # get default foods.

    user_id = myutils.extract_user_id(request_body)
    custom_foods = get_foods(user_id)

    default_foods_pretty = ['{} = {}'.format(x[0], x[1]) for x in default_foods.items()]
    custom_foods_pretty = ['{} = {}'.format(x[0], x[1]) for x in custom_foods.items()]
    all_foods = ['Default foods: '] + default_foods_pretty + ['Custom foods: '] + custom_foods_pretty
    result = '\n'.join(all_foods)

    return result.decode('utf-8')
Exemplo n.º 5
0
def handle_foods(request_body, text):
    uid = myutils.extract_user_id(request_body)
    user_obj = userstore.get_max_calories(uid)

    reply = []
    max_calories = 0
    if user_obj == None:
        reply.append('Sorry, you didn\'t set max calories. Please use /set_max_calories command.')
    else:
        max_calories = user_obj.max_calories

    # Create a dict of foods where the custom foods override the default foods
    # where necessary.
    foods = get_foods(0)
    custom_foods = get_foods(uid)
    for food in custom_foods.items():
        foods[food[0]] = food[1]

    calories_consumed = 0
    logging.info(foods)
    logging.info(text)
    logging.info(len(text))
    len_text = len(text)
    # Iterate through the message and try to find emojis
    for i in range(0, len_text):
        # Emojis can be one character long or two, so first check if it is
        # a single character emoji, if not try two characters.
        calories_consumed += get_food_calorie(text[i], foods)
        # Try two characters
        calories_consumed += get_food_calorie(text[i]+text[i+1], foods) if i < len_text-1 else 0

    # If no food found, maybe it was only text or not food tracking at all.
    if calories_consumed == 0:
        return ''

    date = myutils.extract_date(request_body)

    key = getCaloriesKey(uid, date)
    calories_obj = getCalories(key)

    if calories_obj is not None:
        calories_consumed += calories_obj.calories

    update_obj = CalorieStore(key=ndb.Key('CalorieStore', key), calories=calories_consumed)
    update_obj.put()

    reply.append('Calories counted {}/{}'.format(calories_consumed, max_calories))

    return '\n'.join(reply)
Exemplo n.º 6
0
def set_max_calories(request_body, params):
    if len(params) != 1 or not myutils.is_number(params[0]):
        logging.info('Params: {}'.format(params))
        return 'Didn\'t fully understand. Should be like: /set_max_calories 1800'

    uid = myutils.extract_user_id(request_body)
    name = myutils.extract_user_first_name(request_body)
    new_calories = int(params[0])
    logging.info('uid: {}, cals: {}'.format(uid, new_calories))

    user_obj = UserStore(key=ndb.Key('UserStore', uid),
                         max_calories=new_calories)
    user_obj.put()

    return '{}, max calories were updated.'.format(name)
Exemplo n.º 7
0
def update_coffee(request_body, params):
    logging.info('Inside updateCoffee...')
    if len(params) != 1 or not myutils.is_number(params[0]):
        logging.info('Params: {}'.format(params))
        return 'Didn\'t fully understand. Should be like: /coffeeupd 2'

    name = myutils.extract_user_first_name(request_body)
    uid = myutils.extract_user_id(request_body)
    date = myutils.extract_date(request_body)
    amount = int(params[0])

    key = getCoffeeKey(uid, date)
    coffeeDrank = CoffeeStore(key=ndb.Key('CoffeeStore', key),timesDrank=amount)
    coffeeDrank.put()

    return '{}, the coffee amount was updated.'.format(name)
Exemplo n.º 8
0
def show_food(request_body, params):
    if len(params) != 1:
        logging.info('Params: {}'.format(params))
        return u'Didn\'t fully understand. Should be like: /show_food \ud83c\udf6a'

    food_name = params[0]
    user_id = str(myutils.extract_user_id(request_body))

    default_cals = ndb.Key('FoodCalorieValues', food_name).get()
    user_cals = ndb.Key('FoodCalorieValues', food_name+':'+user_id).get()

    results = []
    if default_cals is not None:
        results.append(u'Default {} = {}'.format(food_name, default_cals.calories))
    if user_cals is not None:
        results.append(u'Custom {} = {}'.format(food_name, user_cals.calories))

    if len(results) == 0:
        results.append(u'Sorry, I don\'t recognize this food: {}'.format(food_name))

    return '\n'.join(results)
Exemplo n.º 9
0
def handle_water(request_body, text):
    waters = text.count(u'\U0001f376')
    if waters == 0:
        return ''

    uid = myutils.extract_user_id(request_body)
    date = myutils.extract_date(request_body)

    key = getWaterKey(uid, date)
    queryWater = getWaterAmount(key)

    logging.info('key: ' + key)
    logging.info('queryWater.timesDrank: ' + str(queryWater))
    logging.info('Waters to add: {}'.format(waters))

    amount_drank = waters
    if queryWater is not None:
        amount_drank += queryWater.timesDrank

    waterDrank = WaterStore(key=ndb.Key('WaterStore', key),
                            timesDrank=amount_drank)
    waterDrank.put()

    return 'Water drank today: {} cups'.format(amount_drank)