-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.py
181 lines (141 loc) · 6.34 KB
/
script.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os
import yagmail
import dns.resolver
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.chrome.options import Options
# Load environment variables
load_dotenv()
EMAIL = os.getenv('EMAIL') # Email username
EMAIL_PASSWORD = os.getenv('EMAIL_PASSWORD') # Email password
LOGIN_USERNAME = os.getenv('LOGIN_USERNAME')
LOGIN_PASSWORD = os.getenv('LOGIN_PASSWORD')
# Configuration
WEBSITE_NAME = 'HSBI' # for user email, has no semantic value
LOGIN_URL = 'https://www.hsbi.de/qisserver/rds?state=wlogin&login=in&breadCrumbSource=portal'
XPATH_LOGIN_USERNAME = '//*[@id="asdf"]'
XPATH_LOGIN_PASSWORD = '//*[@id="fdsa"]'
XPATH_LOGIN_BUTTON = '//*[@id="loginForm:login"]'
XPATH_BUTTON_STEP_2 = '//*[@id="wrapper"]/div[5]/div/ul/li[1]/a'
XPATH_BUTTON_STEP_3 = '//*[@id="makronavigation"]/ul/li[2]/a'
XPATH_BUTTON_STEP_4 = '//*[@id="wrapper"]/div[5]/table/tbody/tr/td/div/div[2]/div/form/div/ul/li[3]/a'
XPATH_BUTTON_STEP_5 = '//*[@id="wrapper"]/div[5]/table/tbody/tr/td/div/div[2]/form/ul/li/a[2]'
XPATH_TRACK_AREA = '//*[@id="wrapper"]/div[5]/table/tbody/tr/td/div/div[2]/form/table[2]/tbody'
CONTENT_FILE = 'content_file.txt'
TO_EMAIL = EMAIL
SMTP_PROVIDERS = {
"gmail.com": {"host": "smtp.gmail.com", "port": 587, "use_ssl": False, "use_tls": True},
"outlook.com": {"host": "smtp.office365.com", "port": 587, "use_ssl": False, "use_tls": True},
"hotmail.com": {"host": "smtp.office365.com", "port": 587, "use_ssl": False, "use_tls": True},
"icloud.com": {"host": "smtp.mail.me.com", "port": 587, "use_ssl": False, "use_tls": True},
"yahoo.com": {"host": "smtp.mail.yahoo.com", "port": 465, "use_ssl": True, "use_tls": False},
"aol.com": {"host": "smtp.aol.com", "port": 465, "use_ssl": True, "use_tls": False},
"zoho.com": {"host": "smtp.zoho.com", "port": 587, "use_ssl": False, "use_tls": True},
"protonmail.com": {"host": "smtp.protonmail.com", "port": 465, "use_ssl": True, "use_tls": False},
}
def get_website_content():
options = Options()
options.add_argument("--headless") # Enable headless mode
options.add_argument("--disable-gpu") # Disable GPU hardware acceleration (not strictly necessary but can improve performance)
options.add_argument("--no-sandbox") # Avoid issues in certain environments (e.g., Docker)
# Initialize Selenium WebDriver with the specified options
driver = webdriver.Chrome(options=options)
try:
# Step 1: Open the login page
driver.get(LOGIN_URL)
time.sleep(1)
# Step 2: Enter username
username_field = driver.find_element(By.XPATH, XPATH_LOGIN_USERNAME)
username_field.send_keys(LOGIN_USERNAME)
# Step 3: Enter password
password_field = driver.find_element(By.XPATH, XPATH_LOGIN_PASSWORD)
password_field.send_keys(LOGIN_PASSWORD)
# Step 4: Click the login button
login_button = driver.find_element(By.XPATH, XPATH_LOGIN_BUTTON)
login_button.click()
button_step_2 = driver.find_element(By.XPATH, XPATH_BUTTON_STEP_2)
button_step_2.click()
button_step_3 = driver.find_element(By.XPATH, XPATH_BUTTON_STEP_3)
button_step_3.click()
button_step_4 = driver.find_element(By.XPATH, XPATH_BUTTON_STEP_4)
button_step_4.click()
button_step_5 = driver.find_element(By.XPATH, XPATH_BUTTON_STEP_5)
button_step_5.click()
# Step 5: Wait for the login process to complete
time.sleep(1) # Adjust based on website behavior
common_parent_element = driver.find_element(By.XPATH, XPATH_TRACK_AREA)
page_content = common_parent_element.get_attribute('outerHTML') # Gets the HTML of the parent element and its content
return page_content
finally:
# Ensure the driver is closed
driver.quit()
def load_previous_content():
"""Load the previously stored content from file."""
if os.path.exists(CONTENT_FILE):
with open(CONTENT_FILE, 'r') as f:
return f.read().strip()
return None
def save_current_content(content):
"""Save the current content to a file."""
with open(CONTENT_FILE, 'w') as f:
f.write(content)
def get_smtp_settings(email):
"""Determines the SMTP settings based on email domain."""
domain = email.split('@')[-1]
if domain in SMTP_PROVIDERS:
return SMTP_PROVIDERS[domain]
# If the domain is unknown, perform an MX record lookup
try:
mx_records = dns.resolver.resolve(domain, 'MX')
mx_host = str(mx_records[0].exchange).rstrip('.')
# Basic provider detection using MX host
if "google" in mx_host:
return SMTP_PROVIDERS["gmail.com"]
elif "outlook" in mx_host or "office365" in mx_host:
return SMTP_PROVIDERS["outlook.com"]
elif "yahoo" in mx_host:
return SMTP_PROVIDERS["yahoo.com"]
elif "icloud" in mx_host:
return SMTP_PROVIDERS["icloud.com"]
except Exception as e:
print(f"MX Lookup Failed: {e}")
# Default to Gmail if nothing is found
print("Unknown email provider. Using default Gmail settings.")
return SMTP_PROVIDERS["gmail.com"]
def send_email(subject, body):
"""Send an email notification."""
smtp_settings = get_smtp_settings(EMAIL)
yag = yagmail.SMTP(
user=EMAIL,
password=EMAIL_PASSWORD,
host=smtp_settings["host"],
port=smtp_settings["port"],
smtp_ssl=smtp_settings["use_ssl"],
smtp_starttls=smtp_settings["use_tls"]
)
yag.send(to=TO_EMAIL, subject=subject, contents=body)
def main():
try:
# Fetch the website content
content = get_website_content()
# Compare with the previous content
previous_content = load_previous_content()
if previous_content != content:
# Content has changed
send_email("Website Updated", f"The website {WEBSITE_NAME} has been updated.")
print("Website updated. Email sent.")
else:
print("No changes detected.")
save_current_content(content)
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()
# Use this as ENCRYPTION_KEY
# from cryptography.fernet import Fernet
#
# key = Fernet.generate_key()
# print(key.decode())