(20241226) Kafka certs in order.
This commit is contained in:
@@ -146,6 +146,53 @@ class AsyncNSEBase:
|
||||
def debug_everything(self):
|
||||
self._debug_only_errors = False
|
||||
|
||||
# ┓┏ ┓
|
||||
# ┣┫┏┓┃┏┓┏┓┏┓┏
|
||||
# ┛┗┗ ┗┣┛┗ ┛ ┛
|
||||
# ┛
|
||||
|
||||
@staticmethod
|
||||
def parse_datetime_string(
|
||||
dt_str: str,
|
||||
dt_formats: str | List[str] = None,
|
||||
input_tz: str = date_time.TIMEZONE_IST,
|
||||
output_tz: str = date_time.TIMEZONE_UTC
|
||||
) -> datetime.datetime | None:
|
||||
|
||||
"""
|
||||
Handles the date-time conversion and normalization for strings received from NSE.
|
||||
:param dt_str: The string value as received from NSE.
|
||||
:param dt_formats: The format(s) in which to attempt to parse the date string.
|
||||
:param input_tz: The timezone in which the input must be assumed to be.
|
||||
:param output_tz: The timezone the output must be converted to.
|
||||
:return: The parsed datetime object, or null if parsing fails.
|
||||
"""
|
||||
|
||||
# Basic input formatting:
|
||||
dt_formats = dt_formats or "%d-%b-%Y"
|
||||
|
||||
# Try to parse the date-time string:
|
||||
if isinstance(dt_formats, list):
|
||||
dt_obj = date_time.parse_date_time(
|
||||
input_value = dt_str,
|
||||
date_formats = dt_formats
|
||||
)
|
||||
else:
|
||||
try: dt_obj = datetime.datetime.strptime(dt_str, dt_formats)
|
||||
except: dt_obj = None
|
||||
|
||||
# If the parsing failed:
|
||||
if not dt_obj: return None
|
||||
|
||||
# If parsed successfully:
|
||||
return date_time.to_timezone(
|
||||
datetime_object = date_time.as_if_timezone(
|
||||
datetime_object = dt_obj,
|
||||
timezone = input_tz
|
||||
),
|
||||
timezone = output_tz
|
||||
)
|
||||
|
||||
# ┏┓ ┓ • ┳┳┓ ┓ •
|
||||
# ┃ ┏┓┏┓┃┏┓┏┓┏ ┃┃┃┏┓┃┏┓┏┓┏┓
|
||||
# ┗┛┗┛┗┛┛┗┗┗ ┛ ┛ ┗┗┻┛┗┗┛┗┗┫
|
||||
|
||||
@@ -55,6 +55,7 @@ import httpx
|
||||
|
||||
# To work with date and time:
|
||||
import datetime
|
||||
import pytz
|
||||
|
||||
# To work with datatypes:
|
||||
from typing import Any, List
|
||||
@@ -159,8 +160,8 @@ class NSECorporateActions(AsyncNSEBase):
|
||||
# Done here:
|
||||
return api_response
|
||||
|
||||
@staticmethod
|
||||
def format_data(
|
||||
self,
|
||||
raw_json: List[dict],
|
||||
timestamp: datetime.datetime = None,
|
||||
raise_exception: bool = False
|
||||
@@ -188,26 +189,25 @@ class NSECorporateActions(AsyncNSEBase):
|
||||
try:
|
||||
|
||||
# Format the data:
|
||||
formatted_data = [
|
||||
{
|
||||
formatted_data = []
|
||||
for action in raw_json:
|
||||
formatted_data.append({
|
||||
"scrapeTs": timestamp,
|
||||
"symbol": action["symbol"],
|
||||
"company": action["comp"],
|
||||
"isin": action["isin"],
|
||||
"segment": action["series"],
|
||||
"faceVal": action["faceVal"],
|
||||
"action": action["subject"],
|
||||
"exDate": action["exDate"],
|
||||
"recDate": action["recDate"],
|
||||
"bcStartDate": action["bcStartDate"],
|
||||
"bcEndDate": action["bcEndDate"],
|
||||
"ndStartDate": action["ndStartDate"],
|
||||
"caBroadcastDate": action["caBroadcastDate"],
|
||||
"ind": action["ind"],
|
||||
# "action": action["subject"],
|
||||
# "action": action["subject"],
|
||||
} for action in raw_json
|
||||
]
|
||||
"faceVal": float(action["faceVal"]),
|
||||
"action": action["subject"],
|
||||
"exDate": self.parse_datetime_string(action["exDate"], ["%d-%b-%Y"]),
|
||||
"recDate": self.parse_datetime_string(action["recDate"]),
|
||||
"bcStartDate": self.parse_datetime_string(action["bcStartDate"]),
|
||||
"bcEndDate": self.parse_datetime_string(action["bcEndDate"]),
|
||||
"ndStartDate": self.parse_datetime_string(action["ndStartDate"]),
|
||||
"ndEndDate": self.parse_datetime_string(action["ndEndDate"]),
|
||||
"caBroadcastDate": self.parse_datetime_string(action["caBroadcastDate"])
|
||||
})
|
||||
|
||||
# If something goes wrong:
|
||||
except Exception as exception:
|
||||
@@ -248,14 +248,14 @@ if __name__ == "__main__":
|
||||
|
||||
# Get and show the data:
|
||||
to_date = date_time.get_current_ist_date_time()
|
||||
from_date = to_date - datetime.timedelta(days = 31)
|
||||
from_date = to_date - datetime.timedelta(days = 365)
|
||||
print("FROM :", from_date)
|
||||
print("TO :", to_date)
|
||||
api_response = await my_nse.get_data(
|
||||
action_type = NSECorporateActions.TYPE_EQUITIES,
|
||||
from_date = from_date,
|
||||
to_date = to_date,
|
||||
return_raw = True
|
||||
return_raw = False
|
||||
)
|
||||
print("SUMMARY:", api_response.to_markdown(), "\n---\n\n")
|
||||
if api_response.success: print("CORPORATE ACTIONS:", json.to_string(api_response.data, default = str))
|
||||
|
||||
Reference in New Issue
Block a user