From 61777d7f05a84df44b4ea08710523cd6a6946196 Mon Sep 17 00:00:00 2001 From: khushal Date: Fri, 27 Dec 2024 18:36:16 +0530 Subject: [PATCH] (20241226) Kafka certs in order. --- utils_v2/nse/controllers/base.py | 47 +++++++++++++++++++ .../controllers/calendar/corporate_actions.py | 34 +++++++------- wsio/finstitutions/trading/main.py | 6 +-- 3 files changed, 67 insertions(+), 20 deletions(-) diff --git a/utils_v2/nse/controllers/base.py b/utils_v2/nse/controllers/base.py index 42768c1..72f8c6c 100644 --- a/utils_v2/nse/controllers/base.py +++ b/utils_v2/nse/controllers/base.py @@ -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 + ) + # ┏┓ ┓ • ┳┳┓ ┓ • # ┃ ┏┓┏┓┃┏┓┏┓┏ ┃┃┃┏┓┃┏┓┏┓┏┓ # ┗┛┗┛┗┛┛┗┗┗ ┛ ┛ ┗┗┻┛┗┗┛┗┗┫ diff --git a/utils_v2/nse/controllers/calendar/corporate_actions.py b/utils_v2/nse/controllers/calendar/corporate_actions.py index d56923d..f6905d1 100644 --- a/utils_v2/nse/controllers/calendar/corporate_actions.py +++ b/utils_v2/nse/controllers/calendar/corporate_actions.py @@ -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)) diff --git a/wsio/finstitutions/trading/main.py b/wsio/finstitutions/trading/main.py index ae83bd7..6fa7430 100644 --- a/wsio/finstitutions/trading/main.py +++ b/wsio/finstitutions/trading/main.py @@ -241,9 +241,9 @@ async def init(): bootstrap_servers = "del.ditscentre.in:9092", security_protocol = "SSL", ssl_context = get_ssl_context( - ca_file = "../../../creds/kafka/cert_authority.pem", - cert_file = "../../../creds/kafka/fullchain.pem", - key_file = "../../../creds/kafka/privkey.pem" + ca_file = "/etc/ssl/dbu/ca.pem", + cert_file = "/etc/ssl/dbu/fullchain.pem", + key_file = "/etc/ssl/dbu/privkey.pem" # ca_file = os.path.join(parent_dir, "creds", "kafka", "cert_authority.pem"), # cert_file = os.path.join(parent_dir, "creds", "kafka", "fullchain.pem"), # key_file = os.path.join(parent_dir, "creds", "kafka", "privkey.pem")