SR05 commited on
Commit
14adba0
Β·
verified Β·
1 Parent(s): 2360f0d

Create fetch_data()

Browse files
Files changed (1) hide show
  1. fetch_data() +80 -0
fetch_data() ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import pandas as pd
3
+ import streamlit as st
4
+ from io import BytesIO
5
+ from bs4 import BeautifulSoup
6
+
7
+ @st.cache_data(ttl=3600)
8
+ def fetch_data():
9
+ url = "https://www.ireland.ie/en/india/newdelhi/services/visas/processing-times-and-decisions/"
10
+ headers = {
11
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
12
+ }
13
+
14
+ st.write("πŸ”„ Fetching webpage...")
15
+ try:
16
+ response = requests.get(url, headers=headers, timeout=10) # Timeout added
17
+ response.raise_for_status() # Raise an error if request fails
18
+ except requests.exceptions.RequestException as e:
19
+ st.error(f"❌ Failed to fetch webpage: {e}")
20
+ return None, None
21
+
22
+ # Parse the HTML
23
+ st.write("πŸ”„ Parsing webpage...")
24
+ soup = BeautifulSoup(response.content, "html.parser")
25
+
26
+ file_url = None
27
+ for link in soup.find_all("a"):
28
+ if "Visa decisions made from 1 January 2025" in link.get_text():
29
+ file_url = link.get("href")
30
+ if not file_url.startswith("http"):
31
+ file_url = requests.compat.urljoin(url, file_url)
32
+ break
33
+
34
+ if not file_url:
35
+ st.error("❌ Could not find the visa decisions file link.")
36
+ return None, None
37
+
38
+ st.write(f"πŸ“₯ Found file link: {file_url}")
39
+
40
+ # Download the .ods file
41
+ try:
42
+ st.write("πŸ”„ Downloading file...")
43
+ ods_response = requests.get(file_url, headers=headers, timeout=15) # Timeout added
44
+ ods_response.raise_for_status()
45
+ except requests.exceptions.RequestException as e:
46
+ st.error(f"❌ Failed to download the file: {e}")
47
+ return None, None
48
+
49
+ st.write("πŸ“‚ Processing file...")
50
+ ods_file = BytesIO(ods_response.content)
51
+
52
+ # Read Excel file
53
+ try:
54
+ df = pd.read_excel(ods_file, engine="odf")
55
+ except Exception as e:
56
+ st.error(f"❌ Error reading ODS file: {e}")
57
+ return None, None
58
+
59
+ # Drop empty rows
60
+ df.dropna(how="all", inplace=True)
61
+ df.reset_index(drop=True, inplace=True)
62
+
63
+ # Find header row
64
+ header_rows = df[df.iloc[:, 0].astype(str).str.contains("Application Number", na=False)].index
65
+ if len(header_rows) == 0:
66
+ st.error("❌ Could not find the header row. Check the file format.")
67
+ return None, None
68
+
69
+ header_index = header_rows[0]
70
+ df = df.iloc[header_index + 1:].reset_index(drop=True)
71
+
72
+ # Rename columns
73
+ df.columns = ["Application Number", "Decision"]
74
+ df.dropna(inplace=True)
75
+ df["Application Number"] = df["Application Number"].astype(str).str.strip()
76
+
77
+ st.write("βœ… Data loaded successfully!")
78
+ return df, "Visa Decisions Report"
79
+
80
+ precomputed_df, file_name = fetch_data()