forked from DirtNap/stock-quotes-and-recco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (55 loc) · 2.04 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
var requestPromise = require('request-promise');
var superagent = require('superagent');
//Routes
app.use('/', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get('/:symbol', function (req, res) {
var symbol=req.params.symbol;
var options = {
uri: 'http://finance.yahoo.com/webservice/v1/symbols/'+symbol+'/quote?format=json&view=detail',
json:true
};
requestPromise(options)
.then(function (resp){
var relevantStockResources = resp.list.resources.filter(function(resource){
return resource.resource.fields.symbol==symbol;}).map(function(resource){
return {symbol:resource.resource.fields.symbol, price:resource.resource.fields.price, issuer:resource.resource.fields.issuer_name};
})
res.send(relevantStockResources[0]);
})
.catch(function (err) {
console.log(err);
res.send('Http request to yahoo threw error');
})
});
app.get('/:symbol/historical', function (req, res) {
var symbol=req.params.symbol;
superagent.get('http://query.yahooapis.com/v1/public/yql')
.query({//q:'SELECT * FROM yahoo.finance.historicaldata WHERE symbol='+symbol+' AND startDate="2015-01-01" AND endDate="2015-12-31";',
q:'SELECT * FROM yahoo.finance.historicaldata WHERE symbol in ("'+symbol+'") AND startDate="2014-12-31" AND endDate="2015-12-31";',
env:'store://datatables.org/alltableswithkeys',
format:'json'})
.end(function(err, response){
if(err){
res.send({error:err});
}
else{
if(JSON.parse(response.text).query.results){
var graphData = JSON.parse(response.text).query.results.quote.map(singleQuote=>(singleQuote.Close))
res.send(graphData.reverse());}
else{
console.log("no quote info. Response was: ", response.text);
res.send([]);
}
}
})
});
var server = app.listen(8080, function () {
var port = server.address().port;
console.log('Example app available at: http://localhost:%s', port);
});