Added 'financials' endpoint script with fixes for legacy CDN paths#7
Added 'financials' endpoint script with fixes for legacy CDN paths#7hareeshkar wants to merge 1 commit intoGH0STH4CKER:mainfrom
Conversation
CSE Financial Documents Archive API — Python Documentation Description A Python implementation for interacting with the /api/financials endpoint. This script provides a structured interface to access the official document repository for any listed security on the Colombo Stock Exchange (CSE). Data Coverage The API provides categorized PDF documents dating from 2012 to the present: Annual Reports: Official audited year-end financial statements. Quarterly Reports: Unaudited interim financial results released every three months. Legal & Other: Regulatory filings including Prospectuses, Trust Deeds, and supplementary disclosures. Endpoint Specifications URL: https://www.cse.lk/api/financials Method: POST Payload Type: application/x-www-form-urlencoded Required Parameter: symbol (e.g., SAMP.N0000) Technical Implementation Request Headers: The API requires Origin and Referer headers to validate requests. Without these, the server may return empty data arrays. CDN Path Normalization: Resolves relative paths from the API against the official CDN (https://cdn.cse.lk/). Automatically adds missing /cmt/ prefixes for legacy files. URL-encodes filenames containing spaces or special characters to ensure valid download links. Data Categorization: The JSON response is parsed into three categories for easy integration: infoAnnualData infoQuarterlyData infoOtherData How to Use Send a POST request to /api/financials with the desired company symbol. The script parses the JSON response, converting timestamps into human-readable dates. Generates direct download links for each document, ready for browser access or automated downloading. ### Usage Example ```python from cse_financials import get_financial_archives # Fetch all available financial documents for Sampath Bank symbol = "SAMP.N0000" get_financial_archives(symbol)
There was a problem hiding this comment.
Pull request overview
This PR adds a Python script to interact with the CSE /api/financials endpoint, enabling retrieval of financial documents (Annual Reports, Quarterly Reports, and Legal/Other Documents) for listed securities. The implementation includes path normalization for legacy CDN files and URL encoding for special characters.
Changes:
- Added
example_financial_reports.pyscript with functions to fetch and display financial documents from the CSE API - Implemented CDN path normalization to fix legacy file paths missing the 'cmt/' prefix
- Added URL encoding support for filenames with special characters
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return "Unknown Date" | ||
| try: | ||
| return datetime.fromtimestamp(timestamp_ms / 1000.0).strftime('%Y-%m-%d') | ||
| except: |
There was a problem hiding this comment.
Bare except clause catches all exceptions including system-exiting exceptions like KeyboardInterrupt and SystemExit. Use a specific exception type like 'except (ValueError, OSError, OverflowError):' to catch only timestamp conversion errors.
| except: | |
| except (TypeError, ValueError, OverflowError, OSError): |
| if not raw_path.startswith("cmt/"): | ||
| raw_path = f"cmt/{raw_path}" |
There was a problem hiding this comment.
The logic assumes all paths without 'cmt/' prefix should have it added. However, if a path already starts with '/cmt/', it will become 'cmt//cmt/', creating an invalid URL. Consider checking for both 'cmt/' and '/cmt/' patterns, or strip leading slashes before the check.
| Fetches and prints all available financial documents for a given symbol. | ||
| """ | ||
| headers = { | ||
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", |
There was a problem hiding this comment.
The User-Agent string references Chrome 120.0.0.0, which may become outdated. Consider using a more generic User-Agent string or extracting it to a constant with a comment explaining why this specific version is needed.
/api/financials — CSE Financial Documents Archive
Description
This Python script provides a structured interface to interact with the
/api/financialsendpoint of the Colombo Stock Exchange (CSE).It allows easy access to the official document repository for any listed security.
Data Coverage
The API provides categorized PDF documents dating from 2012 to the present:
Endpoint Specifications
https://www.cse.lk/api/financialsPOSTapplication/x-www-form-urlencodedsymbol(e.g.,SAMP.N0000)Technical Implementation
Request Headers
The API requires specific headers to validate requests:
Origin: https://www.cse.lkReferer: https://www.cse.lk/company-profile?symbol={symbol}CDN Path Normalization
The script ensures all document links are valid by:
https://cdn.cse.lk//cmt/prefixes for legacy files (2012–2018)Data Categorization
The JSON response is parsed into three categories for easy integration:
infoAnnualData→ Annual ReportsinfoQuarterlyData→ Quarterly ReportsinfoOtherData→ Legal & Other DocumentsHow to Use
POSTrequest to/api/financialswith the desired company symbol.Usage Example