Spaces:
Sleeping
Sleeping
WITH LOGS
Browse files
app.py
CHANGED
|
@@ -30,7 +30,11 @@ def epoch_to_ist_time(epoch_seconds):
|
|
| 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 |
|
|
@@ -39,12 +43,16 @@ def fetch_stock_data(ticker, date):
|
|
| 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 = {
|
|
@@ -53,18 +61,36 @@ def fetch_stock_data(ticker, date):
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
data = response.json()
|
|
|
|
| 64 |
|
| 65 |
# Check if candles data exists
|
| 66 |
if "candles" not in data or not data["candles"]:
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Process candles data
|
| 70 |
simplified_data = []
|
|
@@ -79,15 +105,23 @@ def fetch_stock_data(ticker, date):
|
|
| 79 |
"open": opening_price
|
| 80 |
})
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
except requests.exceptions.Timeout:
|
| 86 |
-
|
|
|
|
| 87 |
except requests.exceptions.RequestException as e:
|
| 88 |
-
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
# Create Gradio interface
|
| 93 |
with gr.Blocks(title="Groww Stock Data Fetcher") as app:
|
|
|
|
| 30 |
def fetch_stock_data(ticker, date):
|
| 31 |
"""Fetch stock data from Groww API and return simplified JSON"""
|
| 32 |
|
| 33 |
+
debug_log = []
|
| 34 |
+
|
| 35 |
# Validate date format
|
| 36 |
+
debug_log.append(f"[DEBUG] Input - Ticker: {ticker}, Date: {date}")
|
| 37 |
+
|
| 38 |
if not validate_date_format(date):
|
| 39 |
return "Error: Please enter date in dd-mm-yyyy format"
|
| 40 |
|
|
|
|
| 43 |
return "Error: Please enter a valid ticker symbol"
|
| 44 |
|
| 45 |
ticker = ticker.strip().upper()
|
| 46 |
+
debug_log.append(f"[DEBUG] Cleaned Ticker: {ticker}")
|
| 47 |
|
| 48 |
try:
|
| 49 |
# Calculate start and end timestamps
|
| 50 |
start_millis = convert_to_unix_millis(date, "00:05")
|
| 51 |
end_millis = convert_to_unix_millis(date, "23:55")
|
| 52 |
|
| 53 |
+
debug_log.append(f"[DEBUG] Start Time (IST 00:05): {start_millis} ms")
|
| 54 |
+
debug_log.append(f"[DEBUG] End Time (IST 23:55): {end_millis} ms")
|
| 55 |
+
|
| 56 |
# Construct API URL
|
| 57 |
url = f"https://groww.in/v1/api/charting_service/v4/chart/exchange/NSE/segment/CASH/{ticker}"
|
| 58 |
params = {
|
|
|
|
| 61 |
"startTimeInMillis": start_millis
|
| 62 |
}
|
| 63 |
|
| 64 |
+
# Full URL with parameters
|
| 65 |
+
full_url = f"{url}?endTimeInMillis={end_millis}&intervalInMinutes=1&startTimeInMillis={start_millis}"
|
| 66 |
+
debug_log.append(f"[DEBUG] Full API URL: {full_url}")
|
| 67 |
+
|
| 68 |
# Make API request
|
| 69 |
+
debug_log.append("[DEBUG] Making API request...")
|
| 70 |
response = requests.get(url, params=params, timeout=10)
|
| 71 |
|
| 72 |
+
debug_log.append(f"[DEBUG] Response Status Code: {response.status_code}")
|
| 73 |
+
debug_log.append(f"[DEBUG] Response Headers: {dict(response.headers)}")
|
| 74 |
+
|
| 75 |
# Check if request was successful
|
| 76 |
if response.status_code != 200:
|
| 77 |
+
debug_log.append(f"[DEBUG] Response Body: {response.text[:500]}")
|
| 78 |
+
return "\n".join(debug_log) + "\n\n[ERROR] Invalid ticker or no data available for the given date."
|
| 79 |
+
|
| 80 |
+
debug_log.append(f"[DEBUG] Response Content Length: {len(response.text)} bytes")
|
| 81 |
|
| 82 |
data = response.json()
|
| 83 |
+
debug_log.append(f"[DEBUG] Response JSON Keys: {list(data.keys())}")
|
| 84 |
|
| 85 |
# Check if candles data exists
|
| 86 |
if "candles" not in data or not data["candles"]:
|
| 87 |
+
debug_log.append("[DEBUG] No 'candles' key found or candles array is empty")
|
| 88 |
+
debug_log.append(f"[DEBUG] Full Response: {json.dumps(data, indent=2)[:1000]}")
|
| 89 |
+
return "\n".join(debug_log) + "\n\n[ERROR] Invalid ticker or no data available for the given date."
|
| 90 |
+
|
| 91 |
+
debug_log.append(f"[DEBUG] Number of candles received: {len(data['candles'])}")
|
| 92 |
+
debug_log.append(f"[DEBUG] First candle sample: {data['candles'][0]}")
|
| 93 |
+
debug_log.append(f"[DEBUG] Last candle sample: {data['candles'][-1]}")
|
| 94 |
|
| 95 |
# Process candles data
|
| 96 |
simplified_data = []
|
|
|
|
| 105 |
"open": opening_price
|
| 106 |
})
|
| 107 |
|
| 108 |
+
debug_log.append(f"[DEBUG] Successfully processed {len(simplified_data)} candles")
|
| 109 |
+
debug_log.append("\n" + "="*50 + "\n[SUCCESS] DATA OUTPUT:\n" + "="*50 + "\n")
|
| 110 |
+
|
| 111 |
+
# Return debug log + JSON string
|
| 112 |
+
return "\n".join(debug_log) + "\n" + json.dumps(simplified_data, indent=2)
|
| 113 |
|
| 114 |
except requests.exceptions.Timeout:
|
| 115 |
+
debug_log.append("[DEBUG] Request timed out")
|
| 116 |
+
return "\n".join(debug_log) + "\n\n[ERROR] Request timed out. Please try again."
|
| 117 |
except requests.exceptions.RequestException as e:
|
| 118 |
+
debug_log.append(f"[DEBUG] Request Exception: {type(e).__name__} - {str(e)}")
|
| 119 |
+
return "\n".join(debug_log) + "\n\n[ERROR] Invalid ticker or no data available for the given date."
|
| 120 |
except Exception as e:
|
| 121 |
+
debug_log.append(f"[DEBUG] Unexpected Exception: {type(e).__name__} - {str(e)}")
|
| 122 |
+
import traceback
|
| 123 |
+
debug_log.append(f"[DEBUG] Traceback:\n{traceback.format_exc()}")
|
| 124 |
+
return "\n".join(debug_log) + "\n\n[ERROR] An unexpected error occurred"
|
| 125 |
|
| 126 |
# Create Gradio interface
|
| 127 |
with gr.Blocks(title="Groww Stock Data Fetcher") as app:
|