UNPKG

periodicos-capes-mcp

Version:

MCP server para consulta de periódicos científicos do Portal de Periódicos CAPES

69 lines (68 loc) 2.06 kB
import Database from 'better-sqlite3'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); export class QualisService { static instance; db = null; getQualisStmt = null; constructor() { } static getInstance() { if (!QualisService.instance) { QualisService.instance = new QualisService(); } return QualisService.instance; } initDatabase() { if (this.db) return; const dbPath = path.join(__dirname, '..', 'data', 'qualis.db'); try { this.db = new Database(dbPath, { readonly: true }); this.getQualisStmt = this.db.prepare(` SELECT issn, journal_name, area, classification FROM qualis WHERE issn = ? OR issn = ? `); } catch (error) { this.db = null; } } isAvailable() { this.initDatabase(); return this.db !== null; } getQualisByISSN(issn) { if (!this.isAvailable() || !this.getQualisStmt) { return null; } try { // Try both with and without hyphen (some databases store differently) const issnWithHyphen = this.formatISSN(issn); const issnWithoutHyphen = issn.replace('-', ''); const result = this.getQualisStmt.get(issnWithHyphen, issnWithoutHyphen); return result || null; } catch (error) { return null; } } formatISSN(issn) { // Remove any existing formatting const clean = issn.replace(/[^0-9X]/gi, ''); // Add hyphen if not present and valid length if (clean.length === 8 && !issn.includes('-')) { return `${clean.substring(0, 4)}-${clean.substring(4)}`; } return issn; } close() { if (this.db) { this.db.close(); this.db = null; this.getQualisStmt = null; } } }