@writeshh/nepse-sdk
Version:
Node.js wrapper around NepseUnofficialApi (Python)
206 lines (162 loc) • 6.59 kB
JavaScript
const { spawn } = require('node:child_process');
const { join, dirname } = require('node:path');
const fs = require('node:fs');
function runBridge(method, args = {}, options = {}) {
let python = options.python || process.env.NEPSE_NODE_PYTHON || 'python3';
const isAsync = options.async === true;
// Resolve against package root to be robust across bundlers and path layouts
const packageRoot = dirname(require.resolve('..' + '/package.json'));
const scriptPath = join(packageRoot, 'bridge', 'nepse_bridge.py');
// Prefer local venv python if present
try {
const hint = join(__dirname, '..', 'bridge', '.python_path');
if (fs.existsSync(hint)) {
const p = fs.readFileSync(hint, 'utf8').trim();
if (p) python = p;
}
} catch {}
return new Promise((resolve, reject) => {
const spawnArgs = [scriptPath, '--method', method, '--args', JSON.stringify(args)];
if (isAsync) spawnArgs.push('--async');
const proc = spawn(python, spawnArgs, {
stdio: ['ignore', 'pipe', 'pipe'],
});
let stdout = '';
let stderr = '';
proc.stdout.on('data', (d) => (stdout += d.toString()));
proc.stderr.on('data', (d) => (stderr += d.toString()));
proc.on('error', reject);
proc.on('close', (code) => {
if (!stdout && stderr) {
return reject(new Error(stderr.trim()));
}
try {
const parsed = JSON.parse(stdout);
if (parsed && parsed.ok) return resolve(parsed.data);
return reject(new Error(parsed && parsed.error ? parsed.error : `Bridge error (code ${code})`));
} catch (e) {
return reject(new Error(stderr || e.message));
}
});
});
}
class NepseNode {
constructor(options = {}) {
this.options = options;
}
getCompanyList() {
return runBridge('getCompanyList', {}, { ...this.options, async: true });
}
getSecurityList() {
return runBridge('getSecurityList', {}, { ...this.options, async: true });
}
getSectorScrips() {
return runBridge('getSectorScrips', {}, { ...this.options, async: true });
}
getLiveMarket() {
return runBridge('getLiveMarket', {}, { ...this.options, async: true });
}
getPriceVolume() {
return runBridge('getPriceVolume', {}, { ...this.options, async: true });
}
getSummary() {
return runBridge('getSummary', {}, { ...this.options, async: false });
}
getTopTenTradeScrips() {
return runBridge('getTopTenTradeScrips', {}, { ...this.options, async: false });
}
getTopTenTransactionScrips() {
return runBridge('getTopTenTransactionScrips', {}, { ...this.options, async: false });
}
getTopTenTurnoverScrips() {
return runBridge('getTopTenTurnoverScrips', {}, { ...this.options, async: false });
}
getSupplyDemand() {
return runBridge('getSupplyDemand', {}, { ...this.options, async: false });
}
getTopGainers() {
return runBridge('getTopGainers', {}, { ...this.options, async: false });
}
getTopLosers() {
return runBridge('getTopLosers', {}, { ...this.options, async: false });
}
isNepseOpen() {
return runBridge('isNepseOpen', {}, { ...this.options, async: false });
}
getNepseIndex() {
return runBridge('getNepseIndex', {}, { ...this.options, async: false });
}
getNepseSubIndices() {
return runBridge('getNepseSubIndices', {}, { ...this.options, async: false });
}
getDailyNepseIndexGraph() {
return runBridge('getDailyNepseIndexGraph', {}, { ...this.options, async: false });
}
getDailySensitiveIndexGraph() {
return runBridge('getDailySensitiveIndexGraph', {}, { ...this.options, async: false });
}
getDailyFloatIndexGraph() {
return runBridge('getDailyFloatIndexGraph', {}, { ...this.options, async: false });
}
getDailySensitiveFloatIndexGraph() {
return runBridge('getDailySensitiveFloatIndexGraph', {}, { ...this.options, async: false });
}
getDailyBankSubindexGraph() {
return runBridge('getDailyBankSubindexGraph', {}, { ...this.options, async: false });
}
getDailyDevelopmentBankSubindexGraph() {
return runBridge('getDailyDevelopmentBankSubindexGraph', {}, { ...this.options, async: false });
}
getDailyFinanceSubindexGraph() {
return runBridge('getDailyFinanceSubindexGraph', {}, { ...this.options, async: false });
}
getDailyHotelTourismSubindexGraph() {
return runBridge('getDailyHotelTourismSubindexGraph', {}, { ...this.options, async: false });
}
getDailyHydroSubindexGraph() {
return runBridge('getDailyHydroSubindexGraph', {}, { ...this.options, async: false });
}
getDailyInvestmentSubindexGraph() {
return runBridge('getDailyInvestmentSubindexGraph', {}, { ...this.options, async: false });
}
getDailyLifeInsuranceSubindexGraph() {
return runBridge('getDailyLifeInsuranceSubindexGraph', {}, { ...this.options, async: false });
}
getDailyManufacturingSubindexGraph() {
return runBridge('getDailyManufacturingSubindexGraph', {}, { ...this.options, async: false });
}
getDailyMicrofinanceSubindexGraph() {
return runBridge('getDailyMicrofinanceSubindexGraph', {}, { ...this.options, async: false });
}
getDailyMutualfundSubindexGraph() {
return runBridge('getDailyMutualfundSubindexGraph', {}, { ...this.options, async: false });
}
getDailyNonLifeInsuranceSubindexGraph() {
return runBridge('getDailyNonLifeInsuranceSubindexGraph', {}, { ...this.options, async: false });
}
getDailyOthersSubindexGraph() {
return runBridge('getDailyOthersSubindexGraph', {}, { ...this.options, async: false });
}
getDailyTradingSubindexGraph() {
return runBridge('getDailyTradingSubindexGraph', {}, { ...this.options, async: false });
}
getFloorSheet() {
return runBridge('getFloorSheet', {}, { ...this.options, async: true });
}
getFloorSheetOf(symbol, businessDate) {
return runBridge('getFloorSheetOf', { symbol, business_date: businessDate }, { ...this.options, async: true });
}
getCompanyDetails(symbol) {
return runBridge('getCompanyDetails', { symbol }, { ...this.options, async: true });
}
getDailyScripPriceGraph(symbol) {
return runBridge('getDailyScripPriceGraph', { symbol }, { ...this.options, async: true });
}
getCompanyPriceVolumeHistory(symbol, startDate, endDate) {
return runBridge('getCompanyPriceVolumeHistory', { symbol, start_date: startDate, end_date: endDate }, { ...this.options, async: true });
}
getSymbolMarketDepth(symbol) {
return runBridge('getSymbolMarketDepth', { symbol }, { ...this.options, async: true });
}
}
module.exports = { NepseNode };