UNPKG

@baguskto/saham

Version:

MCP Server untuk data saham Indonesia (IDX) - Implementasi Node.js/TypeScript

128 lines 4.83 kB
"use strict"; /** * GitHub API utilities for fetching Dataset-Saham-IDX repository data */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GitHubApiService = void 0; const axios_1 = __importDefault(require("axios")); const logger_1 = require("./logger"); class GitHubApiService { baseUrl = 'https://api.github.com/repos/wildangunawan/Dataset-Saham-IDX'; rawUrl = 'https://raw.githubusercontent.com/wildangunawan/Dataset-Saham-IDX/master'; /** * Get list of stock CSV files from LQ45 directory */ async getLQ45StockFiles() { try { const response = await axios_1.default.get(`${this.baseUrl}/contents/Saham/LQ45`, { timeout: 10000 }); const files = response.data; return files.filter(file => file.type === 'file' && file.name.endsWith('.csv')); } catch (error) { logger_1.logger.error('Failed to fetch LQ45 stock files:', error); throw new Error('Unable to fetch stock file list from GitHub'); } } /** * Get list of all stock CSV files from Semua directory */ async getAllStockFiles() { try { const response = await axios_1.default.get(`${this.baseUrl}/contents/Saham/Semua`, { timeout: 10000 }); const files = response.data; return files.filter(file => file.type === 'file' && file.name.endsWith('.csv')); } catch (error) { logger_1.logger.error('Failed to fetch all stock files:', error); // Fallback to LQ45 if Semua directory is not accessible logger_1.logger.info('Falling back to LQ45 stock files'); return this.getLQ45StockFiles(); } } /** * Download stock CSV data for a specific ticker */ async downloadStockData(ticker, useAllStocks = false) { const directory = useAllStocks ? 'Semua' : 'LQ45'; const fileName = `${ticker.toUpperCase()}.csv`; const url = `${this.rawUrl}/Saham/${directory}/${fileName}`; try { const response = await axios_1.default.get(url, { timeout: 15000, responseType: 'text' }); return response.data; } catch (error) { if (!useAllStocks) { // Try the comprehensive dataset if LQ45 fails logger_1.logger.info(`${ticker} not found in LQ45, trying comprehensive dataset`); return this.downloadStockData(ticker, true); } logger_1.logger.error(`Failed to download stock data for ${ticker}:`, error); throw new Error(`Stock data not available for ticker: ${ticker}`); } } /** * Get repository information and metadata */ async getRepositoryInfo() { try { // Get repository metadata const repoResponse = await axios_1.default.get(this.baseUrl, { timeout: 10000 }); // Get available stock files const stockFiles = await this.getAllStockFiles(); const availableStocks = stockFiles.map(file => file.name.replace('.csv', '').toUpperCase()); return { lastUpdated: repoResponse.data.updated_at, totalFiles: stockFiles.length, availableStocks }; } catch (error) { logger_1.logger.error('Failed to get repository info:', error); throw new Error('Unable to fetch repository information'); } } /** * Check if a specific stock ticker is available in the dataset */ async isStockAvailable(ticker) { try { const info = await this.getRepositoryInfo(); return info.availableStocks.includes(ticker.toUpperCase()); } catch (error) { logger_1.logger.error(`Failed to check availability for ${ticker}:`, error); return false; } } /** * Get column definitions from the repository */ async getColumnDefinitions() { try { const url = `${this.rawUrl}/Keterangan%20Nama%20Kolom.md`; const response = await axios_1.default.get(url, { timeout: 10000, responseType: 'text' }); return response.data; } catch (error) { logger_1.logger.error('Failed to fetch column definitions:', error); return 'Column definitions not available'; } } } exports.GitHubApiService = GitHubApiService; //# sourceMappingURL=github-api.js.map