Пример #1
0
class SofiaAPI:
  def __init__(self, intent_request, session):
    self.session = session
    self.fs = Fatsecret(oauth_consumer_key, oauth_secret_key)
    self.calorie_count = 0
    # self.request_date = datetime.strptime(intent_request['timestamp'], "%Y-%m-%dT%H:%M:%SZ")

  # ------- Interface with the Intents ----------
  #def save_exercise(self, exercise_name, duration):
    #method = "method":"exercises.get"
   # response = encode([method])
   # if response is not None:
   #     exercise_name = response.exercise_types.exercise[0].exercise_name
   # method = "exercise_entries.commit_day"
   # #return encode([method, self.auth_token])

  def calculate(self, response_type):
    if self.calorie_count < 500:
        return "You have eaten way too little today! Only "+ calorie_count + " calories!"
    elif self.calorie_count < 1000 and calorie_count > 500:
        return "You are doing well today. You have eaten "+ calorie_count + " calories!"
    elif self.calorie_count > 1000:
        return "You have eaten way too much, you fatty!"

  def add_food(self, food_name):
    foods = self.fs.foods_search(food_name)
    if foods is not None:
        food_description = foods[0]['food_description']
        print(food_description)
        food_id = foods[0]['food_id']
        food_name = foods[0]['food_name']
        #self.fs.food_entry_create(food_id, food_name, 0, 1, "breakfast", date=None)
        print(food_description)
        return self.get_calories(food_description)
    return "Could not find this food type."

  def get_calories(self, str):
      index = str.find("Calories: ")
      if index != -1:
        str = str[index+9: index+13]
        cals = str.strip(' ')
        self.calorie_count = self.calorie_count + int(cals)
        return cals
      else:
        return 0

  def configure_me(self, height, weight, age, gender):
      return "2000"
    # self.fs.profile_create(user_id=None)
    # if weight:
    #     if height:
    #         return self.set_weight(weight, height)
    #     else:
    #         return self.set_weight(weight, 180)

  def set_weight(self, weight, height):
      self.fs.weight_update(weight, date=None, weight_type='kg', height_type='cm', goal_weight_kg=None, current_height_cm=None, comment=None)