Skip to content

Instantly share code, notes, and snippets.

@autch
Created July 26, 2011 01:36
Show Gist options
  • Save autch/1105730 to your computer and use it in GitHub Desktop.
Save autch/1105730 to your computer and use it in GitHub Desktop.
習慣^H^H週刊韓国経済をヲンで買う。レートはStooqからその場で取得。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<title>習慣^H^H週刊韓国経済をヲンで買う</title>
<style type="text/css">
input { text-align: right; }
</style>
<script type="text/javascript">
// https://gist.github.com/1077689 の朴李
// コンストラクタ
// @param symbols 取得すべき銘柄の文字列配列。取得結果はこの配列の順番で表示する。
// Stooq でその銘柄を表示した時に URL に現れる文字列でなければならない。
// たとえば KOSPI は ^kospi と書かなければならない。
// @param done_callback 取得結果の表示が完了してから呼ばれるコールバック。
// 自動リロードの設定とかする。
var StooqScraper = function(symbols, done_callback) {
this.initialize(symbols, done_callback);
};
StooqScraper.prototype = {
symbols: [ 'usdkrw', 'usdjpy' ],
clean_query: "SELECT * FROM csv WHERE url='http://stooq.com/q/l/?s=%s&f=sd4t2ohlc&h&e=csv' AND columns='Symbol,Date,Time,Open,High,Low,Close'",
yql_url: 'http://query.yahooapis.com/v1/public/yql?callback=?',
initialize: function(symbols, done_callback) {
this.symbols = symbols || this.symbols;
this.done = done_callback || function() {};
},
// すべての銘柄について取得と表示が完了してから呼ばれる
// ユーザ定義のコールバック
done: function() {
},
// 銘柄 key の取得結果 line を this に保存する。
// this.symbols に指定されたすべての銘柄を取得し終わったら
// this.show() と this.done() を呼ぶ。
okay: function(key, line) {
this[key] = line;
if(this.is_complete()) {
this.done();
}
},
// this.symbols で指定されたすべての銘柄が取得できたかどうかチェックして真偽値を返す。
is_complete: function() {
for(key in this.symbols) {
var sym = this.symbols[key];
if(typeof this[sym] == "undefined") {
return false;
}
}
return true;
},
// 銘柄 symbol について取得を開始する
// 取得結果はテキストにして this に保存しておく。
// this.symbols で指定されたすべての銘柄を取得し終わったら、テキストエリアへ反映する
get_value: function(symbol) {
var real_query = this.clean_query.replace(/%s/, encodeURIComponent(symbol));
var me = this;
$.getJSON(this.yql_url, {
q: real_query,
format: "json",
}, function(data) {
var row = data.query.results.row[1];
me.okay(symbol, parseFloat(row.Close));
});
},
start: function() {
for(key in this.symbols) {
var sym = this.symbols[key];
this.get_value(sym);
}
}
};
function on_submit()
{
var scraper = new StooqScraper(null, function() {
$('#usdkrw').val(new Number(scraper['usdkrw']).toFixed(2));
$('#usdjpy').val(new Number(scraper['usdjpy']).toFixed(2));
var yen = parseFloat($('#yen').val());
var r = yen / scraper['usdjpy'] * scraper['usdkrw'];
$('#result').val(new Number(r).toFixed(2));
});
scraper.start();
}
</script>
</head>
<body onload="on_submit()">
<h3><s>習慣</s>週刊韓国経済をヲンで買う</h3>
税込<input id="yen" value="980" size="5" maxlength="6" />円
÷<input id="usdjpy" size="8" value="" readonly="readonly"/>円/$
×<input id="usdkrw" size="8" value="" readonly="readonly"/>ヲン/$
=<input id="result" size="10" value="" readonly="readonly"/>ヲン
<button onclick="on_submit()">いくらニカ?</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment