ntalekt / Cox_Data_Usage

Script that collects Cox Communications data usage for internet subscribers
18 stars 8 forks source link

Login Method Changed #5

Closed atowarnyckyj closed 4 years ago

atowarnyckyj commented 4 years ago

As of 7/1/2020, Cox has changed the way you log in to their system.

When you sign in to https://www.cox.com/resaccount/sign-in.cox it redirects to https://idm.east.cox.net/idm/coxnetlogin where you are asked to sign in again.

When executing the script, this is the result I get:

bash-5.0# cat cox_usage.json | python -m json.tool { "businessUnit": "res:sign-in", "channel": "cox:res:signin", "dateStamp": "", "language": "en", "pageName": "cox:res:sign-in", "pageType": "signin", "purchaseStep": "signin", "responsiveDisplayType": "desktop", "selfHelpName": "signin:page", "subSection": "cox:res:signin", "visitCount": "", "visitorLoginStatus": "loggedout", "zip": "" }

dejanzelic commented 4 years ago

The new flow(from what I can tell) uses JS to load the form. I was able to get it to work using selenium. Here is my updated script: https://github.com/dejanzelic/coxusage-docker I'm also running this in docker.

chris8837 commented 4 years ago

How to I get this to work with a non-docker Home Assistant?

atowarnyckyj commented 4 years ago

Thank you dejanzelic. I will test and report if it works for me. Chris8837, the script doesn't need to be ran in a docker container. You can run the python script stand-alone and it'll create a .json file that you read from. Just modify the script to work with your environment.

chris8837 commented 4 years ago

Man I thought I knew how to work HA, but I have no idea what you talking about. If you have a quick sec to point me in the right direction?

ntalekt commented 4 years ago

@chris8837, @atowarnyckyj is talking about the script in @dejanzelic's repo as the one in mine hasn't been updated to work with the new Cox login system. Updated script: https://github.com/dejanzelic/coxusage-docker/blob/master/coxusage.py

@dejanzelic thanks for the work on this I'll test it out shortly, I'm really excited for the docker option!! I haven't been maintaining my repo so if you don't mind I'll just link yours. Is that ok?

atowarnyckyj commented 4 years ago

Actually, looking into it myself, the way @dejanzelic 's linked docker container runs requires a little more manipulation on the python side than I thought.

The way I had @chris8837 's cox_data_usage script running with HA was to have an automation execute a shell command which executed the script and produced the json file (per his instructions in the readme). The data was pulled from the file into sensors and was also sent to an InfraDB for Grafana import.

Having to execute this through docker changes a lot.

If you don't want to run it from docker, you could download the .py script from the repo, import the python selenium library you need using pip install selenium but you'll also need to configure it properly. Then you'd create the same shell command automation in HA to execute the python script (python <path.to/script>) the way I described above.

Since I'm running HA in docker, this complicates things as I need to install python modules in my container.

You could also just run a cron job that executes the docker container as is described but that's if you have docker installed at all.

Sorry if this is confusing. I'm not really good at teaching stuff.

dejanzelic commented 4 years ago

I should have added a bit more clarity. I like using docker to keep my host system as minimal as possible. I run HA using docker and I run this script using docker as well. Instead of using the shell_command component to execute the script, I have a cronjob that runs the docker container and output the JSON to a file that HA has access to. HA will then read this file (using the same sensor: component originally created by @ntalekt and I have my utilization in HA.

@atowarnyckyj is exactly right in the previous message sent. If you want to run this through HA directly you will have to install selenium and a web driver. This is possible, but I'm pretty sure it will be a headache to get the web driver to work.

Let's say that you have you're HA configuration files in /etc/docker/ha. To get a JSON file in that directory, you would run:

docker container run --rm --env-file secret/cox.env -v /etc/docker/ha/:/data/ dejandayoff/coxusage

Now in your HA docker container, you can configure the sensor templates to read from /config/coxusage.json

PS @ntalekt, I would have made a PR, but I figured my changes were too drastic since it required a rewrite with this change. Linking works for me! I really like your documentation for importing the sensor data.

ntalekt commented 4 years ago

Thanks @dejanzelic I agree about your statement on the PR. I just noted that this repo is deprecated and linked to yours and my new one. I forked the updated script, did some testing with a smaller docker image based on alpine, updated the documentation, and also published an image on docker hub.

I'd be happy to shoot you a PR if you want to own it going forward. Just let me know!

@chris8837 as far as your question, it is possible to run the script natively but like @atowarnyckyj said it will take some reverse engineering to get python, chrome driver, and selenium working. If you wish to try there are some examples in the docker-python-chromedriver repo of how they configure their images: https://github.com/joyzoursky/docker-python-chromedriver/blob/master/py3/py3.7-selenium/Dockerfile

It's a ton cleaner by leveraging docker.

dejanzelic commented 4 years ago

I don't mind maintaining it. Feel free to send the PR over. Thanks!