Skip to content

Commit

Permalink
Add Exception Handling
Browse files Browse the repository at this point in the history
MOD Tidy Up
  • Loading branch information
Dnic94 committed Feb 13, 2022
1 parent 96a29aa commit e1b88df
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions myfitnesspal/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,6 @@ def get_food_item_details(self, mfp_id: int) -> FoodItem:
### Dominic Schwarz (Dnic94) <[email protected]> - 25.08.2021 ###
### Added function to submit new foods to MFP.


def set_new_food(self, brand: str, description: str, calories: int, fat: float, carbs: float, protein: float,
sodium: float = "", potassium: float = "", saturated_fat: float = "",
polyunsaturated_fat: float = "",
Expand All @@ -828,7 +827,7 @@ def set_new_food(self, brand: str, description: str, calories: int, fat: float,
SUBMIT_NEW_PATH = f"food/new?date={datetime.datetime.today().strftime('%Y-%m-%d')}&meal=0"
SUBMIT_POST_PATH = "food/new"

#get Authenticity Token
# get Authenticity Token
url = parse.urljoin(self.BASE_URL_SECURE, self.SUBMIT_PATH)
now = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
document = self._get_document_for_url(url)
Expand Down Expand Up @@ -1034,19 +1033,19 @@ def set_new_goal(self, energy: float = "", energy_unit: str = "", carbohydrates:
energy = macro_energy

# Build payload based on observed browser behaviour
#TODO Insert additional micro nurtitions
# TODO Insert additional micro nurtitions
new_goals = {}
new_goals['item'] = old_goals['items'][0]

new_goals['item'].pop('valid_to', None)
new_goals['item'].pop('default_group_id', None)
new_goals['item'].pop('updated_at', None)
new_goals['item']['default_goal']['meal_goals'] = []
#new_goals['item']['default_goal'].pop('exercise_carbohydrates_percentage', None)
#new_goals['item']['default_goal'].pop('exercise_fat_percentage', None)
#new_goals['item']['default_goal'].pop('exercise_protein_percentage', None)
#new_goals['item']['default_goal'].pop('exercise_saturated_fat_percentage', None)
#new_goals['item']['default_goal'].pop('exercise_sugar_percentage', None)
# new_goals['item']['default_goal'].pop('exercise_carbohydrates_percentage', None)
# new_goals['item']['default_goal'].pop('exercise_fat_percentage', None)
# new_goals['item']['default_goal'].pop('exercise_protein_percentage', None)
# new_goals['item']['default_goal'].pop('exercise_saturated_fat_percentage', None)
# new_goals['item']['default_goal'].pop('exercise_sugar_percentage', None)

# insert new values
new_goals['item']['valid_from'] = today
Expand All @@ -1056,26 +1055,24 @@ def set_new_goal(self, energy: float = "", energy_unit: str = "", carbohydrates:
new_goals['item']['default_goal']['carbohydrates'] = carbohydrates
new_goals['item']['default_goal']['protein'] = protein
new_goals['item']['default_goal']['fat'] = fat
#TODO Add micro nutritions

# TODO Add micro nutritions

for i in new_goals['item']['daily_goals']:

i['meal_goals'] = []
i.pop('group_id', None)
#i.pop('exercise_carbohydrates_percentage', None)
#i.pop('exercise_fat_percentage', None)
#i.pop('exercise_protein_percentage', None)
#i.pop('exercise_saturated_fat_percentage', None)
#i.pop('exercise_sugar_percentage', None)
# i.pop('exercise_carbohydrates_percentage', None)
# i.pop('exercise_fat_percentage', None)
# i.pop('exercise_protein_percentage', None)
# i.pop('exercise_saturated_fat_percentage', None)
# i.pop('exercise_sugar_percentage', None)

# insert new values
i['energy']['value'] = energy
i['energy']['unit'] = energy_unit
i['carbohydrates'] = carbohydrates
i['protein'] = protein
i['fat'] = fat
#TODO Add micro nutritions
# TODO Add micro nutritions

# Build Post-Request
# Post Request
Expand Down Expand Up @@ -1127,12 +1124,12 @@ def get_recipe_list(self):
else:
next_page = False # Only one link means it is the last page
else:
#Indicator for no recipes
# Indicator for no recipes
next_page = False
if len(recipes_dict) == 0:
return None

#print(recipes_dict.values())
# print(recipes_dict.values())
return recipes_dict

def get_recipe(self, recipeid: int):
Expand All @@ -1153,15 +1150,20 @@ def get_recipe(self, recipeid: int):
recipe_dict['recipeIngredient'].append(ingridient.text.strip(" \n"))

recipe_dict['nutrition'] = {"@type": "NutritionInformation"}
recipe_dict['nutrition']['calories'] = document.xpath('//*[@id="main"]/div[3]/div[2]/div[2]/div')[0].text.strip(
recipe_dict['nutrition']['calories'] = document.xpath('//*[@id="main"]/div[3]/div[2]/div[2]/div')[
0].text.strip(
" \n")
recipe_dict['nutrition']['carbohydrateContent'] = document.xpath('//*[@id="carbs"]/td[1]/span[2]')[
0].text.strip(
" \n")
recipe_dict['nutrition']['fiberContent'] = document.xpath('//*[@id="fiber"]/td[1]/span[2]')[0].text.strip(" \n")
recipe_dict['nutrition']['sugarContent'] = document.xpath('//*[@id="sugar"]/td[1]/span[2]')[0].text.strip(" \n")
recipe_dict['nutrition']['sodiumContent'] = document.xpath('//*[@id="sodium"]/td[1]/span[2]')[0].text.strip(" \n")
recipe_dict['nutrition']['proteinContent'] = document.xpath('//*[@id="protein"]/td[1]/span[2]')[0].text.strip(
recipe_dict['nutrition']['fiberContent'] = document.xpath('//*[@id="fiber"]/td[1]/span[2]')[0].text.strip(
" \n")
recipe_dict['nutrition']['sugarContent'] = document.xpath('//*[@id="sugar"]/td[1]/span[2]')[0].text.strip(
" \n")
recipe_dict['nutrition']['sodiumContent'] = document.xpath('//*[@id="sodium"]/td[1]/span[2]')[0].text.strip(
" \n")
recipe_dict['nutrition']['proteinContent'] = document.xpath('//*[@id="protein"]/td[1]/span[2]')[
0].text.strip(
" \n")
recipe_dict['nutrition']['fatContent'] = document.xpath('//*[@id="total_fat"]/td[1]/span[2]')[0].text.strip(
" \n")
Expand All @@ -1181,11 +1183,10 @@ def get_recipe(self, recipeid: int):
logger.warning(f"Could not extract recipe information from {recipe_url}")
return None


#### add some required tags to match schema
recipe_dict['recipe_instructions'] = []
recipe_dict['tags'] = ["MyFitnessPal"]
#print(json.dumps(recipe_dict))
# print(json.dumps(recipe_dict))
return recipe_dict

def get_meal_list(self):
Expand All @@ -1195,7 +1196,6 @@ def get_meal_list(self):
meals_url = parse.urljoin(self.BASE_URL_SECURE, MEALS_PATH)
document = self._get_document_for_url(meals_url)


meals = document.xpath("//*[@id='matching']/li") # get all items in the recipe list
try:
for meal in meals:
Expand All @@ -1207,8 +1207,7 @@ def get_meal_list(self):
logger.warning("Could not load meals.")
return None


#print(meals_dict.values())
# print(meals_dict.values())
return meals_dict

def get_meal(self, mealid: int, meal_title: str):
Expand All @@ -1225,7 +1224,7 @@ def get_meal(self, mealid: int, meal_title: str):
recipe_dict['recipeIngredient'] = []
try:
ingridients = document.xpath('//*[@id="meal-table"]/tbody/tr')
#No ingridents?
# No ingridents?
if len(ingridients) == 1 and ingridients[0].xpath('./td[1]')[0].text == '\xa0':
raise
else:
Expand Down Expand Up @@ -1259,6 +1258,6 @@ def get_meal(self, mealid: int, meal_title: str):
#### add some required tags to match schema
recipe_dict['recipe_instructions'] = []
recipe_dict['tags'] = ["MyFitnessPal"]
#print(json.dumps(recipe_dict))
# print(json.dumps(recipe_dict))

return recipe_dict

0 comments on commit e1b88df

Please sign in to comment.