Last active
December 20, 2016 09:35
-
-
Save Sperryfreak01/9515a4699a41668881ac to your computer and use it in GitHub Desktop.
Cox Cable internet usage scrapper. Scrapes the usage monitor page and reports the used and remaining data for the billing cycle. Sends a push notification using pushover
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The MIT License (MIT) | |
Copyright (c) [2015] [Matthew Lovett] | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
############################################################################## | |
from bs4 import BeautifulSoup | |
from requests import session | |
import urllib | |
import urllib2 | |
import urlparse | |
import json | |
############################################################################## | |
COX_username = "" | |
COX_password = "" | |
Pushover_app_token = '' | |
Pushover_user_token = '' | |
############################################################################## | |
class PushoverError(Exception): pass | |
PUSHOVER_API = "https://api.pushover.net/1/" | |
def pushover(**kwargs): | |
assert 'message' in kwargs | |
url = urlparse.urljoin(PUSHOVER_API, "messages.json") | |
data = urllib.urlencode(kwargs) | |
req = urllib2.Request(url, data) | |
try: | |
response = urllib2.urlopen(req) | |
output = response.read() | |
data = json.loads(output) | |
except urllib2.HTTPError, httperror: | |
raise PushoverError(httperror) | |
if data['status'] != 1: | |
raise PushoverError(output) | |
payload = { | |
'onsuccess' : 'https://myaccount.cox.net/internettools/datausage/usagemonth.cox', | |
'onfailure' : 'http://www.cox.com/resaccount/orangecounty/sign-in.cox', | |
'targetFN' : 'COX.net', | |
'emaildomain' : '@cox.net', | |
'username' : COX_username, | |
'password' : COX_password, | |
'rememberme' : 'on' | |
} | |
with session() as web: | |
daysleftFlag = False | |
web.post('https://idm.east.cox.net/idm/coxnetlogin', data=payload) | |
request = web.get('https://myaccount.cox.net/internettools/datausage/usagemonth.cox') | |
soup = BeautifulSoup(request.text, 'html5lib') | |
x = 0 | |
for row in soup.stripped_strings: | |
if daysleftFlag is True: | |
daysleftFlag = False | |
daysLeft = row | |
print 'Days Left %s' % row | |
if "Days Left" in row: | |
daysleftFlag = True | |
for cols in soup.findAll('td'): | |
for cell in cols: | |
if "GB" in cell: | |
x = x + 1 | |
if x == 1: | |
limit = cell.strip() | |
#print (limit) | |
print ('Monthly limit is ' + limit) | |
elif x == 2: | |
usage = cell.strip() | |
#print (usage) | |
print ('Current usage is ' +usage) | |
elif x == 3: | |
remain = cell.strip() | |
#print(remain) | |
print ('Remaining bandwidth is ' + remain) | |
pushover(message='%s of the %s month limit has been used. %s remains for the next %s days' % (usage, limit, remain, daysLeft), | |
token=Pushover_app_token, | |
user=Pushover_user_token | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment