|
8 | 8 | import pandas as pd |
9 | 9 | import requests |
10 | 10 |
|
| 11 | +from getbhavcopy.config import load_config |
11 | 12 | from getbhavcopy.settings_windows import load_symbol_mapping |
12 | 13 |
|
13 | 14 | logger = logging.getLogger("getbhavcopy") |
@@ -150,39 +151,46 @@ def get_nse_indices_data_for_date(self, d): |
150 | 151 | return self._apply_symbol_mapping(df) |
151 | 152 |
|
152 | 153 | def process_day(self, day): |
153 | | - |
154 | 154 | date_str = day.strftime("%Y-%m-%d") |
155 | | - |
156 | | - file_path = os.path.join( |
157 | | - self.SaveFolderName, |
158 | | - ( |
159 | | - f"{date_str}-NSE-EQ.csv" |
160 | | - if self.Output_File_Formate == "CSV" |
161 | | - else f"{date_str}-NSE-EQ.txt" |
162 | | - ), |
163 | | - ) |
164 | | - |
165 | | - if os.path.exists(file_path): |
166 | | - logger.info(f"Skipping existing {date_str}") |
167 | | - |
168 | | - return "skipped" |
| 155 | + ext = "csv" if self.Output_File_Formate == "CSV" else "txt" |
| 156 | + |
| 157 | + _cfg = load_config() |
| 158 | + _pattern = _cfg.get("filename_pattern", "").strip() or "{date}-NSE-EQ" |
| 159 | + _idx_pattern = _cfg.get("idx_filename_pattern", "").strip() or "{date}-NSE-IDX" |
| 160 | + _split = bool(_cfg.get("split_eq_idx", False)) |
| 161 | + _eq_name = _pattern.replace("{date}", date_str) |
| 162 | + _idx_name = _idx_pattern.replace("{date}", date_str) |
| 163 | + |
| 164 | + if _split: |
| 165 | + eq_path = os.path.join(self.SaveFolderName, f"{_eq_name}.{ext}") |
| 166 | + idx_path = os.path.join(self.SaveFolderName, f"{_idx_name}.{ext}") |
| 167 | + if os.path.exists(eq_path) and os.path.exists(idx_path): |
| 168 | + logger.info(f"Skipping existing {date_str}") |
| 169 | + return "skipped" |
| 170 | + else: |
| 171 | + file_path = os.path.join(self.SaveFolderName, f"{_eq_name}.{ext}") |
| 172 | + if os.path.exists(file_path): |
| 173 | + logger.info(f"Skipping existing {date_str}") |
| 174 | + return "skipped" |
169 | 175 |
|
170 | 176 | try: |
171 | 177 | eq = self.get_equity_bhavcopy_for_date(day) |
172 | | - |
173 | 178 | idx = self.get_nse_indices_data_for_date(day) |
174 | 179 |
|
175 | | - final_df = pd.concat([eq, idx], ignore_index=True) |
176 | | - |
177 | | - final_df.to_csv(file_path, index=False, header=False) |
178 | | - |
179 | | - logger.info(f"Saved {file_path}") |
| 180 | + if _split: |
| 181 | + eq.to_csv(eq_path, index=False, header=False) |
| 182 | + idx.to_csv(idx_path, index=False, header=False) |
| 183 | + logger.info(f"Saved equity → {eq_path}") |
| 184 | + logger.info(f"Saved indices → {idx_path}") |
| 185 | + else: |
| 186 | + final_df = pd.concat([eq, idx], ignore_index=True) |
| 187 | + final_df.to_csv(file_path, index=False, header=False) |
| 188 | + logger.info(f"Saved {file_path}") |
180 | 189 |
|
181 | 190 | return "success" |
182 | 191 |
|
183 | 192 | except Exception as e: |
184 | 193 | logger.warning(f"Failed {date_str} : {str(e)}") |
185 | | - |
186 | 194 | return "failed" |
187 | 195 |
|
188 | 196 | def get_bhavcopy(self): |
|
0 commit comments