From ef0b6b4f86f1938515c7d39f72e7b97f3f4448f8 Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Fri, 17 Feb 2017 21:55:50 -0800 Subject: [PATCH] Check comcast bandwidth --- cgi-bin/comcast.env.sample | 2 + cgi-bin/comcast.py | 114 +++++++++++++++++++++++++++++++ cgi-bin/service.status.d/comcast | 2 + 3 files changed, 118 insertions(+) create mode 100644 cgi-bin/comcast.env.sample create mode 100644 cgi-bin/comcast.py create mode 100644 cgi-bin/service.status.d/comcast diff --git a/cgi-bin/comcast.env.sample b/cgi-bin/comcast.env.sample new file mode 100644 index 0000000..cbf6386 --- /dev/null +++ b/cgi-bin/comcast.env.sample @@ -0,0 +1,2 @@ +COMCAST_USERNAME=bob +COMCAST_PASSWORD=hope diff --git a/cgi-bin/comcast.py b/cgi-bin/comcast.py new file mode 100644 index 0000000..06d395f --- /dev/null +++ b/cgi-bin/comcast.py @@ -0,0 +1,114 @@ +#!/usr/bin/python3 +from __future__ import print_function +# Source: https://github.com/lachesis/comcast + +import json +import logging +import os +import re +import requests +import time + +logger = logging.getLogger(__name__) +logging.basicConfig(level=logging.DEBUG) +logging.getLogger('requests').setLevel(logging.ERROR) + +session = requests.Session() + +username = os.environ['COMCAST_USERNAME'] +password = os.environ['COMCAST_PASSWORD'] + +logger.debug("Finding req_id for login...") +res = session.get('https://login.comcast.net/login?r=comcast.net&s=oauth&continue=https%3A%2F%2Flogin.comcast.net%2Foauth%2Fauthorize%3Fclient_id%3Dmy-account-web%26redirect_uri%3Dhttps%253A%252F%252Fcustomer.xfinity.com%252Foauth%252Fcallback%26response_type%3Dcode%26state%3D%2523%252Fdevices%26response%3D1&client_id=my-account-web') +assert res.status_code == 200 +m = re.search(r'', res.text) +req_id = m.group(1) +logger.debug("Found req_id = %r", req_id) + +data = { + 'user': username, + 'passwd': password, + 'reqId': req_id, + 'deviceAuthn': 'false', + 's': 'oauth', + 'forceAuthn': '0', + 'r': 'comcast.net', + 'ipAddrAuthn': 'false', + 'continue': 'https://login.comcast.net/oauth/authorize?client_id=my-account-web&redirect_uri=https%3A%2F%2Fcustomer.xfinity.com%2Foauth%2Fcallback&response_type=code&state=%23%2Fdevices&response=1', + 'passive': 'false', + 'client_id': 'my-account-web', + 'lang': 'en', +} + +logger.debug("Posting to login...") +res = session.post('https://login.comcast.net/login', data=data) +assert res.status_code == 200 + +logger.debug("Preloader HTML...") +res = session.get('https://customer.xfinity.com/Secure/Preloading/?backTo=%2fMyServices%2fInternet%2fUsageMeter%2f') +assert res.status_code == 200 + +logger.debug("Preloader AJAX...") +res = session.get('https://customer.xfinity.com/Secure/Preloader.aspx') +assert res.status_code == 200 + +logger.debug("Waiting 5 seconds for preloading to complete...") +time.sleep(5) + +logger.debug("Fetching internet usage HTML...") +res = session.get('https://customer.xfinity.com/MyServices/Internet/UsageMeter/') +assert res.status_code == 200 +html = res.text + +# Example HTML: +#
+# +#

Home Internet Usage

+#
+#
+# +# 222GB of 1024GB +# +#
+#
+# +# 222GB of 1024GB +# +# +# +#
+#
+ +used = None +m = re.search(r'', html) +if m: + total = int(m.group(1)) + +unit = None +m = re.search(r'
total: + exit(1) +else: + exit(0) diff --git a/cgi-bin/service.status.d/comcast b/cgi-bin/service.status.d/comcast new file mode 100644 index 0000000..a5bcbc0 --- /dev/null +++ b/cgi-bin/service.status.d/comcast @@ -0,0 +1,2 @@ +set +x +exec env -i $(xargs -a comcast.env) python2 comcast.py -- 2.47.3