Skip to content

Instantly share code, notes, and snippets.

@leolin310148
Created August 26, 2015 03:42
Show Gist options
  • Save leolin310148/23cd6aecba8bb3c2d372 to your computer and use it in GitHub Desktop.
Save leolin310148/23cd6aecba8bb3c2d372 to your computer and use it in GitHub Desktop.
StockPrices = new Mongo.Collection("StockPrices")
if (Meteor.isClient) {
Template.body.helpers({
stockPrices: function () {
return StockPrices.find({}, {sort: {tlong: -1}});
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
Meteor.setInterval(function () {
Meteor.http.get("http://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=tse_t00.tw&json=1&delay=0", function (err, res) {
var responseAsJson = JSON.parse(res.content)
var priceRecord = responseAsJson.msgArray[0];
var date = new Date(parseInt(priceRecord.tlong));
StockPrices.insert({
lastTradeCount: priceRecord.tv / 100.0,
totalTradeCount: priceRecord.v / 100.0,
lastTradeTimestamp: (date.getUTCHours() + 8) + ":" + date.getUTCMinutes() + ":" + date.getUTCSeconds(),
tlong: priceRecord.tlong,
lastPrice: priceRecord.z
})
});
}, 5000);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment