-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
135 lines (102 loc) · 3.91 KB
/
main.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
import os
import csv
from bs4 import BeautifulSoup
import smtplib
from email import encoders
from email.mime.base import email, MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.common.by import By
load_dotenv()
#setting up a user
class User:
def __init__(self, name, email, field1, field2):
self.name = name
self.email = email
self.field1 = field1
self.field2 = field2
#generate the template of the html file
def create_certaficat(name, field):
#removing backslashes to avoid conflicts
field_ = field.replace("/", "-")
#creating new html file
file_path = f"{name}-{field_}.html"
with open(file_path, "w") as file:
with open("certaficat.html", "r") as template_file:
html_content = template_file.read()
file.write(html_content)
#writing on the certaficat
def write_certaficat(name, field):
field_ = field.replace("/", "-")
file_path = f"{name}-{field_}.html"
with open(file_path, "r") as file:
html_content = file.read()
soup = BeautifulSoup(html_content, "html.parser")
#this is changeable depends on the situtation
h1_name = soup.find("h1", id="name")
h1_name.string = name.replace("-", " ").title()
h1_field = soup.find("h1", id="field")
h1_field.string = field
with open(file_path, "w") as file:
file.write(str(soup))
#converting the generated html to image
def html_to_image(name, field):
options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(options=options)
full_path = os.path.abspath(f"{name}-{field}.html")
#this could be changed as well
driver.get(f"file:///{full_path}")
#the class name can be changed too
elements = driver.find_elements(By.CLASS_NAME, 'container')
for i, element in enumerate(elements):
# Take a screenshot of the element
element_screenshot = element.screenshot_as_png
# Save the screenshot to a file
output_image_path = f"{name}-{field}.png"
with open(output_image_path, "wb") as f:
f.write(element_screenshot)
driver.quit()
#sending emails
def send_emails(name, field, email):
sender_email = os.getenv("SENDER_EMAIL")
sender_password = os.getenv("SENDER_PASSWORD")
field_ = field.replace("/", "-")
html = f"{name}-{field_}.html"
#converting to image
html_to_image(name, field_)
output_image_path = html.replace(".html", ".png")
#sending the image
with open(output_image_path, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {output_image_path}",
)
message = MIMEMultipart()
message['Subject'] = "Summer School Certaficats"
message['From'] = sender_email
message['To'] = email
print(email)
with open("email.html", "r") as email_html:
html_part = MIMEText(email_html.read(), "html")
print(html_part)
message.attach(html_part)
message.attach(part)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
server.login(sender_email, sender_password)
server.sendmail(sender_email, email, message.as_string())
#looping through the students
with open('users_omc.csv', 'r', newline='') as csv_file:
csv_reader = csv.reader(csv_file)
for row in csv_reader:
user = User(row[0].replace(" ", "-"), row[1], row[4].replace(" ", "-"), row[5].replace(" ", "-"))
create_certaficat(user.name, user.field1)
create_certaficat(user.name, user.field2)
write_certaficat(user.name, user.field1)
write_certaficat(user.name, user.field2)
send_emails(user.name, user.field1, user.email)
send_emails(user.name, user.field2, user.email)