1
0
Fork 0
cc-bandwidth/comcast-bandwidth.rb

72 lines
1.5 KiB
Ruby
Raw Permalink Normal View History

2017-05-31 00:22:44 -04:00
#!/usr/bin/ruby
require "addressable/uri"
2017-06-06 22:54:03 -04:00
require "date"
2017-05-31 00:22:44 -04:00
require "dotenv"
require "json"
require "mechanize"
Dotenv.load
agent = Mechanize.new
def continue_uri
Addressable::URI.new(
scheme: "https",
host: "login.comcast.net",
path: "/oauth/authorize",
query_values: {
redirect_uri: "https://customer.xfinity.com/oauth/callback",
client_id: "my-account-web",
state: "#/devices",
response_type: "code",
response: 1,
}
)
end
def login_uri
Addressable::URI.new(
scheme: "https",
host: "login.comcast.net",
path: "/login",
query_values: {
s: "oauth",
continue: continue_uri.to_s,
client_id: "my-account-web",
},
)
end
2017-06-06 22:54:03 -04:00
def total_hours_in_month
Date.new(Time.new.year, Time.new.month, -1).day * 24
end
def past_hours_in_month
(Time.new.day - 1) * 24 + (Time.new.hour - 1)
end
def projected_usage(current_usage)
((current_usage / past_hours_in_month) * total_hours_in_month).round(2)
end
2017-05-31 00:22:44 -04:00
page = agent.get(login_uri)
login_form = page.form("signin")
login_form.user = ENV["COMCAST_USERNAME"]
login_form.passwd = ENV["COMCAST_PASSWORD"]
agent.submit(login_form)
page = agent.get("https://customer.xfinity.com/apis/services/internet/usage")
usage = JSON.parse(page.body)
2017-06-06 22:54:03 -04:00
home_usage = usage["usageMonths"].last["homeUsage"]
allowable_usage = usage["usageMonths"].last["allowableUsage"]
details = {
home_usage: home_usage,
allowable_usage: allowable_usage,
projected_usage: projected_usage(home_usage),
}
2017-05-31 00:22:44 -04:00
2017-06-06 22:54:03 -04:00
puts details