Created
August 18, 2015 22:01
-
-
Save junajan/20ccbc95cae78dedd859 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
var request = require('request'); | |
var cheerio = require('cheerio'); | |
var Tickers = function(app) { | |
const T_NAME = 'watchlist'; | |
const WIKI_SP100_LIST_URL = 'http://en.wikipedia.org/wiki/S%26P_100'; | |
const BARCHART_SP100_LIST_URL = 'http://www.barchart.com/stocks/sp100.php?_dtp1=0'; | |
var self = this; | |
var data = []; | |
var DB = app.get("db"); | |
var Log = require("./Log")(app, "TICK"); | |
self.getSP100Wiki = function(cb) { | |
console.log("Downloading from ... ", WIKI_SP100_LIST_URL); | |
request(WIKI_SP100_LIST_URL, function(err, res, body) { | |
var data = []; | |
if(err) | |
return cb(err); | |
body = body.replace(/[\n\t]*/g, '').match(/<table.*?><tr><th>Symbol<\/th><th>Name<\/th>.*?<\/table>/g); | |
if(!body[0]) | |
return cb("Page is invalid"); | |
var $ = cheerio.load(body[0]); | |
$('tr').each(function(i, tr) { | |
var ticker = $(this).find("td:first-child").text(); | |
var name = $(this).find("td:nth-child(2) a").text(); | |
if(ticker) | |
data.push({ticker: ticker, name: name}); | |
}); | |
console.log("Loaded data count.. ", Object.keys(data).length); | |
cb(err, data); | |
}); | |
}; | |
self.getSP100Barchart = function(cb) { | |
request(BARCHART_SP100_LIST_URL, function(err, res, body) { | |
if(err) | |
return cb(JSON.stringify(err)); | |
var data = []; | |
var $ = cheerio.load(body); | |
var list = $('#frmFlipCharts input[name=symbols]'); | |
if(!list) | |
return cb("Page is invalid"); | |
$('#dt1 tbody tr').each(function(i, tr) { | |
var ticker = $(this).find("td:first-child").text(); | |
var name = $(this).find("td:nth-child(2)").text();; | |
if(ticker) | |
data.push({ticker: ticker, name: name}); | |
data.push({ticker: ticker, name: name}); | |
}); | |
cb(err, data); | |
}); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment