I got no response from the Node.js Bloomberg API package (blpapi) - node.js

I'm working on a Node.js project and I need to price some stocks with the Bloomberg API. I found out that there's an NPM package for this API, so I had it installed and started testing it according to https://github.com/bloomberg/blpapi-node but I'm getting no responses.
This is my code:
var blpapi = require('blpapi');
var bloombergPricing = function ()
{
var session = new blpapi.Session({ host: '127.0.0.1', port: 8194 });
session.on('SessionStarted', function(m) {
console.log('bonjou');
session.openService('//blp/mktdata', 1);
});
var securities = [
{ security: 'AAPL US Equity', correlation: 0, fields: ['LAST_TRADE'] },
{ security: 'GOOG US Equity', correlation: 1, fields: ['LAST_TRADE'] }
];
session.on('ServiceOpened', function(m) {
console.log(session);
if (m.correlations[0].value == service_id) {
console.log(session);
session.subscribe(securities);
}
});
session.on('MarketDataEvents', function(m) {
if (m.data.hasOwnProperty('LAST_TRADE')) {
console.log(securities[m.correlations[0].value].security,
'LAST_TRADE', m.data.LAST_TRADE);
}
});
}
Is this package still working? If not, how is it possible to call the Java Bloomberg API from Node.js?
Thank you very much.

I think you're missing a session.start(); at the end of the function. This will trigger off the connection.
edit to include code that works for me:
var blpapi = require('blpapi');
var bloombergPricing = function ()
{
var session = new blpapi.Session({ host: '127.0.0.1', port: 8194 });
session.on('SessionStarted', function(m) {
console.log('bonjou');
session.openService('//blp/mktdata', 1);
});
var securities = [
{ security: 'AAPL US Equity', correlation: 0, fields: ['LAST_TRADE'] },
{ security: 'GOOG US Equity', correlation: 1, fields: ['LAST_TRADE'] }
];
session.on('ServiceOpened', function(m) {
console.log(session);
if (m.correlations[0].value == 1) {
console.log(session);
session.subscribe(securities);
}
});
session.on('MarketDataEvents', function(m) {
if (m.data.hasOwnProperty('LAST_TRADE')) {
console.log(securities[m.correlations[0].value].security,
'LAST_TRADE', m.data.LAST_TRADE);
}
});
session.start();
}
bloombergPricing();

Related

(nodegit) How to read/write from/to private Github repo using a token

I copied some code from the net and got it working to clone and push to one of my own repos on github. Using the same code I tried to do the same with a repo now owned by me but by an organisation that I am part of and that I have write access to. This fails with a 403.
Here's the code I use.
GithubClient.js
var fs = require('fs');
var NodeGit = require('nodegit');
var config = require('./github-config.js');
var cloneOpts = {
callbacks: {
certificateCheck: () => {
return 1;
},
credentials: function(url, username) {
console.log('creds for' + username);
return nodegit.Cred.userpassPlaintextNew(config.token, "x-oauth-basic");
}
}
};
cloneOpts.fetchOpts = {
callbacks: cloneOpts.callbacks
};
class GitClient {
static clone(options) {
cloneOpts.checkoutBranch = options.branch;
return NodeGit.Clone(options.remote, options.local, cloneOpts)
.catch(err => console.log(err));
}
static addAndCommit(repo, filesArray, msg) {
return repo.createCommitOnHead(
filesArray,
NodeGit.Signature.create(config.realname, config.email, new Date().getTime(), 0),
NodeGit.Signature.create(config.realname, config.email, new Date().getTime(), 0),
msg
);
}
static push(repo, repoName, remoteName, refs) {
return repo.getRemote(remoteName || 'origin')
.then(remote => {
return remote.push(
refs || [`refs/heads/${repoName || 'main'}:refs/heads/${repoName || 'main'}`],
cloneOpts
).then(function() {
console.log('success', arguments);
});
});
}
}
module.exports = GitClient;
GithubClientRunner.js
var fs = require('fs');
var GitClient = require('./GithubClient.js');
var config = require('./github-config.js');
let repo;
console.log('starting clone');
return GitClient.clone({
branch: config.branch,
remote: config.remote,
local: config.local
}).then(r => {
repo = r;
let contents = new Date().getTime() + ' please ignore - will remove soon';
console.log('got repo, writing contents:', contents);
fs.writeFileSync(config.local + '/myTemporaryTestFile', contents);
// add the file
return GitClient.addAndCommit(repo, ['myTemporaryTestFile'], 'Test Commit not breaking anything');
}).then(() => {
console.log('commit done, pushing');
return GitClient.push(repo, config.branch);
})
.then(() => {
console.log('done');
process.exit(0);
})
.catch(err => {
console.error('uncaught!', err);
process.exit(1);
});
This is the config for my own repo (working):
const config = {
branch: 'main',
remote: 'https://___MY_GITHUB_TOKEN___:x-oauth-basic#github.com/MyGitHubUserName/myTestRepo.git',
local: './.gitcache/myTestRepo.io',
username: 'MyGitHubUserName',
realname: 'My Name',
email: 'me#email.com',
token: '___MY_GITHUB_TOKEN___'
};
module.exports = config;
The outcome is as follows:
starting clone
got repo, writing contents: 1612971904692 please ignore - will remove soon
commit done, pushing
success [Arguments] { '0': undefined }
done
And now this is the config for the organisations repo (not working):
const config = {
branch: 'main',
remote: 'https://___MY_GITHUB_TOKEN___:x-oauth-basic#github.com/theOrganisationIAmAMemberOf/someOtherRepo.git',
local: './.gitcache/someOtherRepo.io',
username: 'MyGitHubUserName',
realname: 'My Name',
email: 'me#email.com',
token: '___MY_GITHUB_TOKEN___'
};
module.exports = config;
The outcome now is:
starting clone
[Error: unexpected HTTP status code: 403] {
errno: -1,
errorFunction: 'Clone.clone'
}
How can I make the code work with both types of repos?
Thank you!
I found the mistake myself. The code is working properly as expected.
However I forgot the enable my token for the organisation. As soon as I did that it all worked just fine.
Pro tip: if you encounter an authentication problem and the error message doesn't help try to achieve the same thing using the command line git client. Error messages there will be much more informative.

Moving data from nodejs console to SQL Server

I am very new to node.js. Using the following code I am able to retrive data from the wesbite intrino into console.
var https = require("https");
var username = "********";
var password = "********";
var auth = "Basic " + new Buffer(username + ':' +
password).toString('base64');
var request = https.request({
method: "GET",
host: "api.intrinio.com",
path: "/companies?ticker=AAPL",
headers: {
"Authorization": auth
}
}, function (response) {
var json = "";
response.on('data', function (chunk) {
json += chunk;
});
response.on('end', function () {
var company = JSON.parse(json);
console.log(company);
});
});
request.end();
And the result is as follows:-
My question is: how do I transfer this data into SQL Server? I tried watching a few tutorials and videos. But I was not able to exactly understand how it works.
Thanks in advance.
This is a very broad question. Connecting MS-SQL server from node usually uses tedious package. You can directly use this package to insert data as described in https://learn.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-using-node-js
Following is a snippet from the above link.
var Connection = require('tedious').Connection;
var config = {
userName: 'yourusername',
password: 'yourpassword',
server: 'yourserver.database.windows.net',
// If you are on Azure SQL Database, you need these next options.
options: {encrypt: true, database: 'AdventureWorks'}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
executeStatement1();
});
var Request = require('tedious').Request
var TYPES = require('tedious').TYPES;
function executeStatement1() {
request = new Request("INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES (#Name, #Number, #Cost, #Price, CURRENT_TIMESTAMP);", function(err) {
if (err) {
console.log(err);}
});
request.addParameter('Name', TYPES.NVarChar,'SQL Server Express 2014');
request.addParameter('Number', TYPES.NVarChar , 'SQLEXPRESS2014');
request.addParameter('Cost', TYPES.Int, 11);
request.addParameter('Price', TYPES.Int,11);
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
console.log("Product id of inserted item is " + column.value);
}
});
});
connection.execSql(request);
}
But using an ORM is the best practice. sequelize is one of the best in the node community.
$ npm install --save sequelize
$ npm install --save tedious // MSSQL
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'mssql'
});
You must go through either of these modules documentation to understand better.

Alexa Test Response does not contain outputSpeech

I'm new to Alexa, and have followed the airportinfo tutorial and I have copied the code from github https://github.com/bignerdranch/alexa-airportinfo and when i test it using npm and input an airport code e.g. SFO, Theres no "outputSpeech:" and i tried making a similar skill with the same issue, I'm not sure what I'm doing wrong. I have both index.js and FAADataInfo.js Thanks in advance for your help.
This is the index.js file
'use strict';
module.change_code = 1;
var _ = require('lodash');
var Alexa = require('alexa-app');
var skill = new Alexa.app('airportinfo');
var FAADataHelper = require('./faa_data_helper');
skill.launch(function(req, res) {
var prompt = 'For delay information, tell me an Airport code.';
res.say(prompt).reprompt(prompt).shouldEndSession(false);
});
skill.intent('airportInfoIntent', {
'slots': {
'AIRPORTCODE': 'FAACODES'
},
'utterances': [
'{|flight|airport} {|delay|status} {|info} {|for} {-|AIRPORTCODE}'
]
},
function(req, res) {
var airportCode = req.slot('AIRPORTCODE');
var reprompt = 'Tell me an airport code to get delay information.';
if (_.isEmpty(airportCode)) {
var prompt = 'I didn\'t hear an airport code. Tell me an airport code.';
res.say(prompt).reprompt(reprompt).shouldEndSession(false);
return true;
} else {
var faaHelper = new FAADataHelper();
console.log(airportCode);
faaHelper.getAirportStatus(airportCode).then(function(airportStatus) {
console.log(airportStatus);
res.say(faaHelper.formatAirportStatus(airportStatus)).send();
}).catch(function(err) {
console.log(err.statusCode);
var prompt = 'I didn\'t have data for an airport code of ' +
airportCode;
res.say(prompt).reprompt(reprompt).shouldEndSession(false).send();
});
return false;
}
}
);
module.exports = skill;
and heres FAADataInfo.js
'use strict';
var _ = require('lodash');
var requestPromise = require('request-promise');
var ENDPOINT = 'http://services.faa.gov/airport/status/';
function FAADataHelper() {
}
FAADataHelper.prototype.getAirportStatus = function(airportCode) {
var options = {
method: 'GET',
uri: ENDPOINT + airportCode,
json: true
};
return requestPromise(options);
};
FAADataHelper.prototype.formatAirportStatus = function(aiportStatusObject) {
if (aiportStatusObject.delay === 'true') {
var template = _.template('There is currently a delay for ${airport}. ' +
'The average delay time is ${delay_time}.');
return template({
airport: aiportStatusObject.name,
delay_time: aiportStatusObject.status.avgDelay
});
} else {
//no delay
var template =_.template('There is currently no delay at ${airport}.');
return template({
airport: aiportStatusObject.name
});
}
};
module.exports = FAADataHelper;
This is the response that I get
{
"version": "1.0",
"response": {
"directives": [],
"shouldEndSession": true
},
"sessionAttributes": {},
"dummy": "text"
}
The alexa-app version that the tutorial is using is out of date. When using the latest alexa-app npm version (4.0.0), the return value for the .intent() function should be a Promise and not a boolean if you are running asynchronous functions.
In your index.js, add:
return faaHelper.getAirportStatus(....) {}.catch(){}
and remove the return false; after the catch.
Here's the full skill.intent() code
skill.intent('airportInfoIntent', {
'slots': {
'AIRPORTCODE': 'FAACODES'
},
'utterances': [
'{|flight|airport} {|delay|status} {|info} {|for} {-|AIRPORTCODE}'
]
},
function(req, res) {
var airportCode = req.slot('AIRPORTCODE');
var reprompt = 'Tell me an airport code to get delay information.';
if (_.isEmpty(airportCode)) {
var prompt = 'I didn\'t hear an airport code. Tell me an airport code.';
res.say(prompt).reprompt(reprompt).shouldEndSession(false);
return true;
} else {
var faaHelper = new FAADataHelper();
console.log(airportCode);
return faaHelper.getAirportStatus(airportCode).then(function(airportStatus) {
console.log(airportStatus);
res.say(faaHelper.formatAirportStatus(airportStatus)).send();
}).catch(function(err) {
console.log(err.statusCode);
var prompt = 'I didn\'t have data for an airport code of ' +
airportCode;
res.say(prompt).reprompt(reprompt).shouldEndSession(false).send();
});
//return false;
}
}
);

How to send trap messages from linux to snmp agent on my local machine?

I could not find any solution on internet so i decided to post my question on stackoverflow, I have a program on linux server when i execute it should send trap messages to my local snmp agent. I am setting host name to my system IP address, so i see all logs are printing from the program but its not sedning traps to my local snmp agent any idea how i can make it work in this case ?
app.js
var snmp = require("net-snmp");
var msg = require('./event.js');
function process (msg) {
var host = msg.event.body.trapHost;
var snmpVersion = snmp.Version1;
if (msg.event.body.snmpVersion === "v2"){
snmpVersion = snmp.Version2c
}
var sessionOptions = {
port: 161,
retries: 1,
timeout: 5000,
transport: "udp4",
trapPort: msg.event.body.trapPort,
version: snmpVersion
};
//Create snmp Session
var session = snmp.createSession(host,"public",sessionOptions);
var trapOid = msg.event.body.snmp.trapOID;
var varbinds = msg.event.body.snmp.appOIDs;
var options = {upTime: 1000};
varbinds.forEach(function (oids) {
oids.type = snmp.ObjectType.OctetString;
console.log(oids.value);
});
try {
if (snmp.isVarbindError(varbinds)) {
Logger.error(snmp.varbindError(varbinds));
} else {
session.trap(trapOid, varbinds, options, function (error) {
if (error)
console.log(error);
else
console.log('SNMP successfully delivered');
});
}
} catch (e) {
console.log("SNMP processing error: " + e);
}
};
process(msg);
event.js
module.exports = {
event: {
header: {
eventSource: "d-snmp"
},
body: {
snmp: {
trapHost:"135.68.100.236",
trapPort:"162",
trapOID:"1.3.6.1.4.140.625",
appOIDs: [
{
oid: "1.3.6.1.4.140.900",
type: "",
value: "Problem with Hardware"
},
{
oid: "1.3.6.1.4.140.700",
type: "",
value: "Hardware ok"
}
]
},
snmpVersion: "v2"
}
}
};

Why does this Bottleneck-ed app stall?

I have the app below and it stalls (code below). And I have no idea why. I suspect I might be using the Bottleneck module the wrong way.
Disclaimer: I am trying to learn programming and NodeJS myself using this project. Please help.
Intro
The point of the app is to fetch data missing from documents in a DB by requesting a webpage and parsing it jQuery-style. Then saving the returned data to new keys in the document. The database consists of ~92 000 documents. The app uses the bottleneck, cheerio and request modules. I run the app on OS X.
The problem
If I set a limit to the number of requests, such as
var limiter = new bottleneck(5, 0);
The app stalls after the first batch (5 in this case). But why? I suspect something might be wrong with Bottleneck and how it expects my program to work. Something to do with callbacks per Bottleneck "Gotchas" maybe?
If I set no limit, the app kind-of-works. It fetches webpages and writes to the DB. However with a lot of errors due to resources being overloaded and thus slowly. This is how I tell bottleneck not to limit:
var limiter = new bottleneck(0, 0);
These are the kind of errors I get:
{ [Error: getaddrinfo ENOTFOUND www.vestnikverejnychzakazek.cz www.vestnikverejnychzakazek.cz:443]
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'www.vestnikverejnychzakazek.cz',
host: 'www.vestnikverejnychzakazek.cz',
port: 443 }
{ [Error: connect EMFILE 65.52.146.11:443 - Local (undefined:undefined)]
code: 'EMFILE',
errno: 'EMFILE',
syscall: 'connect',
address: '65.52.146.11',
port: 443 }
App code
'use strict';
var express = require('express');
var router = express.Router();
var assert = require('assert');
var mongo = require('mongoskin');
var path = require('path');
var ObjectID = require('mongodb').ObjectID;
var db = mongo.db("mongodb://localhost:27017/zak", {
native_parser: true
});
var database = db.collection("zakazky");
var cheerio = require("cheerio");
var request = require("request");
var fs = require("fs");
var toJs = (path.join(__dirname, '../public/javascripts', 'jquery.min.js'));
var jquery = fs.readFileSync(toJs).toString();
var bottleneck = require("bottleneck");
var limiter = new bottleneck(5, 0);
/* GET home page. */
router.get('/', function(req, res) {
var cursor = database.find();
cursor.each(function(err, data) {
assert.equal(err, null);
if (data != null) {
var vvz = "vestnikverejnychzakazek";
var praha = "zakazky.praha.eu";
var id = data["_id"];
var zdroj = data["zdroj"];
if (zdroj.indexOf(vvz) > -1) {
if ((data["cpv"] == null) || (data["predpokladana_hodnota"] == null)) {
limiter.submit(getCPV, id, zdroj, null);
// getCPV(id, zdroj);
} else {
// console.log("we're good");
return
}
} else if (zdroj.indexOf(praha) > -1) {
// console.log("pha");
}
} else {
// callback();
}
});
var getCPV = function(id, zdroj, callback) {
console.log("CPV started");
var zdroj = zdroj.replace("http://", "https://");
console.log("zdroj: " + zdroj);
var cpv = [];
var retryWrapper = function(retries) {
var retries; // I added this
if (retries === 3) {
return;
} else if (retries === undefined) {
retries = 0;
} else if (retries > 0) {
console.log("trying again");
}
request(zdroj, function(err, resp, data) {
if (err) {
console.log(err);
return retryWrapper(retries + 1);
}
var $ = cheerio.load(data);
var predpokladnaHodnota = $("[id*='Hodnota1_']").first().attr("value");
$("[id*='HlavniSlovnik']").each(function() {
cpv.push(this.attribs.value);
});
// let's check what we've got is actual data
if (cpv.length === 0) {
return
} else {
// send it off
writeCPV(id, "cpv", cpv)
}
if (predpokladnaHodnota == undefined || predpokladnaHodnota == null) {
return
} else {
// send it off
writeCPV(id, "predpokladana_hodnota", predpokladnaHodnota)
}
callback();
});
}; // end of retryWrapper
retryWrapper();
};
var writeCPV = function(id, key, value) {
id = ObjectID(id);
(function() {
console.log("starting DB write 1");
database.update({
"_id": id
}, {
$set: {
[key]: value
}
}, function(err, results) {
if (err) {
console.log("error in Mongo DB: \n------------------------\n" + err);
}
console.log("Mongo success!:\n ----------------------\n" + results);
// callback();
});
})();
};
// send the browser we're done
res.sendStatus(200);
});
// ---------------------
module.exports = router;
Here is a sample document from the DB including the fetched keys:
{
"_id": ObjectId("568d91396912101c1007ab4e"),
"cena": 1636363,
"cena_celkem": 1500000,
"cena_dopocitano": false,
"created": "2015-04-07T13:45:10.420739",
"datum_zadani": "2015-02-16",
"dodavatel": "/api/v1/dodavatel/381836/",
"druh_rizeni": "/api/v1/druh_rizeni/1116/",
"id": 1312587,
"modified": "2015-04-18T14:22:10.765733",
"nazev": "Pohostinství",
"pocet_nabidek": 2,
"podporeno_eu": true,
"popis": "Kurzy v oblasti pohostinství (formou profesní kvalifikace)",
"ramcova_smlouva": true,
"resource_uri": "/api/v1/zakazka/1312587/",
"skupina": "490648-ISVZUS_2011",
"typ_zakazky": "/api/v1/typ_zakazky/193/",
"zadavatel": "/api/v1/zadavatel/131528/",
"zdroj": "http://www.vestnikverejnychzakazek.cz/en/Form/Display/568547",
"zdroj_nazev": "isvzus.cz",
"cpv": ["80000000-4", "80400000-8", "", "", ""],
"predpokladana_hodnota": "1 500 000,00"
}
Sample URL being requested:
http://www.vestnikverejnychzakazek.cz/en/Form/Display/568547
This has been up here for a bit but in case anyone else stumbles upon this hopefully this will help someone!
The limiter is calling getCPV and it does call the callback at the end of the sequence, however, there are some conditional statements in retryWrapper that would allow an early return and as a result, never call the callback. The limiter will get piled up until it fires, so always make sure the callback will get fired in all of the scenarios.

Resources