Created
July 4, 2011 01:01
-
-
Save aktowns/1062778 to your computer and use it in GitHub Desktop.
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
require 'httparty' | |
INTERNODE_USER = "@internode.on.net" | |
INTERNODE_PASS = "" | |
class Internode | |
include HTTParty | |
base_uri 'https://customer-webtools-api.internode.on.net/api/v1.5/' | |
def initialize creds | |
@auth = creds | |
@id = get_id | |
end | |
def get_usage(options={}) | |
options.merge!({:basic_auth => @auth}) | |
output = self.class.get("/#{@id}/usage", options)["internode"]["api"] | |
usage = output["traffic"] | |
usage_attribs = output["traffic"].attributes | |
rollover, quota = usage_attribs["rollover"], usage_attribs["quota"] | |
usage = { | |
:usage => ((usage.to_f / 1000) / 1000).to_f, | |
:quota => ((quota.to_f / 1000) / 1000).to_f, | |
:roll => Date.strptime(rollover,"%Y-%d-%m").strftime("%d-%m-%Y"), | |
:now => DateTime.now.strftime("%d-%m-%Y"), | |
:left => (Date.strptime(rollover,"%Y-%m-%d") - DateTime.now).to_i | |
} | |
usage_bar_count = ((usage[:usage].to_f / usage[:quota].to_f) * 100.0) / 10 | |
bar = "[#{'|'*usage_bar_count.to_i}#{'_'*(10-usage_bar_count.to_i)}](#{sprintf("%0.2f", usage_bar_count*10)}%)" | |
usage_left = sprintf("%0.2f", ((usage[:quota]-usage[:usage]) / 1000)) | |
per_day = sprintf("%0.2f", ((usage[:quota]-usage[:usage]) / usage[:left])) | |
"NodeUsage: #{usage[:left]} days until rollover with #{usage_left}/#{usage[:quota]/1000}GB remaining, with approx #{per_day}MB per day. #{bar}" | |
end | |
private | |
def get_id(options={}) | |
options.merge!({:basic_auth => @auth}) | |
self.class.get('',options)["internode"]["api"]["services"]["service"] | |
end | |
end | |
def usage_cb(data, buffer, args) | |
Weechat.command(buffer, @node.get_usage) | |
Weechat::WEECHAT_RC_OK | |
end | |
def weechat_init | |
@node = Internode.new(:username => INTERNODE_USER, :password => INTERNODE_PASS) | |
Weechat.register("InternodeUsage", "[email protected]", "0.2", "GPL3", "Spam a channel with your internode usage", "", "") | |
Weechat.hook_command("node", "internode usage","","","","usage_cb", "") | |
Weechat::WEECHAT_RC_OK | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment