coddingtonbear / python-myfitnesspal

Access your meal tracking data stored in MyFitnessPal programatically
MIT License
797 stars 139 forks source link

feat: new MFP APIs to incorporate #154

Closed sean-freeman closed 1 year ago

sean-freeman commented 1 year ago

There are a number of new MFP APIs being released, which would simplify and enhance this SDK

New diary report

https://www.myfitnesspal.com/reports/printable-diary?from=2023-02-22&to=2023-04-22 https://www.myfitnesspal.com/reports/printable-diary/friend_username?from=2023-02-22&to=2023-04-22

headers = {'Content-Type': 'application/json'}
data = '{"id":"1234567890","show_food_diary":1,"show_exercise_diary":1,"show_food_notes":0,"show_exercise_notes":0,"from":"2023-02-22","to":"2023-04-22"}'
response = requests.post('https://www.myfitnesspal.com/api/services/diary/report', headers=headers, data=data)

New friends list

https://www.myfitnesspal.com/friends

# Get MyFitnessPal build id to then determine friends list URL
response = requests.get("https://www.myfitnesspal.com")
response_text_json_extract = re.search('<script id="__NEXT_DATA__" type="application/json">(.+?)</script>', response.text).group(1)
mfp_build_id = json.loads(response_text_json_extract)["buildId"]
self_friends_json = json.loads(client._get_content_for_url("https://www.myfitnesspal.com/_next/data/" + mfp_build_id + "/en/friends.json?sort_order=oldest&limit=120"))
# Get friends
self_friends_list = []
for item in self_friends_json['pageProps']['dehydratedState']['queries']:
    if "total_friends" in item['state']['data']:
        for friend in item['state']['data']['friends']:
            self_friends_list.append(friend['username'])