-
Notifications
You must be signed in to change notification settings - Fork 0
/
reminder.py
executable file
·67 lines (58 loc) · 1.93 KB
/
reminder.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
#!/usr/bin/env python3
from datetime import datetime
import smtplib
from email.message import EmailMessage
from pathlib import Path
from configparser import ConfigParser
join_paths = (str(Path(__file__).parent), "/config")
join_paths_list = (str(Path(__file__).parent), "/list.markdown")
config_path = "".join(join_paths)
list_path = "".join(join_paths_list)
config = ConfigParser()
config.read(config_path)
today = datetime.strftime(datetime.today(), "%d.%m")
day = datetime.strftime(datetime.today(), "%d")
weekday = "day " + str(datetime.today().isoweekday())
def get_tasks():
"""Get tasks for today and create a list."""
with open(list_path, "r") as f:
tasks = list()
for row in f:
if row[0:5] == today:
# Append onetime and yearly tasks
tasks.append(row)
if row[0:5] == day + ".XX":
# Append monthly tasks
tasks.append(row)
if row[0:5] == weekday:
# Append weekly tasks
tasks.append(row)
return tasks
get_tasks()
# This is used later for logging
date_now = datetime.now()
if get_tasks():
log_text = "Tasks sent."
host = config.get("smtp", "server")
port = config.get("smtp", "port")
subject = "Your personal reminder for %s" % today
address = config.get("smtp", "to")
sender = config.get("smtp", "from")
text = "".join(get_tasks())
body = "\n".join((
"From: %s" % sender,
"To: %s" % address,
"Subject: %s" % subject,
"",
text
))
server = smtplib.SMTP(host, port)
server.starttls()
server.ehlo()
server.login(config.get("smtp", "login"), config.get("smtp", "password"))
server.sendmail(sender, [address], body.encode("utf-8"))
server.quit()
else:
log_text = "No tasks found."
with open("/var/log/python_reminder/log", "a") as log:
log.write("Reminder executed on %s. %s \n" % (date_now, log_text))