sohampawar1030 commited on
Commit
ceabb77
·
verified ·
1 Parent(s): 7c8b162

Update up.py

Browse files
Files changed (1) hide show
  1. up.py +76 -10
up.py CHANGED
@@ -1,32 +1,37 @@
1
  import streamlit as st
2
  import requests
3
  from bs4 import BeautifulSoup
 
 
 
 
 
 
 
 
 
 
4
 
5
  # Function to fetch live recitals from the GDPR website
6
  def fetch_gdpr_recitals():
7
  url = "https://gdpr-info.eu/recitals/"
8
  response = requests.get(url)
9
 
10
- # Check if the request was successful
11
  if response.status_code != 200:
12
  st.error("Failed to fetch data from the GDPR website.")
13
  return {}
14
 
15
  soup = BeautifulSoup(response.content, 'html.parser')
16
-
17
  recitals = {}
18
- # Locate all recital links
19
  articles = soup.find_all('div', class_='artikel')
20
-
21
- # Extract each recital's link and title
22
  for i, article in enumerate(articles):
23
- if i >= 3: # Limit to the first 3 recitals
24
  break
25
  link = article.find('a')['href']
26
  number = article.find('span', class_='nummer').text.strip('()')
27
  title = article.find('span', class_='titel').text.strip()
28
-
29
- # Fetch the content of each recital
30
  rec_response = requests.get(link)
31
  if rec_response.status_code == 200:
32
  rec_soup = BeautifulSoup(rec_response.content, 'html.parser')
@@ -37,10 +42,68 @@ def fetch_gdpr_recitals():
37
 
38
  return recitals
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def display_up_page():
41
  st.title("UP Page - GDPR Recitals")
42
 
43
- # Fetch and display live recitals
44
  if st.button("Fetch Live Recitals"):
45
  with st.spinner("Fetching updates..."):
46
  recitals = fetch_gdpr_recitals()
@@ -48,9 +111,12 @@ def display_up_page():
48
  for number, details in recitals.items():
49
  st.markdown(f"*Recital {number}: {details['title']}*")
50
  st.write(details['content'])
 
 
 
51
  else:
52
  st.write("No recitals found.")
53
 
54
- # Call the function to display the UP page content
55
  if __name__ == "__main__":
56
  display_up_page()
 
1
  import streamlit as st
2
  import requests
3
  from bs4 import BeautifulSoup
4
+ import smtplib
5
+ from email.mime.text import MIMEText
6
+ from email.mime.multipart import MIMEMultipart
7
+ import gspread
8
+ from oauth2client.service_account import ServiceAccountCredentials
9
+ from dotenv import load_dotenv
10
+ import os
11
+
12
+ # Load environment variables from .env file
13
+ load_dotenv()
14
 
15
  # Function to fetch live recitals from the GDPR website
16
  def fetch_gdpr_recitals():
17
  url = "https://gdpr-info.eu/recitals/"
18
  response = requests.get(url)
19
 
 
20
  if response.status_code != 200:
21
  st.error("Failed to fetch data from the GDPR website.")
22
  return {}
23
 
24
  soup = BeautifulSoup(response.content, 'html.parser')
 
25
  recitals = {}
 
26
  articles = soup.find_all('div', class_='artikel')
27
+
 
28
  for i, article in enumerate(articles):
29
+ if i >= 3: # Limit to 3 recitals for the demo
30
  break
31
  link = article.find('a')['href']
32
  number = article.find('span', class_='nummer').text.strip('()')
33
  title = article.find('span', class_='titel').text.strip()
34
+
 
35
  rec_response = requests.get(link)
36
  if rec_response.status_code == 200:
37
  rec_soup = BeautifulSoup(rec_response.content, 'html.parser')
 
42
 
43
  return recitals
44
 
45
+ # Function to send email notifications
46
+ def send_email(recitals):
47
+ sender_email = os.getenv("EMAIL_ADDRESS")
48
+ receiver_email = os.getenv("RECEIVER_EMAIL")
49
+ password = os.getenv("EMAIL_PASSWORD")
50
+
51
+ subject = "GDPR Recitals Update"
52
+ body = "New GDPR recitals have been fetched:\n\n"
53
+
54
+ for number, details in recitals.items():
55
+ body += f"Recital {number}: {details['title']}\n{details['content']}\n\n"
56
+
57
+ msg = MIMEMultipart()
58
+ msg['From'] = sender_email
59
+ msg['To'] = receiver_email
60
+ msg['Subject'] = subject
61
+ msg.attach(MIMEText(body, 'plain'))
62
+
63
+ try:
64
+ with smtplib.SMTP('smtp.gmail.com', 587) as server:
65
+ server.starttls() # Secure the connection
66
+ server.login(sender_email, password) # Log in
67
+ server.send_message(msg) # Send the email
68
+ st.success("Email notification sent!")
69
+ except smtplib.SMTPAuthenticationError:
70
+ st.error("Failed to login: Check your email and password.")
71
+ except smtplib.SMTPConnectError:
72
+ st.error("Failed to connect to the SMTP server. Check your network connection.")
73
+ except smtplib.SMTPException as e:
74
+ st.error(f"SMTP error occurred: {str(e)}") # Improved error message
75
+ except Exception as e:
76
+ st.error(f"Failed to send email: {str(e)}") # General error message
77
+
78
+ # Function to store data in Google Sheets
79
+ def store_in_google_sheets(recitals):
80
+ scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
81
+ creds_path = os.getenv("GOOGLE_CREDENTIALS_PATH")
82
+
83
+ st.write(f"Google Credentials Path: {creds_path}") # Debug statement
84
+
85
+ if not creds_path or not os.path.exists(creds_path):
86
+ st.error("Google credentials path is invalid or not set.")
87
+ return
88
+
89
+ creds = ServiceAccountCredentials.from_json_keyfile_name(creds_path, scope)
90
+ client = gspread.authorize(creds)
91
+
92
+ sheet_id = os.getenv("GOOGLE_SHEET_ID")
93
+ if not sheet_id:
94
+ st.error("Google Sheet ID is not set.")
95
+ return
96
+
97
+ sheet = client.open_by_key(sheet_id).sheet1
98
+
99
+ for number, details in recitals.items():
100
+ sheet.append_row([number, details['title'], details['content']])
101
+ st.success("Data stored in Google Sheets!")
102
+
103
+ # Function to display the UP page
104
  def display_up_page():
105
  st.title("UP Page - GDPR Recitals")
106
 
 
107
  if st.button("Fetch Live Recitals"):
108
  with st.spinner("Fetching updates..."):
109
  recitals = fetch_gdpr_recitals()
 
111
  for number, details in recitals.items():
112
  st.markdown(f"*Recital {number}: {details['title']}*")
113
  st.write(details['content'])
114
+
115
+ send_email(recitals)
116
+ store_in_google_sheets(recitals)
117
  else:
118
  st.write("No recitals found.")
119
 
120
+ # Run the display function
121
  if __name__ == "__main__":
122
  display_up_page()