Created
February 23, 2018 17:46
-
-
Save rage311/a1754b183c4703db842f7ade08c9d5a5 to your computer and use it in GitHub Desktop.
Get your Comcast data usage stats for the month and (optionally) display it in i3blocks
This file contains 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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use 5.024; | |
use Mojo::UserAgent; | |
use DateTime; | |
# User configurables | |
use constant { | |
USER => 'YourUserName', | |
PASSWORD => 'YourPassword', | |
# For determining current date/time to calculate percent of period elapsed. | |
# time zone string, eg: 'America/Denver', 'America/Los_Angeles' | |
TIME_ZONE => 'America/Denver', | |
}; | |
# Comcast static URLs | |
use constant { | |
URL_LOGIN => 'https://login.comcast.net/login', | |
# required for full "authentication" to happen | |
URL_OAUTH => 'https://login.comcast.net/oauth/authorize', | |
URL_USAGE_API => 'https://customer.xfinity.com/apis/services/internet/usage', | |
}; | |
my $ua = Mojo::UserAgent->new->max_redirects(5); | |
$ua->post(URL_LOGIN, form => { user => USER, passwd => PASSWORD })->result; | |
$ua->get( | |
URL_OAUTH, | |
form => { | |
client_id => 'my-account-web', | |
redirect_uri => 'https://customer.xfinity.com/oauth/callback', | |
response_type => 'code', | |
}, | |
)->result; | |
my $usage = $ua->get(URL_USAGE_API)->result->json; | |
# for i3blocks | |
my $full_text = 'error'; | |
my $color; | |
if ($usage->{error}) { | |
$color = '#FF0000'; | |
} else { | |
my $current_usage = $usage->{usageMonths}[-1]; | |
my $used = $current_usage->{homeUsage}; | |
my $allowable = $current_usage->{allowableUsage}; | |
my $units = $current_usage->{unitOfMeasure}; | |
my $now = DateTime->now(time_zone => TIME_ZONE); | |
my $last_day_of_month = DateTime->new( | |
year => $now->year, | |
month => $now->month, | |
day => 1, | |
time_zone => TIME_ZONE, | |
)->add(months => 1) | |
->subtract(days => 1) | |
->day; | |
if (defined $used && defined $allowable) { | |
$full_text = "$used$units / $allowable$units"; | |
if ($allowable > 0 && $used / $allowable > 0.95) { | |
$color = "#DD0000"; | |
} elsif ($used / $allowable > $now->day / $last_day_of_month) { | |
$color = "#FFFFFF"; | |
} | |
} | |
} | |
# i3blocks specific output | |
say $full_text; | |
say $full_text; | |
say $color if defined $color; | |
__DATA__ | |
# Sample data | |
\ { | |
additionalBlocksUsed 0, | |
additionalCostPerBlock 10, | |
additionalIncluded 0, | |
additionalPercentUsed 0, | |
additionalRemaining 0, | |
additionalUnitsPerBlock 50, | |
additionalUsed 0, | |
allowableUsage 1024, | |
billableOverage 0, | |
currentCreditAmount 0, | |
devices [ | |
[0] { | |
id "AA:BB:CC:DD:EE:FF", | |
usage 222 | |
} | |
], | |
endDate "06/30/2017", | |
homeUsage 222, | |
maxCreditAmount 0, | |
overageCharges 0, | |
overageUsed 0, | |
policy "limited", | |
policyName "1 Terabyte Data Plan", | |
startDate "06/01/2017", | |
unitOfMeasure "GB" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment