Skip to content

Added 'financials' endpoint script with fixes for legacy CDN paths#7

Open
hareeshkar wants to merge 1 commit intoGH0STH4CKER:mainfrom
hareeshkar:main
Open

Added 'financials' endpoint script with fixes for legacy CDN paths#7
hareeshkar wants to merge 1 commit intoGH0STH4CKER:mainfrom
hareeshkar:main

Conversation

@hareeshkar
Copy link
Copy Markdown

@hareeshkar hareeshkar commented Jan 15, 2026

/api/financials — CSE Financial Documents Archive

Description

This Python script provides a structured interface to interact with the /api/financials endpoint 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:

  • 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 specific headers to validate requests:

  • Origin: https://www.cse.lk
  • Referer: https://www.cse.lk/company-profile?symbol={symbol}

⚠️ Without these headers, the server may return empty arrays.

CDN Path Normalization

The script ensures all document links are valid by:

  1. Resolving relative paths from the API against the official CDN: https://cdn.cse.lk/
  2. Automatically adding missing /cmt/ prefixes for legacy files (2012–2018)
  3. URL-encoding filenames containing spaces or special characters

Data Categorization

The JSON response is parsed into three categories for easy integration:

  • infoAnnualData → Annual Reports
  • infoQuarterlyData → Quarterly Reports
  • infoOtherData → Legal & Other Documents

How to Use

  1. Send a POST request to /api/financials with the desired company symbol.
  2. The script parses the JSON response, converting timestamps to human-readable dates.
  3. Direct download links for each document are generated, ready for browser access or automated downloading.

Usage Example

from cse_financials import get_financial_archives

# Fetch all available financial documents for Sampath Bank
symbol = "SAMP.N0000"
get_financial_archives(symbol)

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)
Copilot AI review requested due to automatic review settings January 15, 2026 14:14
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py script 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:
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
except:
except (TypeError, ValueError, OverflowError, OSError):

Copilot uses AI. Check for mistakes.
Comment on lines +50 to +51
if not raw_path.startswith("cmt/"):
raw_path = f"cmt/{raw_path}"
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
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",
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants