Update loading_file.py
Browse files- loading_file.py +55 -40
loading_file.py
CHANGED
@@ -1,45 +1,60 @@
|
|
|
|
1 |
import requests
|
2 |
from bs4 import BeautifulSoup
|
3 |
-
from io import BytesIO
|
4 |
import streamlit as st
|
|
|
5 |
|
6 |
# URL of the website to scrape
|
7 |
-
|
8 |
-
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
}
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
import requests
|
3 |
from bs4 import BeautifulSoup
|
|
|
4 |
import streamlit as st
|
5 |
+
from io import BytesIO
|
6 |
|
7 |
# URL of the website to scrape
|
8 |
+
URL = "https://www.ireland.ie/en/india/newdelhi/services/visas/processing-times-and-decisions/"
|
9 |
+
|
10 |
+
# Cache to improve performance
|
11 |
+
@st.cache_data(ttl=3600)
|
12 |
+
def fetch_and_process_file():
|
13 |
+
"""Fetches the .ods file from the web and processes it into a DataFrame."""
|
14 |
+
headers = {
|
15 |
+
"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"
|
16 |
+
}
|
17 |
+
response = requests.get(URL, headers=headers)
|
18 |
+
if response.status_code != 200:
|
19 |
+
st.error(f"Failed to fetch webpage. Status code: {response.status_code}")
|
20 |
+
return None
|
21 |
+
|
22 |
+
# Extract the file link
|
23 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
24 |
+
file_link = None
|
25 |
+
for link in soup.find_all("a"):
|
26 |
+
if "Visa decisions made from 1 January 2024 to" in link.text:
|
27 |
+
file_link = link["href"]
|
28 |
+
if not file_link.startswith("http"):
|
29 |
+
file_link = requests.compat.urljoin(URL, file_link)
|
30 |
+
break
|
31 |
+
|
32 |
+
if not file_link:
|
33 |
+
st.error("Could not find the .ods file link on the webpage.")
|
34 |
+
return None
|
35 |
+
|
36 |
+
# Fetch the .ods file
|
37 |
+
file_response = requests.get(file_link, headers=headers)
|
38 |
+
if file_response.status_code != 200:
|
39 |
+
st.error(f"Failed to fetch the .ods file. Status code: {file_response.status_code}")
|
40 |
+
return None
|
41 |
+
|
42 |
+
# Process the .ods file
|
43 |
+
df = pd.read_excel(BytesIO(file_response.content), engine="odf")
|
44 |
+
df.drop(columns=["Unnamed: 0", "Unnamed: 1"], inplace=True, errors="ignore")
|
45 |
+
df.dropna(how="all", inplace=True)
|
46 |
+
df.reset_index(drop=True, inplace=True)
|
47 |
+
|
48 |
+
# Rename and restructure columns
|
49 |
+
for idx, row in df.iterrows():
|
50 |
+
if row["Unnamed: 2"] == "Application Number" and row["Unnamed: 3"] == "Decision":
|
51 |
+
df.columns = ["Application Number", "Decision"]
|
52 |
+
df = df.iloc[idx + 1 :]
|
53 |
+
break
|
54 |
+
|
55 |
+
# Preprocess the DataFrame
|
56 |
+
df["Application Number"] = df["Application Number"].astype(str)
|
57 |
+
return df
|
58 |
+
|
59 |
+
# Load the DataFrame
|
60 |
+
df = fetch_and_process_file()
|