From c34e7796c5b9b17120db65b4a71ce2ac7956dc3c Mon Sep 17 00:00:00 2001 From: Oliver Boehlk Date: Thu, 14 May 2020 12:12:53 +0200 Subject: [PATCH] fetch bonds every seven minutes and send info --- bondfetch.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 bondfetch.js diff --git a/bondfetch.js b/bondfetch.js new file mode 100644 index 0000000..211f586 --- /dev/null +++ b/bondfetch.js @@ -0,0 +1,43 @@ +const fetch = require("node-fetch"); +var mysql = require('mysql'); +const TelegramBot = require('node-telegram-bot-api'); + +const simCo = require('./simco-credentials'); +const tgInfo = require('./telegram-credentials'); +if (!simCo.cookie || !tgInfo.token) { + console.error("ERR: credentials not set.") + return; +} +const tgBot = new TelegramBot(tgInfo.token, { polling: false }); + +const lowbonds = "https://www.simcompanies.com/api/bonds/rating/BB+-to-D"; +const highbonds = "https://www.simcompanies.com/api/bonds/rating/AAA-to-BBB-"; + +var knownBonds = []; + +const bondFetchProcess = async (url) => { + try { + var BondData = await fetch(url, { + headers: { + Cookie: simCo.cookie + } + }); + var t1 = await BondData.json(); + } catch (e) { + console.warn("errs") + } + for (let data in t1) { + if (t1[data].interest > 0.7 && !knownBonds.includes(t1[data].id)) { + knownBonds.push(t1[data].id); + tgBot.sendMessage(tgInfo.infoChat, `Found ${t1[data].seller.rating} bond x${t1[data].amount} with ${t1[data].interest}% interest, issued by ${t1[data].seller.company}`); + } + } +} + +const fetchBondData = async () => { + await bondFetchProcess(lowbonds); + await bondFetchProcess(highbonds); + setTimeout(fetchBondData, 450000); +} + +fetchBondData();