(20241226) Kafka certs in order.

This commit is contained in:
2024-12-27 18:36:16 +05:30
parent 6f2285c536
commit 61777d7f05
3 changed files with 67 additions and 20 deletions
+47
View File
@@ -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))
+3 -3
View File
@@ -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")