Skip to content

Instantly share code, notes, and snippets.

@d0whc3r
Last active September 28, 2021 10:08
Show Gist options
  • Save d0whc3r/43ba497c63f4ec948b0184670e59bb9a to your computer and use it in GitHub Desktop.
Save d0whc3r/43ba497c63f4ec948b0184670e59bb9a to your computer and use it in GitHub Desktop.
Cornix extract results

Instructions

Requirements

Execution

  • Run node extract.js

PD: You can add --dot as parameter (node extract.js --dot) to use "." as decimal separator, if not, "," (comma) will be used

"out.csv" file should be created

const conversation = require('./result.json');
const { writeFileSync } = require('fs');
const args = process.argv.slice(2);
const useDot = args.includes('--dot');
function parseTextMessage(text) {
return Array.isArray(text) ? text.map((t) => (typeof t === 'string' ? t : t.text)).join('') : text;
}
function parseMessages(messages) {
return messages.map((message) => ({ ...message, text: parseTextMessage(message.text) }));
}
function extractData(messages) {
const cornixNotificationId = 'user605763187';
const tradeText =
/Exchange: (?<exchange>.*)\nClient: (?<client>.*)\nChannel: (?<channel>.*)\n\nSymbol: (?<symbol>.*)\nStatus: (?<status>.*)\nReason: (?<reason>.*)\n\nYou (?<action>lost|earned) (?<percent>\d+\.\d+)% \((?<btc>[\d|\.]+) BTC \/ (?<usd>[\d|\.]+) USD/gi;
const extract = [];
messages
.filter(({ from_id }) => from_id === cornixNotificationId)
.forEach(({ id, date, text }) => {
const match = tradeText.exec(text);
if (match?.length && match?.groups) {
const {
groups: { exchange, symbol, client, channel, status, action, btc, percent, reason, usd }
} = match;
const mul = action === 'lost' ? -1 : 1;
const info = {
id,
date: date.replace('T', ' '),
exchange,
client,
channel,
symbol,
status,
reason,
percent: +percent * mul,
btc: +btc * mul,
usd: +usd * mul
};
extract.push(info);
}
});
return extract;
}
function printExtract(extract) {
const formattedText = extract
.map((e) => {
let line = '"' + Object.values(e).join('","') + '"';
if (!useDot) {
line = line.replace(/\./g, ',');
}
return line;
})
.join('\n');
const header = '"ID","Date","Exchange","Client","Channel","Symbol","Status","Reason","Percent","BTC","USD"';
writeFileSync('out.csv', [header, formattedText].join('\n'));
}
function init() {
const parsedMessages = parseMessages(conversation.messages);
const result = extractData(parsedMessages);
printExtract(result);
}
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment