Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@autch
Last active January 15, 2016 03:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save autch/1077689 to your computer and use it in GitHub Desktop.
Save autch/1077689 to your computer and use it in GitHub Desktop.
谷岡Stooqテキスト版の代わりに jQuery + YQL + Stooq CSV で数値を表示。使い方はページ下部のコメント参照。 http://jsdo.it/autch/egw6
<!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>谷岡 Stooq jQuery 版</title>
<script type="text/javascript">
// コンストラクタ
// @param symbols 取得すべき銘柄の文字列配列。取得結果はこの配列の順番で表示する。
// Stooq でその銘柄を表示した時に URL に現れる文字列でなければならない。
// たとえば KOSPI は ^kospi と書かなければならない。
// @param target_element 取得結果を表示させる DOM オブジェクト。
// this.show() では val() で値を設定しているので、textarea があてはまる。
// @param done_callback 取得結果の表示が完了してから呼ばれるコールバック。
// 自動リロードの設定とかする。
var StooqScraper = function(symbols, target_element, done_callback) {
this.initialize(symbols, target_element, done_callback);
};
StooqScraper.prototype = {
symbols: [ 'usdkrw', '^kospi' ],
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=?',
target_element: {},
initialize: function(symbols, target_element, done_callback) {
this.symbols = symbols || this.symbols;
this.target_element = target_element || {};
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.show();
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;
},
// 銘柄ごとの取得結果を集めてテキストエリアへ挿入する。
show: function() {
var value = "";
for(key in this.symbols) {
var sym = this.symbols[key];
var line = this[sym];
value += line + "\n";
}
$(this.target_element).val(value);
},
zero_prefix: function(n) {
return ((n < 10) ? "0" : "") + n;
},
// 取得結果 row について、結果をテキスト化して返す
// ポーランド標準時からローカル時間(たぶん日本標準時)への変換を行う。
format_item: function(row) {
var line;
var cest, jst;
var jst_milli;
var datetime;
// CEST = UTC+2, JST-7
cest = new Date(row.Date + ' ' + row.Time);
jst_milli = cest.getTime() + (-cest.getTimezoneOffset() - (2 * 60)) * 60000;
jst = new Date();
jst.setTime(jst_milli);
datetime = this.zero_prefix(jst.getHours()) + ':' +
this.zero_prefix(jst.getMinutes()) + ':' +
this.zero_prefix(jst.getSeconds());
line = row.Symbol + ": " + new Number(row.Close).toFixed(2) + " (" + datetime + " JST)";
return line;
},
// 銘柄 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, me.format_item(row));
});
},
start: function() {
for(key in this.symbols) {
var sym = this.symbols[key];
this.get_value(sym);
}
}
};
function oneshot()
{
$('#value').val("Loading...");
var scraper = new StooqScraper(null, $('#value'));
scraper.start();
}
jQuery(document).ready(function($) {
oneshot();
window.setInterval(oneshot, 60000);
});
</script>
</head>
<body>
<textarea rows="3" cols="40" id="value"
onclick="focus(); select();"
readonly="readonly">Loading...</textarea>
<button onclick="oneshot()">Reload</button>
<div><a href="https://gist.github.com/1077689">最新版</a></div>
</body>
</html>
@autch
Copy link
Author

autch commented Jul 22, 2011

jQuery から米 Yahoo! の YQL サービスと Stooq の CSV データを使っているので、ローカルのファイルだけで動く。 がいらない。

使い方

  1. このソースコードをメモ帳かなんかにコピペする
  2. UTF-8 エンコーディングを指定してデスクトップにでも保存する
  3. 保存したファイルをブラウザで開く

UTF-8 エンコーディングを指定できないエディタの時は、4 行目の UTF-8Shift_JIS とかに直す。

銘柄を増やしたい時は、22 行目の symbols という配列に銘柄名を追加する。取得結果はこの配列の順番で表示する。ただし、Stooq でその銘柄を表示した時に URL に現れる文字列でなければならない。たとえば KOSPI は ^kospi と書かなければならない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment