-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.py
189 lines (160 loc) · 5.55 KB
/
engine.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
182
183
184
185
186
187
188
import logging
import random
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from google.oauth2 import service_account
from googleapiclient.discovery import build
EMAIL_ADDRESS = ''
EMAIL_PASSWORD = ''
SERVICE_ACCOUNT_FILE = ''
logging.basicConfig(filename='error_log.log', level=logging.ERROR)
def start_vocab_app():
SHEET_ID = ""
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('sheets', 'v4', credentials=credentials)
SHEET_RANGE = ""
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SHEET_ID, range=SHEET_RANGE).execute()
values = result.get('values', [])
recipients = dict()
for item in values[1:]:
recipients[item[0]] = [thing for thing in item [1:] ]
for email , features in recipients.items():
try:
if (features[-1]).lower() == "no" :
words = read_google_doc(features[0])
email_body = create_email_body(words)
send_email(email, email_body)
if features[1] != '.':
counting_words(words, features[1])
else:
continue
except Exception as e:
logging.error(f"Errore per il destinatario {email}: {e}")
print(f"Errore con {email}. Passo al destinatario successivo.")
continue
def counting_words(words, SHEET_ID):
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('sheets', 'v4', credentials=credentials)
SHEET_RANGE = ""
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SHEET_ID, range=SHEET_RANGE).execute()
values = result.get('values', [])
my_dict = {row[0]: row for row in values[1:]}
modified_values = [values[0]]
english_terms = list()
for terms in words:
english_terms.append(terms[0])
for row in values[1:]:
if row[0] in english_terms:
row[1] = str(int(row[1]) + 1)
modified_values.append(row)
for word in english_terms:
if word not in my_dict:
modified_values.append([word, '1'])
body = {'values': modified_values}
result = service.spreadsheets().values().update(
spreadsheetId=SHEET_ID,
range=SHEET_RANGE,
valueInputOption="RAW",
body=body
).execute()
def read_google_doc(DOCUMENT_ID):
SCOPES = ['https://www.googleapis.com/auth/documents.readonly']
wordstosend =
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('docs', 'v1', credentials=credentials)
doc = service.documents().get(documentId=DOCUMENT_ID).execute()
content = doc.get('body').get('content')
text = ""
for element in content:
if 'paragraph' in element:
elements = element.get('paragraph').get('elements')
for elem in elements:
text_run = elem.get('textRun')
if text_run:
text += text_run.get('content')
lines = text.strip().split('\n')
term_list = []
for line in lines:
if '|||' in line:
term, meaning = line.split('|||')
term_list.append([term.strip(), meaning.strip()])
number = min(len(term_list), wordstosend)
random.shuffle(term_list)
return random.sample(term_list, number)
def create_email_body(words):
word_html = "".join(
f"<div style='text-align: center; margin: 10px 0; font-size: 24px;'><strong>{word_pair[0]} : {word_pair[1]}</strong></div>"
for word_pair in words
)
return f"""
<html>
<head>
<style>
body {{
font-family: Arial, sans-serif;
background-color: #f9f9f9;
padding: 20px;
}}
h1 {{
color: #4A90E2;
text-align: center;
margin-bottom: 10px;
}}
h2 {{
color: #333;
text-align: center;
margin-bottom: 20px;
}}
p {{
font-size: 16px;
line-height: 1.5;
}}
.container {{
max-width: 600px;
margin: 0 auto;
background-color: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}}
.footer {{
text-align: center;
margin-top: 20px;
font-size: 14px;
color: #888;
}}
</style>
</head>
<body>
<div class="container">
<h1>Daily Vocabulary</h1>
<h2>Here are the words for today!</h2>
{word_html}
<div class="footer">
<p>Keep Learning!</p>
<p style='font-style: italic;'>Mail sent automatically. Please do not respond.</p>
</div>
</div>
</body>
</html>
"""
def send_email(EMAIL_DESTINATION, email_body):
msg = MIMEMultipart()
msg['Subject'] = 'Daily Vocabulary'
msg['From'] = EMAIL_ADDRESS
msg['To'] = EMAIL_DESTINATION
msg.attach(MIMEText(email_body, 'html'))
try:
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
print(f'Email inviata a {EMAIL_DESTINATION}')
except Exception as e:
logging.error(f"Errore nell'invio dell'email a {EMAIL_DESTINATION}: {e}")
print(f"Errore nell'invio dell'email a {EMAIL_DESTINATION}: {e}")