File size: 4,415 Bytes
ac07e52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import streamlit as st
import requests
from bs4 import BeautifulSoup
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()

# Function to fetch live recitals from the GDPR website
def fetch_gdpr_recitals():
    url = "https://gdpr-info.eu/recitals/"
    response = requests.get(url)

    if response.status_code != 200:
        st.error("Failed to fetch data from the GDPR website.")
        return {}

    soup = BeautifulSoup(response.content, 'html.parser')
    recitals = {}
    articles = soup.find_all('div', class_='artikel')

    for i, article in enumerate(articles):
        if i >= 3:  # Limit to 3 recitals for the demo
            break
        link = article.find('a')['href']
        number = article.find('span', class_='nummer').text.strip('()')
        title = article.find('span', class_='titel').text.strip()

        rec_response = requests.get(link)
        if rec_response.status_code == 200:
            rec_soup = BeautifulSoup(rec_response.content, 'html.parser')
            content = rec_soup.find('div', class_='entry-content').get_text(strip=True)
            recitals[number] = {'title': title, 'content': content}
        else:
            st.error(f"Failed to fetch recital {number} from {link}")

    return recitals

# Function to send email notifications
def send_email(recitals):
    sender_email = os.getenv("EMAIL_ADDRESS")
    receiver_email = os.getenv("RECEIVER_EMAIL")
    password = os.getenv("EMAIL_PASSWORD")

    subject = "GDPR Recitals Update"
    body = "New GDPR recitals have been fetched:\n\n"

    for number, details in recitals.items():
        body += f"Recital {number}: {details['title']}\n{details['content']}\n\n"

    msg = MIMEMultipart()
    msg['From'] = sender_email
    msg['To'] = receiver_email
    msg['Subject'] = subject
    msg.attach(MIMEText(body, 'plain'))

    try:
        with smtplib.SMTP('smtp.gmail.com', 587) as server:
            server.starttls()  # Secure the connection
            server.login(sender_email, password)  # Log in
            server.send_message(msg)  # Send the email
            st.success("Email notification sent!")
    except smtplib.SMTPAuthenticationError:
        st.error("Failed to login: Check your email and password.")
    except smtplib.SMTPConnectError:
        st.error("Failed to connect to the SMTP server. Check your network connection.")
    except smtplib.SMTPException as e:
        st.error(f"SMTP error occurred: {str(e)}")  # Improved error message
    except Exception as e:
        st.error(f"Failed to send email: {str(e)}")  # General error message

# Function to store data in Google Sheets
def store_in_google_sheets(recitals):
    scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
    creds_path = os.getenv("GOOGLE_CREDENTIALS_PATH")

    st.write(f"Google Credentials Path: {creds_path}")  # Debug statement

    if not creds_path or not os.path.exists(creds_path):
        st.error("Google credentials path is invalid or not set.")
        return

    creds = ServiceAccountCredentials.from_json_keyfile_name(creds_path, scope)
    client = gspread.authorize(creds)

    sheet_id = os.getenv("GOOGLE_SHEET_ID")
    if not sheet_id:
        st.error("Google Sheet ID is not set.")
        return

    sheet = client.open_by_key(sheet_id).sheet1

    for number, details in recitals.items():
        sheet.append_row([number, details['title'], details['content']])
    st.success("Data stored in Google Sheets!")

# Function to display the UP page
def display_Update_tracker_page():
    st.title("Update Tracker - GDPR Recitals")

    if st.button("Fetch Live Recitals"):
        with st.spinner("Fetching updates..."):
            recitals = fetch_gdpr_recitals()
            if recitals:
                for number, details in recitals.items():
                    st.markdown(f"*Recital {number}: {details['title']}*")
                    st.write(details['content'])

                send_email(recitals)
                store_in_google_sheets(recitals)
            else:
                st.write("No recitals found.")

# Run the display function
if __name__ == "__main__":
    display_Update_tracker_page()