forked from drbeep/yahoo_datafeed
-
Notifications
You must be signed in to change notification settings - Fork 77
/
yahoo.js
56 lines (43 loc) · 1.43 KB
/
yahoo.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
/*
This file is a node.js module.
This is a sample implementation of UDF-compatible datafeed wrapper for Quandl (historical data) and yahoo.finance (quotes).
Some algorithms may be incorrect because it's rather an UDF implementation sample
then a proper datafeed implementation.
*/
/* global require */
/* global console */
/* global process */
"use strict";
var http = require("http"),
url = require("url"),
symbolsDatabase = require("./symbols_database"),
RequestProcessor = require("./request-processor").RequestProcessor;
var requestProcessor = new RequestProcessor(symbolsDatabase);
// Usage:
// /config
// /symbols?symbol=A
// /search?query=B&limit=10
// /history?symbol=C&from=DATE&resolution=E
var firstPort = process.env.YAHOO_PORT || 8888;
function getFreePort(callback) {
var port = firstPort;
firstPort++;
var server = http.createServer();
server.listen(port, function (err) {
server.once('close', function () {
callback(port);
});
server.close();
});
server.on('error', function (err) {
getFreePort(callback);
});
}
getFreePort(function(port) {
http.createServer(function(request, response) {
var uri = url.parse(request.url, true);
var action = uri.pathname;
return requestProcessor.processRequest(action, uri.query, response);
}).listen(port);
console.log("Datafeed running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");
});