Python/selenium script to get Xfinity bandwidth usage from Xfinity MyAccount website.
Project description
Python/selenium script to get Xfinity bandwidth usage from Xfinity MyAccount website. Has an easily-usable command line entrypoint as well as a usable Python API, and an entrypoint to send usage to Graphite.
This is a little Python script I whipped up that the selenium-python package to log in to your Xfinity account and screen-scrape the data usage. By default the usage is just printed to STDOUT. You can also use the XfinityUsage class from other applications or scripts; see the docstrings on the __init__ and run methods for information. There are also options to send the data to a Graphite server.
For the changelog, see CHANGES.rst in the GitHub project.
Requirements
Python (tested with 2.7; should work with 3.3+ as well)
selenium Python package
One of the supported browsers:
A recent version of PhantomJS installed on your computer; this should be 2.0+, and the script is tested with 2.1.1.
Google Chrome or Chromium and chromedriver
Firefox and Geckodriver
Installation
pip install xfinity-usage
Usage
Command Line
Export your Xfinity username as the XFINITY_USER environment variable, your password as the XFINITY_PASSWORD environment variable, and run the xfinity-usage entrypoint. See xfinity-usage -h and the top-level docstring in the script for more information.
I’d highly recommend not leaving your username and password hard-coded anywhere on your system, but the methods for securing credentials are varied enough that the choice is yours.
Note that this screen-scrapes their site; it’s likely to break with a redesign.
Python API
See the source of the xfinity_usage.py script, specifically the __init__ and run methods of the XfinityUsage class. As a simple example:
>>> import os
>>> from xfinity_usage.xfinity_usage import XfinityUsage
>>> u = XfinityUsage(os.environ['XFINITY_USER'], os.environ['XFINITY_PASSWORD'], browser_name='chrome-headless')
>>> u.run()
{'units': 'GB', 'used': 553.0, 'total': 1024.0}
Note About Reliability
In short: xfinity’s site isn’t terribly reliable. Personally, I run this script twice an hour via cron, so 48 times a day, every day. I usually see 1-4 failures a day of all different failure modes - elements missing from the page, connection resets, blank pages, server-side error messages, etc. Keep that in mind. My code could probably do more in terms of error handling and retries, but it’s not that important to me.
Rationale
Comcast recently started rolling out a 1TB/month bandwidth cap in my area. I’ve gone over my two “courtesy” months, and the overage fees are pretty insane. I work from home, and sometimes that uses a lot of bandwidth. I want to know when I’m getting close to my limit; this month I’m apparently at 75% and only half way through the month, and I have no idea how that happened.
It’s entirely abusive and invasive that Comcast is injecting bandwidth warnings into my web traffic, but that’s also a pretty awful way of attempting to tell a human something - especially given how much automated traffic my computer generates. Moreover,
Xfinity’s site has a Usage Meter (which is the source of this data), but it only shows a progress bar for the month - no way to find out usage by day or hour to try and figure out what the cause actually was. Also, even if I visit the usage meter from my own computer on Xfinity’s network, using the IP address which Xfinity assigned to me (and is tracking usage for), I still need to log in to my account to view the usage. That’s a complete pain and seems to serve only to prevent customers from keeping track of their usage, not to metion preventing guests or friends from checking usage. Hell, Xfinity used to have a desktop app to track usage but it’s been shut down, and a handy script that used the same API as the desktop app no longer works as a result. With all of this put together, I’d say Comcast is going to great lengths to maximize overage fees and minimize customers’ insight into their usage.
In short, I want to be notified of my usage on a regular basis (I get daily emails with the results of this script), and I also want to be able to see historical trends (I push the output to Graphite).
Disclaimer
I have no idea what Xfinity’s terms of use for their account management website are, or if they claim to have an issue with automating access. They used to have a desktop app to check usage, backed by an API (see https://github.com/WTFox/comcastUsage ), but that’s been discontinued. The fact that they force me to login with my account credentials WHEN CONNECTING FROM THEIR NETWORK, USING THE IP ADDRESS THEY ISSUED TO MY ACCOUNT just to check my usage, pretty clearly shows me that Comcast cares a lot more about extracting the maximum overage fees from their customers than the “quality of service” that they claim these bandwidth limits exist for. So… use this at your own risk, but it seems pretty clear (i.e. discontinuing their “bandwidth meter” desktop app) that Comcast wants to prevent users from having a clear idea of their supposed bandwidth usage.
License
This package is licensed under the GNU AGPLv3.
Contributing
For information on contributing, see .github/CONTRIBUTING.md.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for xfinity_usage-2.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ffa45b966dbb4dc5ea23fabc919cf3a02e24b1aaff7a9d2ccc09f65f65e6faef |
|
MD5 | df3db626dd58438caf8fc76af1562ba4 |
|
BLAKE2b-256 | 9d4c72fdc416e8ceddb323bc4f4723b29a66abf72bbc31df18746790ba071425 |