Spaces:
Sleeping
Sleeping
fresh one
Browse files
app.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import pytz
|
| 6 |
+
|
| 7 |
+
def validate_date_format(date_str):
|
| 8 |
+
"""Validate if the date is in dd-mm-yyyy format"""
|
| 9 |
+
try:
|
| 10 |
+
datetime.strptime(date_str, "%d-%m-%Y")
|
| 11 |
+
return True
|
| 12 |
+
except ValueError:
|
| 13 |
+
return False
|
| 14 |
+
|
| 15 |
+
def convert_to_unix_millis(date_str, time_str):
|
| 16 |
+
"""Convert dd-mm-yyyy date and time to Unix timestamp in milliseconds (IST)"""
|
| 17 |
+
ist = pytz.timezone('Asia/Kolkata')
|
| 18 |
+
datetime_str = f"{date_str} {time_str}"
|
| 19 |
+
dt = datetime.strptime(datetime_str, "%d-%m-%Y %H:%M")
|
| 20 |
+
dt_ist = ist.localize(dt)
|
| 21 |
+
return int(dt_ist.timestamp() * 1000)
|
| 22 |
+
|
| 23 |
+
def epoch_to_ist_time(epoch_seconds):
|
| 24 |
+
"""Convert epoch timestamp (seconds) to IST time string (HH:MM:SS)"""
|
| 25 |
+
ist = pytz.timezone('Asia/Kolkata')
|
| 26 |
+
dt = datetime.fromtimestamp(epoch_seconds, tz=pytz.UTC)
|
| 27 |
+
dt_ist = dt.astimezone(ist)
|
| 28 |
+
return dt_ist.strftime("%H:%M:%S")
|
| 29 |
+
|
| 30 |
+
def fetch_stock_data(ticker, date):
|
| 31 |
+
"""Fetch stock data from Groww API and return simplified JSON"""
|
| 32 |
+
|
| 33 |
+
# Validate date format
|
| 34 |
+
if not validate_date_format(date):
|
| 35 |
+
return "Error: Please enter date in dd-mm-yyyy format"
|
| 36 |
+
|
| 37 |
+
# Validate ticker
|
| 38 |
+
if not ticker or ticker.strip() == "":
|
| 39 |
+
return "Error: Please enter a valid ticker symbol"
|
| 40 |
+
|
| 41 |
+
ticker = ticker.strip().upper()
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
# Calculate start and end timestamps
|
| 45 |
+
start_millis = convert_to_unix_millis(date, "00:05")
|
| 46 |
+
end_millis = convert_to_unix_millis(date, "23:55")
|
| 47 |
+
|
| 48 |
+
# Construct API URL
|
| 49 |
+
url = f"https://groww.in/v1/api/charting_service/v4/chart/exchange/NSE/segment/CASH/{ticker}"
|
| 50 |
+
params = {
|
| 51 |
+
"endTimeInMillis": end_millis,
|
| 52 |
+
"intervalInMinutes": 1,
|
| 53 |
+
"startTimeInMillis": start_millis
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
# Make API request
|
| 57 |
+
response = requests.get(url, params=params, timeout=10)
|
| 58 |
+
|
| 59 |
+
# Check if request was successful
|
| 60 |
+
if response.status_code != 200:
|
| 61 |
+
return "Invalid ticker or no data available for the given date."
|
| 62 |
+
|
| 63 |
+
data = response.json()
|
| 64 |
+
|
| 65 |
+
# Check if candles data exists
|
| 66 |
+
if "candles" not in data or not data["candles"]:
|
| 67 |
+
return "Invalid ticker or no data available for the given date."
|
| 68 |
+
|
| 69 |
+
# Process candles data
|
| 70 |
+
simplified_data = []
|
| 71 |
+
for candle in data["candles"]:
|
| 72 |
+
epoch_seconds = candle[0]
|
| 73 |
+
opening_price = candle[1]
|
| 74 |
+
|
| 75 |
+
time_ist = epoch_to_ist_time(epoch_seconds)
|
| 76 |
+
|
| 77 |
+
simplified_data.append({
|
| 78 |
+
"time": time_ist,
|
| 79 |
+
"open": opening_price
|
| 80 |
+
})
|
| 81 |
+
|
| 82 |
+
# Return as JSON string
|
| 83 |
+
return json.dumps(simplified_data, indent=2)
|
| 84 |
+
|
| 85 |
+
except requests.exceptions.Timeout:
|
| 86 |
+
return "Error: Request timed out. Please try again."
|
| 87 |
+
except requests.exceptions.RequestException as e:
|
| 88 |
+
return "Invalid ticker or no data available for the given date."
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return f"Error: An unexpected error occurred - {str(e)}"
|
| 91 |
+
|
| 92 |
+
# Create Gradio interface
|
| 93 |
+
with gr.Blocks(title="Groww Stock Data Fetcher") as app:
|
| 94 |
+
gr.Markdown("# Groww Stock Data Fetcher")
|
| 95 |
+
gr.Markdown("Fetch intraday 1-minute candle data for NSE stocks")
|
| 96 |
+
|
| 97 |
+
with gr.Row():
|
| 98 |
+
with gr.Column():
|
| 99 |
+
ticker_input = gr.Textbox(
|
| 100 |
+
label="Ticker / Symbol",
|
| 101 |
+
placeholder="e.g., RELIANCE, TCS, HDFCBANK",
|
| 102 |
+
lines=1
|
| 103 |
+
)
|
| 104 |
+
date_input = gr.Textbox(
|
| 105 |
+
label="Date (dd-mm-yyyy)",
|
| 106 |
+
placeholder="e.g., 05-11-2025",
|
| 107 |
+
lines=1
|
| 108 |
+
)
|
| 109 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 110 |
+
|
| 111 |
+
with gr.Row():
|
| 112 |
+
output = gr.Textbox(
|
| 113 |
+
label="Output",
|
| 114 |
+
lines=20,
|
| 115 |
+
max_lines=30,
|
| 116 |
+
show_copy_button=True
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
submit_btn.click(
|
| 120 |
+
fn=fetch_stock_data,
|
| 121 |
+
inputs=[ticker_input, date_input],
|
| 122 |
+
outputs=output
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
gr.Markdown("### Instructions")
|
| 126 |
+
gr.Markdown("""
|
| 127 |
+
1. Enter a valid NSE stock ticker (e.g., RELIANCE, TCS, HDFCBANK)
|
| 128 |
+
2. Enter the date in **dd-mm-yyyy** format (e.g., 05-11-2025)
|
| 129 |
+
3. Click Submit to fetch the data
|
| 130 |
+
4. The output will show timestamp (IST) and opening price for each 1-minute candle
|
| 131 |
+
""")
|
| 132 |
+
|
| 133 |
+
# Launch the app
|
| 134 |
+
if __name__ == "__main__":
|
| 135 |
+
app.launch()
|