Subham9126 commited on
Commit
52aa39d
·
verified ·
1 Parent(s): b63e95d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -33
app.py CHANGED
@@ -1,49 +1,43 @@
1
  import gradio as gr
2
- import requests
3
-
4
- def fetch_train_html(train_no, date):
5
- url = "https://enquiry.indianrail.gov.in/mntes/tr?opt=TrainRunning&subOpt=FindRunningInstance"
6
-
7
- headers = {
8
- "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:142.0) Gecko/20100101 Firefox/142.0",
9
- "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
10
- "Accept-Language": "en-US,en;q=0.5",
11
- "Content-Type": "application/x-www-form-urlencoded",
12
- "Upgrade-Insecure-Requests": "1",
13
- "Sec-Fetch-Dest": "document",
14
- "Sec-Fetch-Mode": "navigate",
15
- "Sec-Fetch-Site": "same-origin",
16
- "Sec-Fetch-User": "?1",
17
- "Priority": "u=0, i",
18
- "Referer": "https://enquiry.indianrail.gov.in/mntes/"
19
- }
20
-
21
- data = {
22
- "lan": "en",
23
- "jDate": date,
24
- "trainNo": train_no
25
- }
26
 
 
27
  try:
28
- resp = requests.post(url, headers=headers, data=data, timeout=20)
29
- if resp.status_code == 200:
30
- # Return first 1000 chars to avoid huge output
31
- return resp.text[:1000]
32
- else:
33
- return f"Failed to fetch. Status code: {resp.status_code}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  except Exception as e:
35
  return f"Error: {e}"
36
 
37
  # Gradio interface
38
  iface = gr.Interface(
39
- fn=fetch_train_html,
40
  inputs=[
41
  gr.Textbox(label="Train Number", value="13019"),
42
  gr.Textbox(label="Date (DD-MMM-YYYY)", value="16-Sep-2025")
43
  ],
44
  outputs="textbox",
45
- title="NTES Train Running Test",
46
- description="Test if NTES train-running HTML can be fetched via requests"
47
  )
48
 
49
  iface.launch()
 
1
  import gradio as gr
2
+ from playwright.sync_api import sync_playwright
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ def fetch_train_running(train_no, date):
5
  try:
6
+ with sync_playwright() as p:
7
+ browser = p.chromium.launch(headless=True)
8
+ page = browser.new_page()
9
+
10
+ # Go to the NTES page
11
+ page.goto("https://enquiry.indianrail.gov.in/mntes/")
12
+
13
+ # Fill the form
14
+ page.fill("input[name='trainNo']", train_no)
15
+ page.fill("input[name='jDate']", date)
16
+
17
+ # Submit the form
18
+ page.click("button[type='submit']")
19
+
20
+ # Wait for network idle (page fully loaded)
21
+ page.wait_for_load_state("networkidle")
22
+
23
+ html = page.content()
24
+ browser.close()
25
+
26
+ # Return first 2000 chars for inspection
27
+ return html[:2000]
28
  except Exception as e:
29
  return f"Error: {e}"
30
 
31
  # Gradio interface
32
  iface = gr.Interface(
33
+ fn=fetch_train_running,
34
  inputs=[
35
  gr.Textbox(label="Train Number", value="13019"),
36
  gr.Textbox(label="Date (DD-MMM-YYYY)", value="16-Sep-2025")
37
  ],
38
  outputs="textbox",
39
+ title="NTES Train Running Fetcher",
40
+ description="Fetch NTES train running info using a headless browser."
41
  )
42
 
43
  iface.launch()