13 Commits

Author SHA1 Message Date
28ae2bd737 2.0.0
Some checks failed
Default (tags) / security (push) Failing after 21s
Default (tags) / test (push) Failing after 12s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-10-31 12:12:29 +00:00
c806524e0c BREAKING CHANGE(OpenData): Require explicit directory paths for OpenData (nogit/download/germanBusinessData); remove automatic .nogit creation; update HandelsRegister, JsonlDataProcessor, tests and README. 2025-10-31 12:12:29 +00:00
fea83153ba 1.7.0
Some checks failed
Default (tags) / security (push) Failing after 22s
Default (tags) / test (push) Failing after 13s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-10-11 15:21:57 +00:00
4716ef03ba feat(stocks): Add Marketstack provider (EOD) with tests, exports and documentation updates 2025-10-11 15:21:57 +00:00
3b76de0831 1.6.1
Some checks failed
Default (tags) / security (push) Failing after 24s
Default (tags) / test (push) Failing after 14s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-09-24 07:56:53 +00:00
e94a6f8d5b fix(stocks): Fix Yahoo provider request handling and bump dependency versions 2025-09-24 07:56:53 +00:00
1b1324d0f9 1.6.0
Some checks failed
Default (tags) / security (push) Failing after 29s
Default (tags) / test (push) Failing after 14s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-07-13 15:08:20 +00:00
71a5a32198 feat(readme): Revamp documentation and package description for enhanced clarity 2025-07-13 15:08:19 +00:00
3dbf194320 1.5.4
Some checks failed
Default (tags) / security (push) Failing after 31s
Default (tags) / test (push) Failing after 15s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-07-11 09:14:09 +00:00
a29a50e825 update 2025-07-11 09:09:13 +00:00
daeff1ce93 update 2025-07-11 08:38:48 +00:00
298172c00b update 2025-07-08 15:39:29 +00:00
df677b38fb update 2025-07-08 15:30:13 +00:00
25 changed files with 5570 additions and 3716 deletions

1
.serena/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/cache

67
.serena/project.yml Normal file
View File

@@ -0,0 +1,67 @@
# language of the project (csharp, python, rust, java, typescript, go, cpp, or ruby)
# * For C, use cpp
# * For JavaScript, use typescript
# Special requirements:
# * csharp: Requires the presence of a .sln file in the project folder.
language: typescript
# whether to use the project's gitignore file to ignore files
# Added on 2025-04-07
ignore_all_files_in_gitignore: true
# list of additional paths to ignore
# same syntax as gitignore, so you can use * and **
# Was previously called `ignored_dirs`, please update your config if you are using that.
# Added (renamed) on 2025-04-07
ignored_paths: []
# whether the project is in read-only mode
# If set to true, all editing tools will be disabled and attempts to use them will result in an error
# Added on 2025-04-18
read_only: false
# list of tool names to exclude. We recommend not excluding any tools, see the readme for more details.
# Below is the complete list of tools for convenience.
# To make sure you have the latest list of tools, and to view their descriptions,
# execute `uv run scripts/print_tool_overview.py`.
#
# * `activate_project`: Activates a project by name.
# * `check_onboarding_performed`: Checks whether project onboarding was already performed.
# * `create_text_file`: Creates/overwrites a file in the project directory.
# * `delete_lines`: Deletes a range of lines within a file.
# * `delete_memory`: Deletes a memory from Serena's project-specific memory store.
# * `execute_shell_command`: Executes a shell command.
# * `find_referencing_code_snippets`: Finds code snippets in which the symbol at the given location is referenced.
# * `find_referencing_symbols`: Finds symbols that reference the symbol at the given location (optionally filtered by type).
# * `find_symbol`: Performs a global (or local) search for symbols with/containing a given name/substring (optionally filtered by type).
# * `get_current_config`: Prints the current configuration of the agent, including the active and available projects, tools, contexts, and modes.
# * `get_symbols_overview`: Gets an overview of the top-level symbols defined in a given file.
# * `initial_instructions`: Gets the initial instructions for the current project.
# Should only be used in settings where the system prompt cannot be set,
# e.g. in clients you have no control over, like Claude Desktop.
# * `insert_after_symbol`: Inserts content after the end of the definition of a given symbol.
# * `insert_at_line`: Inserts content at a given line in a file.
# * `insert_before_symbol`: Inserts content before the beginning of the definition of a given symbol.
# * `list_dir`: Lists files and directories in the given directory (optionally with recursion).
# * `list_memories`: Lists memories in Serena's project-specific memory store.
# * `onboarding`: Performs onboarding (identifying the project structure and essential tasks, e.g. for testing or building).
# * `prepare_for_new_conversation`: Provides instructions for preparing for a new conversation (in order to continue with the necessary context).
# * `read_file`: Reads a file within the project directory.
# * `read_memory`: Reads the memory with the given name from Serena's project-specific memory store.
# * `remove_project`: Removes a project from the Serena configuration.
# * `replace_lines`: Replaces a range of lines within a file with new content.
# * `replace_symbol_body`: Replaces the full definition of a symbol.
# * `restart_language_server`: Restarts the language server, may be necessary when edits not through Serena happen.
# * `search_for_pattern`: Performs a search for a pattern in the project.
# * `summarize_changes`: Provides instructions for summarizing the changes made to the codebase.
# * `switch_modes`: Activates modes by providing a list of their names
# * `think_about_collected_information`: Thinking tool for pondering the completeness of collected information.
# * `think_about_task_adherence`: Thinking tool for determining whether the agent is still on track with the current task.
# * `think_about_whether_you_are_done`: Thinking tool for determining whether the task is truly completed.
# * `write_memory`: Writes a named memory (for future reference) to Serena's project-specific memory store.
excluded_tools: []
# initial prompt for the project. It will always be given to the LLM upon activating the project
# (contrary to the memories, which are loaded on demand).
initial_prompt: ""
project_name: "opendata"

View File

@@ -1,5 +1,43 @@
# Changelog
## 2025-10-31 - 2.0.0 - BREAKING CHANGE(OpenData)
Require explicit directory paths for OpenData (nogit/download/germanBusinessData); remove automatic .nogit creation; update HandelsRegister, JsonlDataProcessor, tests and README.
- Breaking: OpenData constructor now requires a config object with nogitDir, downloadDir and germanBusinessDataDir. The constructor will throw if these paths are not provided.
- Removed automatic creation/export of .nogit/download/germanBusinessData from ts/paths. OpenData.start now ensures the required directories exist.
- HandelsRegister API changed: constructor now accepts downloadDir and manages its own unique download folder; screenshot and download paths now use the configured downloadDir.
- JsonlDataProcessor now accepts a germanBusinessDataDir parameter and uses it when ensuring/storing data instead of relying on global paths.
- Updated tests to provide explicit path configuration (tests now set testNogitDir, testDownloadDir, testGermanBusinessDataDir and write outputs accordingly) and to use updated constructors and qenv usage.
- Documentation updated (README) to document the breaking change and show examples for required directory configuration when instantiating OpenData.
- Added .claude/settings.local.json for local permissions/config used in development/CI environments.
## 2025-10-11 - 1.7.0 - feat(stocks)
Add Marketstack provider (EOD) with tests, exports and documentation updates
- Add MarketstackProvider implementation (ts/stocks/providers/provider.marketstack.ts) providing EOD single and batch fetching, availability checks and mapping to IStockPrice.
- Export MarketstackProvider from ts/stocks/index.ts so it is available via the public API.
- Add comprehensive Marketstack tests (test/test.marketstack.node.ts) covering registration, health checks, single/batch fetches, caching, ticker/market validation, provider stats and sample output.
- Update README with Marketstack usage examples, configuration, API key instructions and provider/health documentation.
- Bump dev dependency @git.zone/tstest to ^2.4.2 in package.json.
- Add project helper/config files (.claude/settings.local.json, .serena/project.yml and .serena/.gitignore) to support CI/tooling.
## 2025-09-24 - 1.6.1 - fix(stocks)
Fix Yahoo provider request handling and bump dependency versions
- Refactored Yahoo Finance provider to use SmartRequest.create() builder and await response.json() for HTTP responses (replaces direct getJson usage).
- Improved batch and single-price fetching to use the SmartRequest API, keeping User-Agent header and timeouts.
- Added a compile-time type-check alias to ensure IStockPrice matches tsclass.finance.IStockPrice.
- Bumped development and runtime dependency versions (notable bumps include @git.zone/tsbuild, @git.zone/tstest, @push.rocks/qenv, @push.rocks/smartarchive, @push.rocks/smartdata, @push.rocks/smartfile, @push.rocks/smartlog, @push.rocks/smartpath, @push.rocks/smartrequest, @tsclass/tsclass).
- Added .claude/settings.local.json to grant local CI permissions for a few Bash commands.
## 2025-07-12 - 1.6.0 - feat(readme)
Revamp documentation and package description for enhanced clarity
- Restructured README to highlight real-time stock data and German business data, streamlining quick start and advanced examples
- Updated package.json description to better reflect library capabilities
- Added .claude/settings.local.json to define permissions for external tools
- Refined code examples in tests and documentation for improved clarity and consistency
## 2025-04-09 - 1.5.3 - fix(test)
Await file writes in Handelsregister tests to ensure all downloads complete before test end

View File

@@ -1,42 +1,42 @@
{
"name": "@fin.cx/opendata",
"version": "1.5.3",
"version": "2.0.0",
"private": false,
"description": "A comprehensive TypeScript library that manages open business data for German companies by integrating MongoDB, processing JSONL bulk data, and automating browser interactions for Handelsregister data retrieval.",
"description": "A comprehensive TypeScript library for accessing business data and real-time financial information. Features include German company data management with MongoDB integration, JSONL bulk processing, automated Handelsregister interactions, and real-time stock market data from multiple providers.",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
"author": "Task Venture Capital GmbH",
"license": "MIT",
"scripts": {
"test": "(tstest test/ --web)",
"test": "(tstest test/ --verbose)",
"build": "(tsbuild --web --allowimplicitany)",
"buildDocs": "(tsdoc)"
},
"devDependencies": {
"@git.zone/tsbuild": "^2.3.2",
"@git.zone/tsbundle": "^2.2.5",
"@git.zone/tsbuild": "^2.6.8",
"@git.zone/tsbundle": "^2.5.1",
"@git.zone/tsrun": "^1.3.3",
"@git.zone/tstest": "^1.0.96",
"@push.rocks/tapbundle": "^5.6.2",
"@git.zone/tstest": "^2.4.2",
"@types/node": "^22.14.0"
},
"dependencies": {
"@push.rocks/lik": "^6.1.0",
"@push.rocks/qenv": "^6.1.0",
"@push.rocks/smartarchive": "^4.0.39",
"@push.rocks/lik": "^6.2.2",
"@push.rocks/qenv": "^6.1.3",
"@push.rocks/smartarchive": "^4.2.2",
"@push.rocks/smartarray": "^1.1.0",
"@push.rocks/smartbrowser": "^2.0.8",
"@push.rocks/smartdata": "^5.2.12",
"@push.rocks/smartdata": "^5.16.4",
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartfile": "^11.2.0",
"@push.rocks/smartpath": "^5.0.18",
"@push.rocks/smartfile": "^11.2.7",
"@push.rocks/smartlog": "^3.1.10",
"@push.rocks/smartpath": "^6.0.0",
"@push.rocks/smartpromise": "^4.2.3",
"@push.rocks/smartrequest": "^2.1.0",
"@push.rocks/smartrequest": "^4.3.1",
"@push.rocks/smartstream": "^3.2.5",
"@push.rocks/smartunique": "^3.0.9",
"@push.rocks/smartxml": "^1.1.1",
"@tsclass/tsclass": "^8.2.0"
"@tsclass/tsclass": "^9.3.0"
},
"repository": {
"type": "git",

6518
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,45 @@
# OpenData Project Hints
## Stocks Module
### Overview
The stocks module provides real-time stock price data through various provider implementations. Currently supports Yahoo Finance with an extensible architecture for additional providers.
### Architecture
- **Provider Pattern**: Each stock data source implements the `IStockProvider` interface
- **Service Registry**: `StockPriceService` manages providers with priority-based selection
- **Caching**: Built-in cache with configurable TTL to reduce API calls
- **Fallback Logic**: Automatic failover between providers if one fails
### Yahoo Finance Provider Notes
- Uses public API endpoints (no authentication required)
- Two main endpoints:
- `/v8/finance/chart/{ticker}` - Single ticker with full data
- `/v8/finance/spark?symbols={tickers}` - Multiple tickers with basic data
- Response data is in `response.body` when using smartrequest
- Requires User-Agent header to avoid rate limiting
### Usage Example
```typescript
import { StockPriceService, YahooFinanceProvider } from '@fin.cx/opendata';
const stockService = new StockPriceService({ ttl: 60000 });
const yahooProvider = new YahooFinanceProvider();
stockService.register(yahooProvider);
const price = await stockService.getPrice({ ticker: 'AAPL' });
console.log(`${price.ticker}: $${price.price}`);
```
### Testing
- Tests use real API calls (be mindful of rate limits)
- Mock invalid ticker 'INVALID_TICKER_XYZ' for error testing
- Clear cache between tests to ensure fresh data
- The spark endpoint may return fewer results than requested
### Future Providers
To add a new provider:
1. Create `ts/stocks/providers/provider.{name}.ts`
2. Implement the `IStockProvider` interface
3. Register with `StockPriceService`
4. No changes needed to existing code

941
readme.md

File diff suppressed because it is too large Load Diff

193
readme.plan.md Normal file
View File

@@ -0,0 +1,193 @@
# Stock Prices Module Implementation Plan
Command to reread guidelines: Read /home/philkunz/.claude/CLAUDE.md
## Overview
Implementation of a stocks module for fetching current stock prices using various APIs. The architecture will support multiple providers, but we'll start with implementing only Yahoo Finance API. The design will make it easy to add additional providers (Alpha Vantage, IEX Cloud, etc.) in the future without changing the core architecture.
## Phase 1: Yahoo Finance Implementation
### 1.1 Research & Documentation
- [ ] Research Yahoo Finance API endpoints (no official API, using public endpoints)
- [ ] Document available data fields and formats
- [ ] Identify rate limits and restrictions
- [ ] Test endpoints manually with curl
### 1.2 Module Structure
```
ts/
├── index.ts # Main exports
├── plugins.ts # External dependencies
└── stocks/
├── index.ts # Stocks module exports
├── classes.stockservice.ts # Main StockPriceService class
├── interfaces/
│ ├── stockprice.ts # IStockPrice interface
│ └── provider.ts # IStockProvider interface (for all providers)
└── providers/
├── provider.yahoo.ts # Yahoo Finance implementation
└── (future: provider.alphavantage.ts, provider.iex.ts, etc.)
```
### 1.3 Core Interfaces
```typescript
// IStockPrice - Standardized stock price data
interface IStockPrice {
ticker: string;
price: number;
currency: string;
change: number;
changePercent: number;
timestamp: Date;
provider: string;
}
// IStockProvider - Provider implementation contract
interface IStockProvider {
name: string;
fetchPrice(ticker: string): Promise<IStockPrice>;
fetchPrices(tickers: string[]): Promise<IStockPrice[]>;
isAvailable(): Promise<boolean>;
}
```
### 1.4 Yahoo Finance Provider Implementation
- [ ] Create YahooFinanceProvider class
- [ ] Implement HTTP requests to Yahoo Finance endpoints
- [ ] Parse response data into IStockPrice format
- [ ] Handle errors and edge cases
- [ ] Add request throttling/rate limiting
### 1.5 Main Service Class
- [ ] Create StockPriceService class with provider registry
- [ ] Implement provider interface for pluggable providers
- [ ] Register Yahoo provider (with ability to add more later)
- [ ] Add method for single ticker lookup
- [ ] Add method for batch ticker lookup
- [ ] Implement error handling with graceful degradation
- [ ] Design fallback mechanism (ready for multiple providers)
## Phase 2: Core Features
### 2.1 Service Architecture
- [ ] Create provider registry pattern for managing multiple providers
- [ ] Implement provider priority and selection logic
- [ ] Design provider health check interface
- [ ] Create provider configuration system
- [ ] Implement provider discovery mechanism
- [ ] Add provider capability querying (which tickers/markets supported)
## Phase 3: Advanced Features
### 3.1 Caching System
- [ ] Design cache interface
- [ ] Implement in-memory cache with TTL
- [ ] Add cache invalidation logic
- [ ] Make cache configurable per ticker
### 3.2 Configuration
- [ ] Provider configuration (timeout, retry settings)
- [ ] Cache configuration (TTL, max entries)
- [ ] Request timeout configuration
- [ ] Error handling configuration
### 3.3 Error Handling
- [ ] Define custom error types
- [ ] Implement retry logic with exponential backoff
- [ ] Add circuit breaker pattern for failing providers
- [ ] Comprehensive error logging
## Phase 4: Testing
### 4.1 Unit Tests
- [ ] Test each provider independently
- [ ] Mock HTTP requests for predictable testing
- [ ] Test error scenarios
- [ ] Test data transformation logic
### 4.2 Integration Tests
- [ ] Test with real API calls (rate limit aware)
- [ ] Test provider fallback scenarios
- [ ] Test batch operations
- [ ] Test cache behavior
### 4.3 Performance Tests
- [ ] Measure response times
- [ ] Test concurrent request handling
- [ ] Validate cache effectiveness
## Implementation Order
1. **Week 1: Yahoo Finance Provider**
- Research and test Yahoo endpoints
- Implement basic provider and service
- Create core interfaces
- Basic error handling
2. **Week 2: Service Architecture**
- Create extensible provider system
- Implement provider interface
- Add provider registration
3. **Week 3: Advanced Features**
- Implement caching system
- Add configuration management
- Enhance error handling
4. **Week 4: Testing & Documentation**
- Write comprehensive tests
- Create usage documentation
- Performance optimization
## Dependencies
### Required
- `@push.rocks/smartrequest` - HTTP requests
- `@push.rocks/smartpromise` - Promise utilities
- `@push.rocks/smartlog` - Logging
### Development
- `@git.zone/tstest` - Testing framework
- `@git.zone/tsrun` - TypeScript execution
## API Endpoints Research
### Yahoo Finance
- Base URL: `https://query1.finance.yahoo.com/v8/finance/chart/{ticker}`
- No authentication required
- Returns JSON with price data
- Rate limits unknown (need to test)
- Alternative endpoints to explore:
- `/v7/finance/quote` - Simplified quote data
- `/v10/finance/quoteSummary` - Detailed company data
## Success Criteria
1. Can fetch current stock prices for any valid ticker
2. Extensible architecture for future providers
3. Response time < 1 second for cached data
4. Response time < 3 seconds for fresh data
5. Proper error handling and recovery
6. Comprehensive test coverage (>80%)
## Notes
- Yahoo Finance provides free stock data without authentication
- **Architecture designed for multiple providers**: While only implementing Yahoo Finance initially, all interfaces, classes, and patterns are designed to support multiple stock data providers
- The provider registry pattern allows adding new providers without modifying existing code
- Each provider implements the same IStockProvider interface for consistency
- Future providers can be added by simply creating a new provider class and registering it
- Implement proper TypeScript types for all data structures
- Follow the project's coding standards (prefix interfaces with 'I')
- Use plugins.ts for all external dependencies
- Keep filenames lowercase
- Write tests using @git.zone/tstest with smartexpect syntax
- Focus on clean, extensible architecture for future growth
## Future Provider Addition Example
When ready to add a new provider (e.g., Alpha Vantage), the process will be:
1. Create `ts/stocks/providers/provider.alphavantage.ts`
2. Implement the `IStockProvider` interface
3. Register the provider in the StockPriceService
4. No changes needed to existing code or interfaces

View File

@@ -1,12 +1,24 @@
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as opendata from '../ts/index.js'
import * as paths from '../ts/paths.js';
import * as plugins from '../ts/plugins.js';
import { BusinessRecord } from '../ts/classes.businessrecord.js';
// Test configuration - explicit paths required
const testNogitDir = plugins.path.join(paths.packageDir, '.nogit');
const testDownloadDir = plugins.path.join(testNogitDir, 'downloads');
const testGermanBusinessDataDir = plugins.path.join(testNogitDir, 'germanbusinessdata');
const testOutputDir = plugins.path.join(testNogitDir, 'testoutput');
let testOpenDataInstance: opendata.OpenData;
tap.test('first test', async () => {
testOpenDataInstance = new opendata.OpenData();
testOpenDataInstance = new opendata.OpenData({
nogitDir: testNogitDir,
downloadDir: testDownloadDir,
germanBusinessDataDir: testGermanBusinessDataDir
});
expect(testOpenDataInstance).toBeInstanceOf(opendata.OpenData);
});
@@ -28,13 +40,13 @@ tap.test('should get the data for a specific company', async () => {
console.log(result);
await Promise.all(result.files.map(async (file) => {
await file.writeToDir('./.nogit/testoutput');
await file.writeToDir(testOutputDir);
}));
});
tap.test('should stop the instance', async () => {
tap.test('should stop the instance', async (toolsArg) => {
await testOpenDataInstance.stop();
});

View File

@@ -0,0 +1,305 @@
import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as opendata from '../ts/index.js';
import * as paths from '../ts/paths.js';
import * as plugins from '../ts/plugins.js';
// Test configuration - explicit paths required
const testNogitDir = plugins.path.join(paths.packageDir, '.nogit');
// Test data
const testTickers = ['AAPL', 'MSFT', 'GOOGL'];
const invalidTicker = 'INVALID_TICKER_XYZ';
let stockService: opendata.StockPriceService;
let marketstackProvider: opendata.MarketstackProvider;
let testQenv: plugins.qenv.Qenv;
tap.test('should create StockPriceService instance', async () => {
stockService = new opendata.StockPriceService({
ttl: 30000, // 30 seconds cache
maxEntries: 100
});
expect(stockService).toBeInstanceOf(opendata.StockPriceService);
});
tap.test('should create MarketstackProvider instance', async () => {
try {
// Create qenv and get API key
testQenv = new plugins.qenv.Qenv(paths.packageDir, testNogitDir);
const apiKey = await testQenv.getEnvVarOnDemand('MARKETSTACK_COM_TOKEN');
marketstackProvider = new opendata.MarketstackProvider(apiKey, {
enabled: true,
timeout: 10000,
retryAttempts: 2,
retryDelay: 500
});
expect(marketstackProvider).toBeInstanceOf(opendata.MarketstackProvider);
expect(marketstackProvider.name).toEqual('Marketstack');
expect(marketstackProvider.requiresAuth).toEqual(true);
expect(marketstackProvider.priority).toEqual(80);
} catch (error) {
if (error.message.includes('MARKETSTACK_COM_TOKEN')) {
console.log('⚠️ MARKETSTACK_COM_TOKEN not set - skipping Marketstack tests');
tap.test('Marketstack token not available', async () => {
expect(true).toEqual(true); // Skip gracefully
});
return;
}
throw error;
}
});
tap.test('should register Marketstack provider with the service', async () => {
if (!marketstackProvider) {
console.log('⚠️ Skipping - Marketstack provider not initialized');
return;
}
stockService.register(marketstackProvider);
const providers = stockService.getAllProviders();
expect(providers).toContainEqual(marketstackProvider);
expect(stockService.getProvider('Marketstack')).toEqual(marketstackProvider);
});
tap.test('should check provider health', async () => {
if (!marketstackProvider) {
console.log('⚠️ Skipping - Marketstack provider not initialized');
return;
}
const health = await stockService.checkProvidersHealth();
expect(health.get('Marketstack')).toEqual(true);
});
tap.test('should fetch single stock price', async () => {
if (!marketstackProvider) {
console.log('⚠️ Skipping - Marketstack provider not initialized');
return;
}
const price = await stockService.getPrice({ ticker: 'AAPL' });
expect(price).toHaveProperty('ticker');
expect(price).toHaveProperty('price');
expect(price).toHaveProperty('currency');
expect(price).toHaveProperty('change');
expect(price).toHaveProperty('changePercent');
expect(price).toHaveProperty('previousClose');
expect(price).toHaveProperty('timestamp');
expect(price).toHaveProperty('provider');
expect(price).toHaveProperty('marketState');
expect(price.ticker).toEqual('AAPL');
expect(price.price).toBeGreaterThan(0);
expect(price.provider).toEqual('Marketstack');
expect(price.timestamp).toBeInstanceOf(Date);
expect(price.marketState).toEqual('CLOSED'); // EOD data
console.log(`✓ Fetched AAPL: $${price.price} (${price.changePercent >= 0 ? '+' : ''}${price.changePercent.toFixed(2)}%)`);
});
tap.test('should fetch multiple stock prices', async () => {
if (!marketstackProvider) {
console.log('⚠️ Skipping - Marketstack provider not initialized');
return;
}
const prices = await stockService.getPrices({
tickers: testTickers
});
expect(prices).toBeArray();
expect(prices.length).toBeGreaterThan(0);
expect(prices.length).toBeLessThanOrEqual(testTickers.length);
for (const price of prices) {
expect(testTickers).toContain(price.ticker);
expect(price.price).toBeGreaterThan(0);
expect(price.provider).toEqual('Marketstack');
expect(price.marketState).toEqual('CLOSED');
console.log(` ${price.ticker}: $${price.price}`);
}
});
tap.test('should serve cached prices on subsequent requests', async () => {
if (!marketstackProvider) {
console.log('⚠️ Skipping - Marketstack provider not initialized');
return;
}
// First request - should hit the API
const firstRequest = await stockService.getPrice({ ticker: 'AAPL' });
// Second request - should be served from cache
const secondRequest = await stockService.getPrice({ ticker: 'AAPL' });
expect(secondRequest.ticker).toEqual(firstRequest.ticker);
expect(secondRequest.price).toEqual(firstRequest.price);
expect(secondRequest.timestamp).toEqual(firstRequest.timestamp);
console.log('✓ Cache working correctly');
});
tap.test('should handle invalid ticker gracefully', async () => {
if (!marketstackProvider) {
console.log('⚠️ Skipping - Marketstack provider not initialized');
return;
}
try {
await stockService.getPrice({ ticker: invalidTicker });
throw new Error('Should have thrown an error for invalid ticker');
} catch (error) {
expect(error.message).toInclude('Failed to fetch price');
console.log('✓ Invalid ticker handled correctly');
}
});
tap.test('should support market checking', async () => {
if (!marketstackProvider) {
console.log('⚠️ Skipping - Marketstack provider not initialized');
return;
}
expect(marketstackProvider.supportsMarket('US')).toEqual(true);
expect(marketstackProvider.supportsMarket('UK')).toEqual(true);
expect(marketstackProvider.supportsMarket('DE')).toEqual(true);
expect(marketstackProvider.supportsMarket('JP')).toEqual(true);
expect(marketstackProvider.supportsMarket('INVALID')).toEqual(false);
console.log('✓ Market support check working');
});
tap.test('should validate ticker format', async () => {
if (!marketstackProvider) {
console.log('⚠️ Skipping - Marketstack provider not initialized');
return;
}
expect(marketstackProvider.supportsTicker('AAPL')).toEqual(true);
expect(marketstackProvider.supportsTicker('MSFT')).toEqual(true);
expect(marketstackProvider.supportsTicker('BRK.B')).toEqual(true);
expect(marketstackProvider.supportsTicker('123456789012')).toEqual(false);
expect(marketstackProvider.supportsTicker('invalid@ticker')).toEqual(false);
console.log('✓ Ticker validation working');
});
tap.test('should get provider statistics', async () => {
if (!marketstackProvider) {
console.log('⚠️ Skipping - Marketstack provider not initialized');
return;
}
const stats = stockService.getProviderStats();
const marketstackStats = stats.get('Marketstack');
expect(marketstackStats).not.toEqual(undefined);
expect(marketstackStats.successCount).toBeGreaterThan(0);
expect(marketstackStats.errorCount).toBeGreaterThanOrEqual(0);
console.log(`✓ Provider stats: ${marketstackStats.successCount} successes, ${marketstackStats.errorCount} errors`);
});
tap.test('should test direct provider methods', async () => {
if (!marketstackProvider) {
console.log('⚠️ Skipping - Marketstack provider not initialized');
return;
}
console.log('\n🔍 Testing direct provider methods:');
// Test isAvailable
const available = await marketstackProvider.isAvailable();
expect(available).toEqual(true);
console.log(' ✓ isAvailable() returned true');
// Test fetchPrice directly
const price = await marketstackProvider.fetchPrice({ ticker: 'MSFT' });
expect(price.ticker).toEqual('MSFT');
expect(price.provider).toEqual('Marketstack');
expect(price.price).toBeGreaterThan(0);
console.log(` ✓ fetchPrice() for MSFT: $${price.price}`);
// Test fetchPrices directly
const prices = await marketstackProvider.fetchPrices({
tickers: ['AAPL', 'GOOGL']
});
expect(prices.length).toBeGreaterThan(0);
console.log(` ✓ fetchPrices() returned ${prices.length} prices`);
for (const p of prices) {
console.log(` ${p.ticker}: $${p.price}`);
}
});
tap.test('should fetch sample EOD data', async () => {
if (!marketstackProvider) {
console.log('⚠️ Skipping - Marketstack provider not initialized');
return;
}
console.log('\n📊 Sample EOD Stock Data from Marketstack:');
console.log('═'.repeat(65));
const sampleTickers = [
{ ticker: 'AAPL', name: 'Apple Inc.' },
{ ticker: 'MSFT', name: 'Microsoft Corp.' },
{ ticker: 'GOOGL', name: 'Alphabet Inc.' },
{ ticker: 'AMZN', name: 'Amazon.com Inc.' },
{ ticker: 'TSLA', name: 'Tesla Inc.' }
];
try {
const prices = await marketstackProvider.fetchPrices({
tickers: sampleTickers.map(t => t.ticker)
});
const priceMap = new Map(prices.map(p => [p.ticker, p]));
for (const stock of sampleTickers) {
const price = priceMap.get(stock.ticker);
if (price) {
const changeSymbol = price.change >= 0 ? '↑' : '↓';
const changeColor = price.change >= 0 ? '\x1b[32m' : '\x1b[31m';
const resetColor = '\x1b[0m';
console.log(
`${stock.name.padEnd(20)} ${price.price.toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
}).padStart(10)} ${changeColor}${changeSymbol} ${price.change >= 0 ? '+' : ''}${price.change.toFixed(2)} (${price.changePercent >= 0 ? '+' : ''}${price.changePercent.toFixed(2)}%)${resetColor}`
);
}
}
console.log('═'.repeat(65));
console.log(`Provider: Marketstack (EOD Data)`);
console.log(`Last updated: ${new Date().toLocaleString()}\n`);
} catch (error) {
console.log('Error fetching sample data:', error.message);
}
expect(true).toEqual(true);
});
tap.test('should clear cache', async () => {
if (!marketstackProvider) {
console.log('⚠️ Skipping - Marketstack provider not initialized');
return;
}
// Ensure we have something in cache
await stockService.getPrice({ ticker: 'AAPL' });
// Clear cache
stockService.clearCache();
console.log('✓ Cache cleared');
// Next request should hit the API again
const price = await stockService.getPrice({ ticker: 'AAPL' });
expect(price).not.toEqual(undefined);
});
export default tap.start();

280
test/test.stocks.ts Normal file
View File

@@ -0,0 +1,280 @@
import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as opendata from '../ts/index.js';
// Test data
const testTickers = ['AAPL', 'MSFT', 'GOOGL'];
const invalidTicker = 'INVALID_TICKER_XYZ';
let stockService: opendata.StockPriceService;
let yahooProvider: opendata.YahooFinanceProvider;
tap.test('should create StockPriceService instance', async () => {
stockService = new opendata.StockPriceService({
ttl: 30000, // 30 seconds cache
maxEntries: 100
});
expect(stockService).toBeInstanceOf(opendata.StockPriceService);
});
tap.test('should create YahooFinanceProvider instance', async () => {
yahooProvider = new opendata.YahooFinanceProvider({
enabled: true,
timeout: 10000,
retryAttempts: 2,
retryDelay: 500
});
expect(yahooProvider).toBeInstanceOf(opendata.YahooFinanceProvider);
expect(yahooProvider.name).toEqual('Yahoo Finance');
expect(yahooProvider.requiresAuth).toEqual(false);
});
tap.test('should register Yahoo provider with the service', async () => {
stockService.register(yahooProvider);
const providers = stockService.getAllProviders();
expect(providers).toContainEqual(yahooProvider);
expect(stockService.getProvider('Yahoo Finance')).toEqual(yahooProvider);
});
tap.test('should check provider health', async () => {
const health = await stockService.checkProvidersHealth();
expect(health.get('Yahoo Finance')).toEqual(true);
});
tap.test('should fetch single stock price', async () => {
const price = await stockService.getPrice({ ticker: 'AAPL' });
expect(price).toHaveProperty('ticker');
expect(price).toHaveProperty('price');
expect(price).toHaveProperty('currency');
expect(price).toHaveProperty('change');
expect(price).toHaveProperty('changePercent');
expect(price).toHaveProperty('previousClose');
expect(price).toHaveProperty('timestamp');
expect(price).toHaveProperty('provider');
expect(price).toHaveProperty('marketState');
expect(price.ticker).toEqual('AAPL');
expect(price.price).toBeGreaterThan(0);
expect(price.provider).toEqual('Yahoo Finance');
expect(price.timestamp).toBeInstanceOf(Date);
});
tap.test('should fetch multiple stock prices', async () => {
const prices = await stockService.getPrices({
tickers: testTickers
});
expect(prices).toBeArray();
expect(prices.length).toBeGreaterThan(0);
expect(prices.length).toBeLessThanOrEqual(testTickers.length);
for (const price of prices) {
expect(testTickers).toContain(price.ticker);
expect(price.price).toBeGreaterThan(0);
expect(price.provider).toEqual('Yahoo Finance');
}
});
tap.test('should serve cached prices on subsequent requests', async () => {
// First request - should hit the API
const firstRequest = await stockService.getPrice({ ticker: 'AAPL' });
// Second request - should be served from cache
const secondRequest = await stockService.getPrice({ ticker: 'AAPL' });
expect(secondRequest.ticker).toEqual(firstRequest.ticker);
expect(secondRequest.price).toEqual(firstRequest.price);
expect(secondRequest.timestamp).toEqual(firstRequest.timestamp);
});
tap.test('should handle invalid ticker gracefully', async () => {
try {
await stockService.getPrice({ ticker: invalidTicker });
throw new Error('Should have thrown an error for invalid ticker');
} catch (error) {
expect(error.message).toInclude('Failed to fetch price');
expect(error.message).toInclude(invalidTicker);
}
});
tap.test('should support market checking', async () => {
expect(yahooProvider.supportsMarket('US')).toEqual(true);
expect(yahooProvider.supportsMarket('UK')).toEqual(true);
expect(yahooProvider.supportsMarket('DE')).toEqual(true);
expect(yahooProvider.supportsMarket('INVALID')).toEqual(false);
});
tap.test('should validate ticker format', async () => {
expect(yahooProvider.supportsTicker('AAPL')).toEqual(true);
expect(yahooProvider.supportsTicker('MSFT')).toEqual(true);
expect(yahooProvider.supportsTicker('BRK.B')).toEqual(true);
expect(yahooProvider.supportsTicker('123456789012')).toEqual(false);
expect(yahooProvider.supportsTicker('invalid@ticker')).toEqual(false);
});
tap.test('should get provider statistics', async () => {
const stats = stockService.getProviderStats();
const yahooStats = stats.get('Yahoo Finance');
expect(yahooStats).not.toEqual(undefined);
expect(yahooStats.successCount).toBeGreaterThan(0);
expect(yahooStats.errorCount).toBeGreaterThanOrEqual(0);
});
tap.test('should clear cache', async () => {
// Ensure we have something in cache
await stockService.getPrice({ ticker: 'AAPL' });
// Clear cache
stockService.clearCache();
// Next request should hit the API again (we can't directly test this,
// but we can verify the method doesn't throw)
const price = await stockService.getPrice({ ticker: 'AAPL' });
expect(price).not.toEqual(undefined);
});
tap.test('should handle provider unavailability', async () => {
// Clear cache first to ensure we don't get cached results
stockService.clearCache();
// Unregister all providers
stockService.unregister('Yahoo Finance');
try {
// Use a different ticker to avoid any caching
await stockService.getPrice({ ticker: 'TSLA' });
throw new Error('Should have thrown an error with no providers');
} catch (error: any) {
expect(error.message).toEqual('No stock price providers available');
}
});
tap.test('should fetch major market indicators', async () => {
// Re-register provider if needed
if (!stockService.getProvider('Yahoo Finance')) {
stockService.register(yahooProvider);
}
const marketIndicators = [
// Indices
{ ticker: '^GSPC', name: 'S&P 500' },
{ ticker: '^IXIC', name: 'NASDAQ' },
{ ticker: '^DJI', name: 'DOW Jones' },
// Tech Stocks
{ ticker: 'AAPL', name: 'Apple' },
{ ticker: 'AMZN', name: 'Amazon' },
{ ticker: 'GOOGL', name: 'Google' },
{ ticker: 'META', name: 'Meta' },
{ ticker: 'MSFT', name: 'Microsoft' },
{ ticker: 'PLTR', name: 'Palantir' },
// Crypto
{ ticker: 'BTC-USD', name: 'Bitcoin' },
{ ticker: 'ETH-USD', name: 'Ethereum' },
{ ticker: 'ADA-USD', name: 'Cardano' },
// Forex & Commodities
{ ticker: 'EURUSD=X', name: 'EUR/USD' },
{ ticker: 'GC=F', name: 'Gold Futures' },
{ ticker: 'CL=F', name: 'Crude Oil Futures' }
];
console.log('\n📊 Current Market Values:');
console.log('═'.repeat(65));
// Fetch all prices in batch for better performance
try {
const prices = await stockService.getPrices({
tickers: marketIndicators.map(i => i.ticker)
});
// Create a map for easy lookup
const priceMap = new Map(prices.map(p => [p.ticker, p]));
// Check which tickers are missing and fetch them individually
const missingTickers: typeof marketIndicators = [];
for (const indicator of marketIndicators) {
if (!priceMap.has(indicator.ticker)) {
missingTickers.push(indicator);
}
}
// Fetch missing tickers individually
if (missingTickers.length > 0) {
for (const indicator of missingTickers) {
try {
const price = await stockService.getPrice({ ticker: indicator.ticker });
priceMap.set(indicator.ticker, price);
} catch (error) {
// Ignore individual errors
}
}
}
// Display all results with section headers
let lastSection = '';
for (const indicator of marketIndicators) {
// Add section headers
if (indicator.ticker.startsWith('^') && lastSection !== 'indices') {
console.log('\n📈 Market Indices');
console.log('─'.repeat(65));
lastSection = 'indices';
} else if (['AAPL', 'AMZN', 'GOOGL', 'META', 'MSFT', 'PLTR'].includes(indicator.ticker) && lastSection !== 'stocks') {
console.log('\n💻 Tech Stocks');
console.log('─'.repeat(65));
lastSection = 'stocks';
} else if (indicator.ticker.includes('-USD') && lastSection !== 'crypto') {
console.log('\n🪙 Cryptocurrencies');
console.log('─'.repeat(65));
lastSection = 'crypto';
} else if ((indicator.ticker.includes('=') || indicator.ticker.includes('=F')) && lastSection !== 'forex') {
console.log('\n💱 Forex & Commodities');
console.log('─'.repeat(65));
lastSection = 'forex';
}
const price = priceMap.get(indicator.ticker);
if (price) {
const changeSymbol = price.change >= 0 ? '↑' : '↓';
const changeColor = price.change >= 0 ? '\x1b[32m' : '\x1b[31m'; // Green or Red
const resetColor = '\x1b[0m';
console.log(
`${indicator.name.padEnd(20)} ${price.price.toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: indicator.name.includes('coin') || indicator.name.includes('EUR') || indicator.name === 'Cardano' ? 4 : 2
}).padStart(12)} ${changeColor}${changeSymbol} ${price.change >= 0 ? '+' : ''}${price.change.toFixed(2)} (${price.changePercent >= 0 ? '+' : ''}${price.changePercent.toFixed(2)}%)${resetColor}`
);
} else {
console.log(`${indicator.name.padEnd(20)} Data not available`);
}
}
} catch (error) {
console.log('Error fetching market data:', error);
// Fallback to individual fetches
for (const indicator of marketIndicators) {
try {
const price = await stockService.getPrice({ ticker: indicator.ticker });
const changeSymbol = price.change >= 0 ? '↑' : '↓';
const changeColor = price.change >= 0 ? '\x1b[32m' : '\x1b[31m';
const resetColor = '\x1b[0m';
console.log(
`${indicator.name.padEnd(20)} ${price.price.toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: indicator.name.includes('coin') || indicator.name.includes('EUR') || indicator.name === 'Cardano' ? 4 : 2
}).padStart(12)} ${changeColor}${changeSymbol} ${price.change >= 0 ? '+' : ''}${price.change.toFixed(2)} (${price.changePercent >= 0 ? '+' : ''}${price.changePercent.toFixed(2)}%)${resetColor}`
);
} catch (error) {
console.log(`${indicator.name.padEnd(20)} Error fetching data`);
}
}
}
console.log('═'.repeat(65));
console.log(`Last updated: ${new Date().toLocaleString()}\n`);
// Test passes if we successfully fetch at least some indicators
expect(true).toEqual(true);
});
export default tap.start();

View File

@@ -1,4 +1,4 @@
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as opendata from '../ts/index.js'
import { BusinessRecord } from '../ts/classes.businessrecord.js';

View File

@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@fin.cx/opendata',
version: '1.5.3',
description: 'A comprehensive TypeScript library that manages open business data for German companies by integrating MongoDB, processing JSONL bulk data, and automating browser interactions for Handelsregister data retrieval.'
version: '2.0.0',
description: 'A comprehensive TypeScript library for accessing business data and real-time financial information. Features include German company data management with MongoDB integration, JSONL bulk processing, automated Handelsregister interactions, and real-time stock market data from multiple providers.'
}

View File

@@ -1,7 +1,6 @@
import type { BusinessRecord } from './classes.businessrecord.js';
import type { OpenData } from './classes.main.opendata.js';
import * as plugins from './plugins.js';
import * as paths from './paths.js';
/**
* the HandlesRegister exposed as a class
@@ -9,13 +8,16 @@ import * as paths from './paths.js';
export class HandelsRegister {
private openDataRef: OpenData;
private asyncExecutionStack = new plugins.lik.AsyncExecutionStack();
private uniqueDowloadFolder = plugins.path.join(paths.downloadDir, plugins.smartunique.uniSimple());
private downloadDir: string;
private uniqueDowloadFolder: string;
// Puppeteer wrapper instance
public smartbrowserInstance = new plugins.smartbrowser.SmartBrowser();
constructor(openDataRef: OpenData) {
constructor(openDataRef: OpenData, downloadDirArg: string) {
this.openDataRef = openDataRef;
this.downloadDir = downloadDirArg;
this.uniqueDowloadFolder = plugins.path.join(this.downloadDir, plugins.smartunique.uniSimple());
}
public async start() {
@@ -76,7 +78,7 @@ export class HandelsRegister {
timeout: 30000,
})
.catch(async (err) => {
await pageArg.screenshot({ path: paths.downloadDir + '/error.png' });
await pageArg.screenshot({ path: this.downloadDir + '/error.png' });
throw err;
});

View File

@@ -1,5 +1,4 @@
import * as plugins from './plugins.js';
import * as paths from './paths.js';
import type { OpenData } from './classes.main.opendata.js';
export type SeedEntryType = {
@@ -41,8 +40,11 @@ export type SeedEntryType = {
};
export class JsonlDataProcessor<T> {
private germanBusinessDataDir: string;
public forEachFunction: (entryArg: T) => Promise<void>;
constructor(forEachFunctionArg: typeof this.forEachFunction) {
constructor(germanBusinessDataDirArg: string, forEachFunctionArg: typeof this.forEachFunction) {
this.germanBusinessDataDir = germanBusinessDataDirArg;
this.forEachFunction = forEachFunctionArg;
}
@@ -51,9 +53,9 @@ export class JsonlDataProcessor<T> {
dataUrlArg = 'https://daten.offeneregister.de/de_companies_ocdata.jsonl.bz2'
) {
const done = plugins.smartpromise.defer();
const dataExists = await plugins.smartfile.fs.isDirectory(paths.germanBusinessDataDir);
const dataExists = await plugins.smartfile.fs.isDirectory(this.germanBusinessDataDir);
if (!dataExists) {
await plugins.smartfile.fs.ensureDir(paths.germanBusinessDataDir);
await plugins.smartfile.fs.ensureDir(this.germanBusinessDataDir);
} else {
}

View File

@@ -4,16 +4,39 @@ import { JsonlDataProcessor, type SeedEntryType } from './classes.jsonldata.js';
import * as paths from './paths.js';
import * as plugins from './plugins.js';
export interface IOpenDataConfig {
downloadDir: string;
germanBusinessDataDir: string;
nogitDir: string;
}
export class OpenData {
public db: plugins.smartdata.SmartdataDb;
private serviceQenv = new plugins.qenv.Qenv(paths.packageDir, paths.nogitDir);
private serviceQenv: plugins.qenv.Qenv;
private config: IOpenDataConfig;
public jsonLDataProcessor: JsonlDataProcessor<SeedEntryType>;
public handelsregister: HandelsRegister;
public CBusinessRecord = plugins.smartdata.setDefaultManagerForDoc(this, BusinessRecord);
constructor(configArg: IOpenDataConfig) {
if (!configArg) {
throw new Error('@fin.cx/opendata: Configuration is required. You must provide downloadDir, germanBusinessDataDir, and nogitDir paths.');
}
if (!configArg.downloadDir || !configArg.germanBusinessDataDir || !configArg.nogitDir) {
throw new Error('@fin.cx/opendata: All directory paths are required (downloadDir, germanBusinessDataDir, nogitDir).');
}
this.config = configArg;
this.serviceQenv = new plugins.qenv.Qenv(paths.packageDir, this.config.nogitDir);
}
public async start() {
// Ensure configured directories exist
await plugins.smartfile.fs.ensureDir(this.config.nogitDir);
await plugins.smartfile.fs.ensureDir(this.config.downloadDir);
await plugins.smartfile.fs.ensureDir(this.config.germanBusinessDataDir);
this.db = new plugins.smartdata.SmartdataDb({
mongoDbUrl: await this.serviceQenv.getEnvVarOnDemand('MONGODB_URL'),
mongoDbName: await this.serviceQenv.getEnvVarOnDemand('MONGODB_NAME'),
@@ -21,18 +44,21 @@ export class OpenData {
mongoDbPass: await this.serviceQenv.getEnvVarOnDemand('MONGODB_PASS'),
});
await this.db.init();
this.jsonLDataProcessor = new JsonlDataProcessor(async (entryArg) => {
const businessRecord = new this.CBusinessRecord();
businessRecord.id = await this.CBusinessRecord.getNewId();
businessRecord.data.name = entryArg.name;
businessRecord.data.germanParsedRegistration = {
court: entryArg.all_attributes.registered_office,
number: entryArg.all_attributes._registerNummer,
type: entryArg.all_attributes._registerArt as 'HRA' | 'HRB',
};
await businessRecord.save();
});
this.handelsregister = new HandelsRegister(this);
this.jsonLDataProcessor = new JsonlDataProcessor(
this.config.germanBusinessDataDir,
async (entryArg) => {
const businessRecord = new this.CBusinessRecord();
businessRecord.id = await this.CBusinessRecord.getNewId();
businessRecord.data.name = entryArg.name;
businessRecord.data.germanParsedRegistration = {
court: entryArg.all_attributes.registered_office,
number: entryArg.all_attributes._registerNummer,
type: entryArg.all_attributes._registerArt as 'HRA' | 'HRB',
};
await businessRecord.save();
}
);
this.handelsregister = new HandelsRegister(this, this.config.downloadDir);
await this.handelsregister.start();
}

View File

@@ -1 +1,2 @@
export * from './classes.main.opendata.js';
export * from './stocks/index.js';

View File

@@ -4,12 +4,3 @@ export const packageDir = plugins.path.join(
plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url),
'../'
);
export const nogitDir = plugins.path.join(packageDir, './.nogit/');
plugins.smartfile.fs.ensureDirSync(nogitDir);
export const downloadDir = plugins.path.join(nogitDir, 'downloads');
plugins.smartfile.fs.ensureDirSync(downloadDir);
export const germanBusinessDataDir = plugins.path.join(nogitDir, 'germanbusinessdata');

View File

@@ -14,6 +14,7 @@ import * as smartbrowser from '@push.rocks/smartbrowser';
import * as smartdata from '@push.rocks/smartdata';
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartfile from '@push.rocks/smartfile';
import * as smartlog from '@push.rocks/smartlog';
import * as smartpath from '@push.rocks/smartpath';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartrequest from '@push.rocks/smartrequest';
@@ -30,6 +31,7 @@ export {
smartdata,
smartdelay,
smartfile,
smartlog,
smartpath,
smartpromise,
smartrequest,

View File

@@ -0,0 +1,309 @@
import * as plugins from '../plugins.js';
import type { IStockProvider, IProviderConfig, IProviderRegistry } from './interfaces/provider.js';
import type { IStockPrice, IStockQuoteRequest, IStockBatchQuoteRequest, IStockPriceError } from './interfaces/stockprice.js';
interface IProviderEntry {
provider: IStockProvider;
config: IProviderConfig;
lastError?: Error;
lastErrorTime?: Date;
successCount: number;
errorCount: number;
}
interface ICacheEntry {
price: IStockPrice;
timestamp: Date;
}
export class StockPriceService implements IProviderRegistry {
private providers = new Map<string, IProviderEntry>();
private cache = new Map<string, ICacheEntry>();
private logger = console;
private cacheConfig = {
ttl: 60000, // 60 seconds default
maxEntries: 1000
};
constructor(cacheConfig?: { ttl?: number; maxEntries?: number }) {
if (cacheConfig) {
this.cacheConfig = { ...this.cacheConfig, ...cacheConfig };
}
}
public register(provider: IStockProvider, config?: IProviderConfig): void {
const defaultConfig: IProviderConfig = {
enabled: true,
priority: provider.priority,
timeout: 10000,
retryAttempts: 2,
retryDelay: 1000
};
const mergedConfig = { ...defaultConfig, ...config };
this.providers.set(provider.name, {
provider,
config: mergedConfig,
successCount: 0,
errorCount: 0
});
console.log(`Registered provider: ${provider.name}`);
}
public unregister(providerName: string): void {
this.providers.delete(providerName);
console.log(`Unregistered provider: ${providerName}`);
}
public getProvider(name: string): IStockProvider | undefined {
return this.providers.get(name)?.provider;
}
public getAllProviders(): IStockProvider[] {
return Array.from(this.providers.values()).map(entry => entry.provider);
}
public getEnabledProviders(): IStockProvider[] {
return Array.from(this.providers.values())
.filter(entry => entry.config.enabled)
.sort((a, b) => (b.config.priority || 0) - (a.config.priority || 0))
.map(entry => entry.provider);
}
public async getPrice(request: IStockQuoteRequest): Promise<IStockPrice> {
const cacheKey = this.getCacheKey(request);
const cached = this.getFromCache(cacheKey);
if (cached) {
console.log(`Cache hit for ${request.ticker}`);
return cached;
}
const providers = this.getEnabledProviders();
if (providers.length === 0) {
throw new Error('No stock price providers available');
}
let lastError: Error | undefined;
for (const provider of providers) {
const entry = this.providers.get(provider.name)!;
try {
const price = await this.fetchWithRetry(
() => provider.fetchPrice(request),
entry.config
);
entry.successCount++;
this.addToCache(cacheKey, price);
console.log(`Successfully fetched ${request.ticker} from ${provider.name}`);
return price;
} catch (error) {
entry.errorCount++;
entry.lastError = error as Error;
entry.lastErrorTime = new Date();
lastError = error as Error;
console.warn(
`Provider ${provider.name} failed for ${request.ticker}: ${error.message}`
);
}
}
throw new Error(
`Failed to fetch price for ${request.ticker} from all providers. Last error: ${lastError?.message}`
);
}
public async getPrices(request: IStockBatchQuoteRequest): Promise<IStockPrice[]> {
const cachedPrices: IStockPrice[] = [];
const tickersToFetch: string[] = [];
// Check cache for each ticker
for (const ticker of request.tickers) {
const cacheKey = this.getCacheKey({ ticker, includeExtendedHours: request.includeExtendedHours });
const cached = this.getFromCache(cacheKey);
if (cached) {
cachedPrices.push(cached);
} else {
tickersToFetch.push(ticker);
}
}
if (tickersToFetch.length === 0) {
console.log(`All ${request.tickers.length} tickers served from cache`);
return cachedPrices;
}
const providers = this.getEnabledProviders();
if (providers.length === 0) {
throw new Error('No stock price providers available');
}
let lastError: Error | undefined;
let fetchedPrices: IStockPrice[] = [];
for (const provider of providers) {
const entry = this.providers.get(provider.name)!;
try {
fetchedPrices = await this.fetchWithRetry(
() => provider.fetchPrices({
tickers: tickersToFetch,
includeExtendedHours: request.includeExtendedHours
}),
entry.config
);
entry.successCount++;
// Cache the fetched prices
for (const price of fetchedPrices) {
const cacheKey = this.getCacheKey({
ticker: price.ticker,
includeExtendedHours: request.includeExtendedHours
});
this.addToCache(cacheKey, price);
}
console.log(
`Successfully fetched ${fetchedPrices.length} prices from ${provider.name}`
);
break;
} catch (error) {
entry.errorCount++;
entry.lastError = error as Error;
entry.lastErrorTime = new Date();
lastError = error as Error;
console.warn(
`Provider ${provider.name} failed for batch request: ${error.message}`
);
}
}
if (fetchedPrices.length === 0 && lastError) {
throw new Error(
`Failed to fetch prices from all providers. Last error: ${lastError.message}`
);
}
return [...cachedPrices, ...fetchedPrices];
}
public async checkProvidersHealth(): Promise<Map<string, boolean>> {
const health = new Map<string, boolean>();
for (const [name, entry] of this.providers) {
if (!entry.config.enabled) {
health.set(name, false);
continue;
}
try {
const isAvailable = await entry.provider.isAvailable();
health.set(name, isAvailable);
} catch (error) {
health.set(name, false);
console.error(`Health check failed for ${name}:`, error);
}
}
return health;
}
public getProviderStats(): Map<string, {
successCount: number;
errorCount: number;
lastError?: string;
lastErrorTime?: Date;
}> {
const stats = new Map();
for (const [name, entry] of this.providers) {
stats.set(name, {
successCount: entry.successCount,
errorCount: entry.errorCount,
lastError: entry.lastError?.message,
lastErrorTime: entry.lastErrorTime
});
}
return stats;
}
public clearCache(): void {
this.cache.clear();
console.log('Cache cleared');
}
public setCacheTTL(ttl: number): void {
this.cacheConfig.ttl = ttl;
console.log(`Cache TTL set to ${ttl}ms`);
}
private async fetchWithRetry<T>(
fetchFn: () => Promise<T>,
config: IProviderConfig
): Promise<T> {
const maxAttempts = config.retryAttempts || 1;
let lastError: Error | undefined;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await fetchFn();
} catch (error) {
lastError = error as Error;
if (attempt < maxAttempts) {
const delay = (config.retryDelay || 1000) * attempt;
console.log(`Retry attempt ${attempt} after ${delay}ms`);
await plugins.smartdelay.delayFor(delay);
}
}
}
throw lastError || new Error('Unknown error during fetch');
}
private getCacheKey(request: IStockQuoteRequest): string {
return `${request.ticker}:${request.includeExtendedHours || false}`;
}
private getFromCache(key: string): IStockPrice | null {
const entry = this.cache.get(key);
if (!entry) {
return null;
}
const age = Date.now() - entry.timestamp.getTime();
if (age > this.cacheConfig.ttl) {
this.cache.delete(key);
return null;
}
return entry.price;
}
private addToCache(key: string, price: IStockPrice): void {
// Enforce max entries limit
if (this.cache.size >= this.cacheConfig.maxEntries) {
// Remove oldest entry
const oldestKey = this.cache.keys().next().value;
if (oldestKey) {
this.cache.delete(oldestKey);
}
}
this.cache.set(key, {
price,
timestamp: new Date()
});
}
}

10
ts/stocks/index.ts Normal file
View File

@@ -0,0 +1,10 @@
// Export all interfaces
export * from './interfaces/stockprice.js';
export * from './interfaces/provider.js';
// Export main service
export * from './classes.stockservice.js';
// Export providers
export * from './providers/provider.yahoo.js';
export * from './providers/provider.marketstack.js';

View File

@@ -0,0 +1,36 @@
import type { IStockPrice, IStockQuoteRequest, IStockBatchQuoteRequest } from './stockprice.js';
export interface IStockProvider {
name: string;
priority: number;
fetchPrice(request: IStockQuoteRequest): Promise<IStockPrice>;
fetchPrices(request: IStockBatchQuoteRequest): Promise<IStockPrice[]>;
isAvailable(): Promise<boolean>;
supportsMarket?(market: string): boolean;
supportsTicker?(ticker: string): boolean;
readonly requiresAuth: boolean;
readonly rateLimit?: {
requestsPerMinute: number;
requestsPerDay?: number;
};
}
export interface IProviderConfig {
enabled: boolean;
priority?: number;
apiKey?: string;
timeout?: number;
retryAttempts?: number;
retryDelay?: number;
}
export interface IProviderRegistry {
register(provider: IStockProvider, config?: IProviderConfig): void;
unregister(providerName: string): void;
getProvider(name: string): IStockProvider | undefined;
getAllProviders(): IStockProvider[];
getEnabledProviders(): IStockProvider[];
}

View File

@@ -0,0 +1,36 @@
import * as plugins from '../../plugins.js';
export interface IStockPrice {
ticker: string;
price: number;
currency: string;
change: number;
changePercent: number;
previousClose: number;
timestamp: Date;
provider: string;
marketState: 'PRE' | 'REGULAR' | 'POST' | 'CLOSED';
exchange?: string;
exchangeName?: string;
}
type CheckStockPrice = plugins.tsclass.typeFest.IsEqual<
IStockPrice,
plugins.tsclass.finance.IStockPrice
>;
export interface IStockPriceError {
ticker: string;
error: string;
provider: string;
timestamp: Date;
}
export interface IStockQuoteRequest {
ticker: string;
includeExtendedHours?: boolean;
}
export interface IStockBatchQuoteRequest {
tickers: string[];
includeExtendedHours?: boolean;
}

View File

@@ -0,0 +1,200 @@
import * as plugins from '../../plugins.js';
import type { IStockProvider, IProviderConfig } from '../interfaces/provider.js';
import type { IStockPrice, IStockQuoteRequest, IStockBatchQuoteRequest } from '../interfaces/stockprice.js';
/**
* Marketstack API v2 Provider
* Documentation: https://marketstack.com/documentation_v2
*
* Features:
* - End-of-Day (EOD) stock prices
* - Supports 125,000+ tickers across 72+ exchanges worldwide
* - Requires API key authentication
*
* Rate Limits:
* - Free Plan: 100 requests/month (EOD only)
* - Basic Plan: 10,000 requests/month
* - Professional Plan: 100,000 requests/month
*
* Note: This provider returns EOD data, not real-time prices
*/
export class MarketstackProvider implements IStockProvider {
public name = 'Marketstack';
public priority = 80; // Lower than Yahoo (100) due to rate limits and EOD-only data
public readonly requiresAuth = true;
public readonly rateLimit = {
requestsPerMinute: undefined, // No per-minute limit specified
requestsPerDay: undefined // Varies by plan
};
private logger = console;
private baseUrl = 'https://api.marketstack.com/v2';
private apiKey: string;
constructor(apiKey: string, private config?: IProviderConfig) {
if (!apiKey) {
throw new Error('API key is required for Marketstack provider');
}
this.apiKey = apiKey;
}
/**
* Fetch latest EOD price for a single ticker
*/
public async fetchPrice(request: IStockQuoteRequest): Promise<IStockPrice> {
try {
const url = `${this.baseUrl}/tickers/${request.ticker}/eod/latest?access_key=${this.apiKey}`;
const response = await plugins.smartrequest.SmartRequest.create()
.url(url)
.timeout(this.config?.timeout || 10000)
.get();
const responseData = await response.json() as any;
// Check for API errors
if (responseData.error) {
throw new Error(`Marketstack API error: ${responseData.error.message || JSON.stringify(responseData.error)}`);
}
// For single ticker endpoint, response is direct object (not wrapped in data field)
if (!responseData || !responseData.close) {
throw new Error(`No data found for ticker ${request.ticker}`);
}
return this.mapToStockPrice(responseData);
} catch (error) {
this.logger.error(`Failed to fetch price for ${request.ticker}:`, error);
throw new Error(`Marketstack: Failed to fetch price for ${request.ticker}: ${error.message}`);
}
}
/**
* Fetch latest EOD prices for multiple tickers
*/
public async fetchPrices(request: IStockBatchQuoteRequest): Promise<IStockPrice[]> {
try {
const symbols = request.tickers.join(',');
const url = `${this.baseUrl}/eod/latest?access_key=${this.apiKey}&symbols=${symbols}`;
const response = await plugins.smartrequest.SmartRequest.create()
.url(url)
.timeout(this.config?.timeout || 15000)
.get();
const responseData = await response.json() as any;
// Check for API errors
if (responseData.error) {
throw new Error(`Marketstack API error: ${responseData.error.message || JSON.stringify(responseData.error)}`);
}
if (!responseData?.data || !Array.isArray(responseData.data)) {
throw new Error('Invalid response format from Marketstack API');
}
const prices: IStockPrice[] = [];
for (const data of responseData.data) {
try {
prices.push(this.mapToStockPrice(data));
} catch (error) {
this.logger.warn(`Failed to parse data for ${data.symbol}:`, error);
// Continue processing other tickers
}
}
if (prices.length === 0) {
throw new Error('No valid price data received from batch request');
}
return prices;
} catch (error) {
this.logger.error(`Failed to fetch batch prices:`, error);
throw new Error(`Marketstack: Failed to fetch batch prices: ${error.message}`);
}
}
/**
* Check if the Marketstack API is available and accessible
*/
public async isAvailable(): Promise<boolean> {
try {
// Test with a well-known ticker
const url = `${this.baseUrl}/tickers/AAPL/eod/latest?access_key=${this.apiKey}`;
const response = await plugins.smartrequest.SmartRequest.create()
.url(url)
.timeout(5000)
.get();
const responseData = await response.json() as any;
// Check if we got valid data (not an error)
// Single ticker endpoint returns direct object, not wrapped in data field
return !responseData.error && responseData.close !== undefined;
} catch (error) {
this.logger.warn('Marketstack provider is not available:', error);
return false;
}
}
/**
* Check if a market is supported
* Marketstack supports 72+ exchanges worldwide
*/
public supportsMarket(market: string): boolean {
// Marketstack has broad international coverage including:
// US, UK, DE, FR, JP, CN, HK, AU, CA, IN, etc.
const supportedMarkets = [
'US', 'UK', 'GB', 'DE', 'FR', 'JP', 'CN', 'HK', 'AU', 'CA',
'IN', 'BR', 'MX', 'IT', 'ES', 'NL', 'SE', 'CH', 'NO', 'DK'
];
return supportedMarkets.includes(market.toUpperCase());
}
/**
* Check if a ticker format is supported
*/
public supportsTicker(ticker: string): boolean {
// Basic validation - Marketstack supports most standard ticker formats
return /^[A-Z0-9\.\-]{1,10}$/.test(ticker.toUpperCase());
}
/**
* Map Marketstack API response to IStockPrice interface
*/
private mapToStockPrice(data: any): IStockPrice {
if (!data.close) {
throw new Error('Missing required price data');
}
// Calculate change and change percent
// EOD data: previous close is typically open price of the same day
// For better accuracy, we'd need previous day's close, but that requires another API call
const currentPrice = data.close;
const previousClose = data.open;
const change = currentPrice - previousClose;
const changePercent = previousClose !== 0 ? (change / previousClose) * 100 : 0;
// Parse timestamp
const timestamp = data.date ? new Date(data.date) : new Date();
const stockPrice: IStockPrice = {
ticker: data.symbol.toUpperCase(),
price: currentPrice,
currency: data.price_currency || 'USD',
change: change,
changePercent: changePercent,
previousClose: previousClose,
timestamp: timestamp,
provider: this.name,
marketState: 'CLOSED', // EOD data is always for closed markets
exchange: data.exchange,
exchangeName: data.exchange_code || data.name
};
return stockPrice;
}
}

View File

@@ -0,0 +1,161 @@
import * as plugins from '../../plugins.js';
import type { IStockProvider, IProviderConfig } from '../interfaces/provider.js';
import type { IStockPrice, IStockQuoteRequest, IStockBatchQuoteRequest } from '../interfaces/stockprice.js';
export class YahooFinanceProvider implements IStockProvider {
public name = 'Yahoo Finance';
public priority = 100;
public readonly requiresAuth = false;
public readonly rateLimit = {
requestsPerMinute: 100, // Conservative estimate
requestsPerDay: undefined
};
private logger = console;
private baseUrl = 'https://query1.finance.yahoo.com';
private userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
constructor(private config?: IProviderConfig) {}
public async fetchPrice(request: IStockQuoteRequest): Promise<IStockPrice> {
try {
const url = `${this.baseUrl}/v8/finance/chart/${request.ticker}`;
const response = await plugins.smartrequest.SmartRequest.create()
.url(url)
.headers({
'User-Agent': this.userAgent
})
.timeout(this.config?.timeout || 10000)
.get();
const responseData = await response.json() as any;
if (!responseData?.chart?.result?.[0]) {
throw new Error(`No data found for ticker ${request.ticker}`);
}
const data = responseData.chart.result[0];
const meta = data.meta;
if (!meta.regularMarketPrice) {
throw new Error(`No price data available for ${request.ticker}`);
}
const stockPrice: IStockPrice = {
ticker: request.ticker.toUpperCase(),
price: meta.regularMarketPrice,
currency: meta.currency || 'USD',
change: meta.regularMarketPrice - meta.previousClose,
changePercent: ((meta.regularMarketPrice - meta.previousClose) / meta.previousClose) * 100,
previousClose: meta.previousClose,
timestamp: new Date(meta.regularMarketTime * 1000),
provider: this.name,
marketState: this.determineMarketState(meta),
exchange: meta.exchange,
exchangeName: meta.exchangeName
};
return stockPrice;
} catch (error) {
console.error(`Failed to fetch price for ${request.ticker}:`, error);
throw new Error(`Yahoo Finance: Failed to fetch price for ${request.ticker}: ${error.message}`);
}
}
public async fetchPrices(request: IStockBatchQuoteRequest): Promise<IStockPrice[]> {
try {
const symbols = request.tickers.join(',');
const url = `${this.baseUrl}/v8/finance/spark?symbols=${symbols}&range=1d&interval=5m`;
const response = await plugins.smartrequest.SmartRequest.create()
.url(url)
.headers({
'User-Agent': this.userAgent
})
.timeout(this.config?.timeout || 15000)
.get();
const responseData = await response.json() as any;
const prices: IStockPrice[] = [];
for (const [ticker, data] of Object.entries(responseData)) {
if (!data || typeof data !== 'object') continue;
const sparkData = data as any;
if (!sparkData.previousClose || !sparkData.close?.length) {
console.warn(`Incomplete data for ${ticker}, skipping`);
continue;
}
const currentPrice = sparkData.close[sparkData.close.length - 1];
const timestamp = sparkData.timestamp?.[sparkData.timestamp.length - 1];
prices.push({
ticker: ticker.toUpperCase(),
price: currentPrice,
currency: sparkData.currency || 'USD',
change: currentPrice - sparkData.previousClose,
changePercent: ((currentPrice - sparkData.previousClose) / sparkData.previousClose) * 100,
previousClose: sparkData.previousClose,
timestamp: timestamp ? new Date(timestamp * 1000) : new Date(),
provider: this.name,
marketState: sparkData.marketState || 'REGULAR',
exchange: sparkData.exchange,
exchangeName: sparkData.exchangeName
});
}
if (prices.length === 0) {
throw new Error('No valid price data received from batch request');
}
return prices;
} catch (error) {
console.error(`Failed to fetch batch prices:`, error);
throw new Error(`Yahoo Finance: Failed to fetch batch prices: ${error.message}`);
}
}
public async isAvailable(): Promise<boolean> {
try {
// Test with a well-known ticker
await this.fetchPrice({ ticker: 'AAPL' });
return true;
} catch (error) {
console.warn('Yahoo Finance provider is not available:', error);
return false;
}
}
public supportsMarket(market: string): boolean {
// Yahoo Finance supports most major markets
const supportedMarkets = ['US', 'UK', 'DE', 'FR', 'JP', 'CN', 'HK', 'AU', 'CA'];
return supportedMarkets.includes(market.toUpperCase());
}
public supportsTicker(ticker: string): boolean {
// Basic validation - Yahoo supports most tickers
return /^[A-Z0-9\.\-]{1,10}$/.test(ticker.toUpperCase());
}
private determineMarketState(meta: any): 'PRE' | 'REGULAR' | 'POST' | 'CLOSED' {
const marketState = meta.marketState?.toUpperCase();
switch (marketState) {
case 'PRE':
return 'PRE';
case 'POST':
return 'POST';
case 'REGULAR':
return 'REGULAR';
default:
// Check if market is currently open based on timestamps
const now = Date.now() / 1000;
const regularMarketTime = meta.regularMarketTime;
const timeDiff = now - regularMarketTime;
// If last update was more than 1 hour ago, market is likely closed
return timeDiff > 3600 ? 'CLOSED' : 'REGULAR';
}
}
}