Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Added the option to specify the VIN number to Update_cars #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ Usage is very basic.
# update stats for all cars, and returns the list of cars. This will take
# 60+ seconds per car
cars = page.update_cars()

# Optionally you can call update_cars() with a vin of your choice to only update that car.
cars = page.update_cars('vin number')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that instead of putting the special logic on the update_cars to take vin, probably just make vin optional to the MyChevy constructor. Then get_cars() will just filter out the list of cars that don't match the vin, and should throw an exception if no car was found. That will let update_cars() remain unchanged, as the loop will just be a single car element.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I like that idea better. I will look into making the change to the constructor. I will also try to create a unit test for it too and then submit a new pull request.


# Percent battery charge
print(cars[0].percent)
Expand All @@ -98,13 +101,14 @@ provided.
<EVCar vin="...", range=185 miles, bat=100%, plugged_in=True, mileage=903 miles, charging=Your battery is fully charged., charge_mode=Departure Based, eta=None, state="">

config.ini must include your user and password for the mychevy site in the
following format:
following format, 'vin' is optional:

.. code-block:: ini

[default]
user = [email protected]
passwd = my@wes0mepa55w0rd
vin = KL821337VIN283498207

The ``mychevy`` command also takes the ``-S`` flag which makes the selenium
controlled web browser non headless during it's execution. This can be useful
Expand Down
5 changes: 4 additions & 1 deletion mychevy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ def main(config=None, show_browser=None):
click.echo("Loading data, this takes up to 2 minutes...")
page.login()
page.get_cars()
cars = page.update_cars()
if cfile.has_option("default","vin"):
cars = page.update_cars(cfile["default"]["vin"])
else:
cars = page.update_cars()
for c in cars:
click.echo(c)

Expand Down
6 changes: 5 additions & 1 deletion mychevy/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ def main(config=None, verbose=False):
click.echo(c)
click.echo("Updating cars with data")
try:
page.update_cars()
if cfile.has_option("default","vin"):
page.update_cars(cfile["default"]["vin"])
else:
page.update_cars()

click.echo("Displaying found cars with data")
for c in page.cars:
click.echo(c)
Expand Down
22 changes: 15 additions & 7 deletions mychevy/mychevy.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def from_json(self, data):
res = json.loads(data.decode('utf-8'))

# I've never actually seen serverErrorMsgs, but allow for them
if res["serverErrorMsgs"] or type(res["data"]) == str:
# The Error message I was seeing was data = {"messages":[],"serverErrorMsgs":[],"data":"SERVER ERROR"}
if res["serverErrorMsgs"] or type(res["data"]) == str or res['data'] == 'SERVER ERROR':
raise ServerError(res)

d = res["data"]
Expand All @@ -110,7 +111,7 @@ def from_json(self, data):
for a in CAR_ATTRS:
setattr(self, a, d[a])

except json.JSONDecodeError:
except ValueError:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh, JSONDecodeError was added in python 3.5, we can roll it back to value error for earlier versions. Probably also add 3.4 back into travis testing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the Travis tests I wasn't aware that this was developed with Python3. I have been doing all of my development and runs with Python2. Since Python2 isn't tested it might be nice to mention Python3 either in the README or put a sys.version_info[0] check in there to warn users that Python2 is unsupported.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good idea. I had the trove information up to date, but we could throw a warning there as well.

_LOGGER.exception("Failure to decode json: %s" % data)
except KeyError as e:
_LOGGER.exception("Expected key not found")
Expand Down Expand Up @@ -190,11 +191,18 @@ def get_cars(self):
Location: %s
""" % (self.cookies, res.content, res.history))

def update_cars(self):
def update_cars(self, vin=None):
headers = {"user-agent": USER_AGENT}
for c in self.cars:
url = EVSTATS_URL.format(c.vin, c.onstar)
res = requests.get(url, headers=headers,
cookies=self.cookies, allow_redirects=False)
c.from_json(res.content)
if vin:
if vin == c.vin:
url = EVSTATS_URL.format(c.vin, c.onstar)
res = requests.get(url, headers=headers,cookies=self.cookies, allow_redirects=False)
c.from_json(res.content)
return [c]
else:
url = EVSTATS_URL.format(c.vin, c.onstar)
res = requests.get(url, headers=headers,cookies=self.cookies, allow_redirects=False)
c.from_json(res.content)

return self.cars