coddingtonbear / python-myfitnesspal

Access your meal tracking data stored in MyFitnessPal programatically
MIT License
800 stars 138 forks source link

get_exercise: invalid literal for int() with base 10: '' #52

Closed rbelzile closed 6 years ago

rbelzile commented 7 years ago

When I attempt to use the get_exercise code, I get the following error.

>>> import myfitnesspal
>>> client = myfitnesspal.Client('********', '********')
>>> client.get_exercise(2017,9,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "myfitnesspal\client.py", line 432, in get_exercise
    exercise = self._get_exercise(document)
  File "myfitnesspal\client.py", line 381, in _get_exercise
    value = self._get_numeric(column.text)
  File "myfitnesspal\client.py", line 220, in _get_numeric
    return int(re.sub(r'[^\d.]+', '', string))
ValueError: invalid literal for int() with base 10: ''

I believe it's related to the fact that my iPhone syncs my step data in and returns N/A for minutes, which means that the re.sub... in _get_numeric is returning an empty string to int()

I can fix this, but I already have a pull request open and I'm unsure semantically if it makes more sense to check in _get_numeric and return 0 for blank strings or whether the get_exercise should be parsing for entirely non-numeric content before calling _get_numeric

jtszalay commented 7 years ago

That's exactly what it's related to. I have the same issue.

jtszalay commented 7 years ago

In addition, when fixing this so that if 'N/A' in column.text store as None, I discovered that Fitbit calorie adjustments show up with a blank name. They have two a tags in the td.

`

          <div class="exercise-description">

              <a onclick="showAdjustmentInfo(); return false;" href="#">

                Fitbit calorie adjustment
              </a>

          </div>

            <a onclick="showAdjustmentInfo );" style="cursor: pointer;">
              <img alt="Ic_i" src="http://d34yn14tavczy0.cloudfront.net/assets/ic_i-5a18e414bbf2a80fbfa2bed9c28c79c7.png">
            </a>

        </td>`

The if statement for grabbing the name returns the non nested a tag which is empty.

if columns[0].find('a') is None or not columns[0].find('a').text.strip():

^ That seems to fix it. I'll try to put in a PR soon with this fix.

jtszalay commented 7 years ago

55 This is the related PR

1minus1 commented 6 years ago

I am having this same issue. Any chance for a fix?

jtszalay commented 6 years ago

I make a PR and it got merged. But yesterday I had the same issue again. I installed from source instead of pip install MyFitnessPal and it worked. Try doing a pip install git+https://GitHub.com/Coddingtonbear/Python-myfitnesspal.git

1minus1 commented 6 years ago

Done, that fixed it. Thanks.

jtszalay commented 6 years ago

No problem. I guess the pip version is stale?

coddingtonbear commented 6 years ago

Ahh; hrm -- sorry about that; I'll push out a new release later today!

coddingtonbear commented 6 years ago

Released @jgissend10's fix as part of 1.11.0; sorry for the delay!