| """
|
| Batch Download NSE F&O Data Module
|
| """
|
|
|
| import gradio as gr
|
| import datetime
|
| from typing import List
|
|
|
| class BatchDownload:
|
| def __init__(self):
|
| self.download_dir = "nse_derivatives_data"
|
| self.processed_dir = "processed_derivatives"
|
|
|
|
|
| from .single_day_download import SingleDayDownload
|
| self.single_download = SingleDayDownload()
|
|
|
| def download_multiple_dates(self, start_date: str, end_date: str, force_download: bool = False) -> str:
|
| """Download derivatives data for a date range"""
|
| try:
|
| if not start_date or not end_date:
|
| return "β Please select both start and end dates"
|
|
|
| start_obj = datetime.datetime.strptime(start_date, "%Y-%m-%d").date()
|
| end_obj = datetime.datetime.strptime(end_date, "%Y-%m-%d").date()
|
|
|
| if start_obj > end_obj:
|
| return "β Start date cannot be after end date"
|
|
|
| if end_obj > datetime.date.today():
|
| return "β Cannot download data for future dates"
|
|
|
| output = f"## π Batch Download ({start_date} to {end_date})\n\n"
|
| successful_downloads = 0
|
| total_days = 0
|
|
|
| current_date = start_obj
|
| while current_date <= end_obj:
|
|
|
| if current_date.weekday() < 5:
|
| total_days += 1
|
| result = self.single_download._download_derivatives_data(current_date, force_download)
|
|
|
| if result['success']:
|
| successful_downloads += 1
|
| output += f"β
{current_date}: Success\n"
|
| else:
|
| output += f"β {current_date}: Failed\n"
|
|
|
| current_date += datetime.timedelta(days=1)
|
|
|
| output += f"\nπ **Summary:** {successful_downloads}/{total_days} trading days downloaded"
|
| return output
|
|
|
| except Exception as e:
|
| return f"β Error: {str(e)}"
|
|
|
| def create_interface(self):
|
| """Create batch download interface"""
|
| gr.Markdown("### π Batch Download F&O Data")
|
| gr.Markdown("Download NSE derivatives data for a date range")
|
|
|
| with gr.Row():
|
| start_date = gr.Textbox(
|
| label="Start Date (YYYY-MM-DD)",
|
| placeholder="e.g., 2024-01-01",
|
| value=(datetime.date.today() - datetime.timedelta(days=7)).strftime("%Y-%m-%d")
|
| )
|
| end_date = gr.Textbox(
|
| label="End Date (YYYY-MM-DD)",
|
| placeholder="e.g., 2024-01-15",
|
| value=datetime.date.today().strftime("%Y-%m-%d")
|
| )
|
| force_download = gr.Checkbox(label="Force Re-download", value=False)
|
|
|
| batch_download_btn = gr.Button("π₯ Download Batch Data", variant="primary")
|
| batch_download_output = gr.Markdown()
|
|
|
| batch_download_btn.click(
|
| fn=self.download_multiple_dates,
|
| inputs=[start_date, end_date, force_download],
|
| outputs=batch_download_output
|
| )
|
|
|
| gr.Markdown("---")
|
| gr.Markdown("### π‘ Tips")
|
| gr.Markdown("- Downloads data for weekdays only (Monday-Friday)")
|
| gr.Markdown("- Large date ranges may take several minutes")
|
| gr.Markdown("- Check 'Force Re-download' to overwrite existing files") |