Skip to content
This repository has been archived by the owner on Mar 21, 2021. It is now read-only.

Commit

Permalink
#8 update trading urls
Browse files Browse the repository at this point in the history
  • Loading branch information
pladaria committed Jul 26, 2017
1 parent 063abca commit 3410947
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 45 deletions.
84 changes: 39 additions & 45 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const omit = require('lodash/omit');
const isNil = require('lodash/isNil');
const fromPairs = require('lodash/fromPairs');
const qs = require('querystring');
const {lcFirst} = require('./utils');

const BASE_URL = 'https://trader.degiro.nl';

Expand Down Expand Up @@ -43,7 +44,7 @@ const create = (
const params = querystring.stringify(options);
log('getData', params);
return fetch(
`${BASE_URL}/trading/secure/v5/update/${session.account};jsessionid=${session.id}?${params}`,
`${BASE_URL}/trading_s/secure/v5/update/${session.account};jsessionid=${session.id}?${params}`,
).then(res => res.json());
};

Expand All @@ -70,88 +71,81 @@ const create = (
*
* @return {Promise}
*/
const requestVwdSession = (issueId) => {
return fetch(`https://degiro.quotecast.vwdservices.com/CORS/request_session?version=1.0.20170315&userToken=${session.userToken}`, {
method: 'POST',
headers: { 'Origin': 'https://trader.degiro.nl' },
body: JSON.stringify({
"referrer": "https://trader.degiro.nl"
})
})
.then(res => res.json())
const requestVwdSession = issueId => {
return fetch(
`https://degiro.quotecast.vwdservices.com/CORS/request_session?version=1.0.20170315&userToken=${session.userToken}`,
{
method: 'POST',
headers: {Origin: 'https://trader.degiro.nl'},
body: JSON.stringify({referrer: 'https://trader.degiro.nl'}),
},
).then(res => res.json());
};


/**
* Use VWD session to get latest bid/ask prices for a VWD issue ID
*
* @return {Promise}
*/
const getAskBidPrice = (issueId, timesChecked) => {
timesChecked = timesChecked || 0;

return requestVwdSession().then(vwdSession => {
var checkData = function(res) {
const getAskBidPrice = (issueId, timesChecked = 0) =>
requestVwdSession().then(vwdSession => {
const checkData = res => {
timesChecked++;
var prices = {};
const prices = {};

//sanity check
if(!Array.isArray(res)) {
if (!Array.isArray(res)) {
throw Error('Bad result: ' + JSON.stringify(res));
}

//retry needed?
if(res.length == 1 && res[0].m == 'h') {
if (res.length == 1 && res[0].m == 'h') {
console.log('retry needed');
if(timesChecked <= 3) {
if (timesChecked <= 3) {
return getAskBidPrice(issueId, timesChecked);
}
else {
} else {
throw Error('Tried 3 times to get data, but nothing was returned: ' + JSON.stringify(res));
}
}

//process incoming data
var keys = [];
res.forEach(function(row) {
if(row.m == 'a_req') {
if(row.v[0].startsWith(issueId)) {
function smallFirstLetter(string) {
return string.charAt(0).toLowerCase() + string.slice(1);
}

var key = smallFirstLetter(row.v[0].slice(issueId.length+1));

res.forEach(row => {
if (row.m == 'a_req') {
if (row.v[0].startsWith(issueId)) {
var key = lcFirst(row.v[0].slice(issueId.length + 1));
prices[key] = null;
keys[row.v[1]] = key;
}
}
else if(row.m == 'un' || row.m == 'us') {
prices[keys[row.v[0]]] = row.v[1]
} else if (row.m == 'un' || row.m == 'us') {
prices[keys[row.v[0]]] = row.v[1];
}
});

//check if everything is there
if(typeof prices.bidPrice == 'undefined' || typeof prices.askPrice == 'undefined' || typeof prices.lastPrice == 'undefined' || typeof prices.lastTime == 'undefined') {
throw Error('Couldn\'t find all requested info: ' + JSON.stringify(res));
if (
typeof prices.bidPrice == 'undefined' ||
typeof prices.askPrice == 'undefined' ||
typeof prices.lastPrice == 'undefined' ||
typeof prices.lastTime == 'undefined'
) {
throw Error("Couldn't find all requested info: " + JSON.stringify(res));
}

return prices;
};

return fetch(`https://degiro.quotecast.vwdservices.com/CORS/${vwdSession.sessionId}`, {
method: 'POST',
headers: { 'Origin': 'https://trader.degiro.nl' },
body: `{"controlData":"req(${issueId}.BidPrice);req(${issueId}.AskPrice);req(${issueId}.LastPrice);req(${issueId}.LastTime);"}`
})
.then(function(res) {
return fetch(`https://degiro.quotecast.vwdservices.com/CORS/${vwdSession.sessionId}`);
headers: {Origin: 'https://trader.degiro.nl'},
body: JSON.stringify({
controlData: `req(${issueId}.BidPrice);req(${issueId}.AskPrice);req(${issueId}.LastPrice);req(${issueId}.LastTime);`,
}),
})
.then(res => res.json())
.then(checkData)
.then(() => fetch(`https://degiro.quotecast.vwdservices.com/CORS/${vwdSession.sessionId}`))
.then(res => res.json())
.then(checkData);
});
};


/**
* Get portfolio
Expand Down
7 changes: 7 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Returns given string with the first letter in lower case
*
* @param {string} str
* @return {string}
*/
module.exports.lcFirst = str => str.charAt(0).toLowerCase() + str.slice(1);

0 comments on commit 3410947

Please sign in to comment.