forked from ntalekt/Cox_Data_Usage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcox_usage.py
67 lines (54 loc) · 1.95 KB
/
cox_usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python
# cox_usage.py
# Script designed to get Cox Communications internet
# usage data into a JSON format for Home Assistant.
#
# Version 1.0
# Original Author : Rick Rocklin
# Original Date : 10/02/2017
#
# 10/17/2017: Output to file
# 11/02/2017: Updated to use mechanicalsoup
import mechanicalsoup
import re
import json
import os
# URL that we authenticate against
login_url = "https://www.cox.com/resaccount/sign-in.cox"
# URL that we grab all the data from
stats_url = "https://www.cox.com/internet/mydatausage.cox"
# Your cox user account (e.g. [email protected]) and password
cox_user = "username"
cox_pass = "password"
home = os.getenv('HOME', '/home/homeassistant')
json_file = f"{home}/.homeassistant/cox_usage.json"
# Setup browser
browser = mechanicalsoup.StatefulBrowser(
soup_config={'features': 'lxml'},
user_agent='Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13',
)
#Disable SSL verification workaround for issue #2
browser.session.verify = False
# Open the login URL
login_page = browser.get(login_url)
# Similar to assert login_page.ok but with full status code in case of failure.
login_page.raise_for_status()
# Find the form named sign-in
login_form = mechanicalsoup.Form(
login_page.soup.select_one('form[name="sign-in"]'))
# Specify username and password
login_form.input({'username': cox_user, 'password': cox_pass})
# Submit the form
browser.submit(login_form, login_page.url)
# Read the stats URL
stats_page = browser.get(stats_url)
# Grab the script with the stats in it
stats = stats_page.soup.findAll(
'script', string=re.compile('utag_data'))[0].string
# Split and RSplit on the first { and on the last } which is where the data object is located
jsonValue = '{%s}' % (stats.split('{', 1)[1].rsplit('}', 1)[0],)
# Load into json
data = json.loads(jsonValue)
# Print JSON to file
with open(json_file, 'w+') as outfile:
json.dump(data, outfile, sort_keys=True)