読者です 読者をやめる 読者になる 読者になる

WHITELEAF:Kindle応援サイト

KindleでWEB小説を読もう! Narou.rb 公開中

東京電力電力供給状況APIを使ってLimeChat2に表示する Tepco.js

LimeChat2.25以上専用。
東京電力電力供給状況API を使って現在の電力量を表示します。
「停電」か「tepco」と発言すると実行出来ます。

ZIPでダウンロード (下のソースコードと同じ内容です)

使い方:
設定→スクリプトの設定→スクリプトフォルダを開く
でスクリプトフォルダを開き、tepco.js を配置します。そしてスクリプトの設定の tepco.js の欄で、有効にしたいサーバ部分で右クリックをして有効にします(○がつく)

eval(load_prototypejs_by_google());

var Tepco = {
  start: function() {
    this.crawler = new Tepco.Crawler;
  },

  command: function(prefix, channel, text) {
    if (/^(tepco|停電)$/.test(text)) {
      if (!this.crawler.latest_data) return;
      this.output(new Tepco.Console(channel));
    }
  },

  color: ["green", "green", "green", "green", "green",
          "orange", "orange", "orange",
          "red", "red", "red"],

  output: function(console) {
    var latest_data = this.crawler.latest_data;
    var usage = latest_data.usage;
    var capacity = latest_data.capacity;
    var ratio = usage / capacity * 100;
    var message = "[";
    for (var i = 0; i < 10; i++) {
      var two_digit = Math.floor(ratio / 10);
      var one_digit = Math.floor(ratio - two_digit * 10);
      var color = this.color[i];
      message += "<color " + color + ">";
      if (i < two_digit) {
        message += "|||";
      }
      else if (two_digit == i) {
        for (var k = 0; k < 3; k++) {
          if (k * 3.3 < one_digit) {
            message += "|";
          }
          else {
            message += " ";
          }
        }
      }
      else {
        message += "   ";
      }
      message += "<stop>";
    }
    message += Math.floor(ratio) + "%] (" + usage + "/" + capacity + "万kW) ";
    message += (latest_data.saving ? "<bold>停電実施中<stop>" : "<color silver>停電未実施<stop>");
    console.privmsg(message);
  }
};

Tepco.Crawler = Class.create({
  tepco_api_host: "http://tepco-usage-api.appspot.com/",
  api_latest: "latest.json",
  latest_data: null,
  interval: 10 * 60 * 1000,

  initialize: function() {
    this.start_crawl();
    this.crawl();
  },

  start_crawl: function() {
    this.timer_id = setInterval(this.crawl.bind(this), this.interval);
  },

  stop_crawl: function() {
    if (this.timer_id) {
      clearInterval(this.timer_id);
      this.timer_id = null;
    }
  },

  crawl: function() {
    var api_url = this.tepco_api_host + this.api_latest;
    new Ajax.Request(api_url + "?" + (new Date).getTime().toString(), {
      method: "get",
      onComplete: function(transport) {
        this.latest_data = transport.responseText.evalJSON();
      }.bind(this),
      onFailure: function(transport) {
        log("[Tepco] 受信出来ませんでした");
      },
      onException: function(transport, exception) {
        log("[Tepco] エラーが発生しました(" + exception.message + ")");
        this.stop_crawl();
      }.bind(this)
    });
  }
});

Tepco.Console = Class.create({
  initialize: function(channel) {
    this.channel = channel;
  },

  send: function(message) {
    send(this.channel, message);
  },

  action: function(message) {
    action(this.channel, message);
  },

  privmsg: function(message) {
    sendRaw("privmsg " + this.channel + " " + message);
  },

  information: function(message) {
    this.send("[Rosv] " + message);
  }
});

function event::onLoad() {
  Tepco.start();
}

function event::onChannelText(prefix, channel, text) {
  Tepco.command(prefix, channel, text);
}

function load_script(filename) {
  var xhr;
  if (filename.match(/^https?:/)) {
    if (xhr = new ActiveXObject("Microsoft.XMLHTTP")) {
      xhr.open("GET", filename, false);
      xhr.send("");
      return xhr.responseText;
    }
  }
  else {
    var f = openFile(userScriptPath + "\\" + filename);
    var res = "";
    if (f) {
      var res = f.readAll();
      f.close();
    }
    else {
      log(filename + " is not found");
    }
    return res;
  }
}

function load_prototypejs_by_google() {
  return "var document={getElementsByTagName:function(){return[{src:''}];}," +
         "createElement:function(){return{appendChild:function(){}};}," +
         "createTextNode:function(){},createEvent:function(){return{__proto__:{}};}," +
         "getElementById:function(){return{};},write:function(){},domain:'localhost'};" +
         "var window={document:document,attachEvent:function(){},setTimeout:function(a,b){setTimeout(a,b);}};" +
         "var navigator={userAgent:'LimeChat'};var location={protocol:'http:',port:''};" +
         load_script("http://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js").substr(0, 46965);
}