/
infrequent.py
141 lines (114 loc) · 3.64 KB
/
infrequent.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
import pathlib
from dateutil.relativedelta import relativedelta
import datetime
import boto3
from botocore.exceptions import ClientError
import sys
def parse_interval(text):
text = text.lower().strip()
assert len(text.split(" ")) == 2
value, units = text.split(" ")
value = int(value)
if "month" in units:
return relativedelta(months=value)
elif "year" in units:
return relativedelta(years=value)
elif "week" in units:
return relativedelta(weeks=value)
else:
return None
def get_list_of_updates():
people_folder = "people"
today = datetime.datetime.today()
to_contact = list()
for file in pathlib.Path(people_folder).glob("**/*.md"):
lines = file.read_text().splitlines()
name = lines[0].removeprefix("Name:").strip()
relation = lines[1].removeprefix("Relationship:").strip()
contact_frequency = lines[2].removeprefix("Interval(every):").strip().lower()
time_delta = parse_interval(contact_frequency)
interaction_dates_str = [
_.removeprefix("##").strip() for _ in lines if _.startswith("## ")
]
interaction_dates = [
datetime.datetime.strptime(_, "%d-%m-%Y")
for _ in interaction_dates_str
if _
]
interaction_dates.sort()
last_interaction_date = interaction_dates[-1]
next_interaction_date = last_interaction_date + time_delta
delay = today - last_interaction_date
delay_in_days = delay.days
message = f"{name} - {relation} - (Cadency: every {contact_frequency}, {delay_in_days} days passed since last contact)"
if next_interaction_date < today:
to_contact.append(message)
print(last_interaction_date)
print(message)
print("==" * 89)
return to_contact
def send_email_to_myself(subject, body_text, body_html):
CHARSET = "UTF-8"
SENDER = "Duarte O.Carmo <duarteocarmo@gmail.com>"
RECIPIENT = "duarteocarmo@gmail.com"
AWS_REGION = "eu-west-1"
client = boto3.client("ses", region_name=AWS_REGION)
try:
client.send_email(
Destination={
"ToAddresses": [
RECIPIENT,
],
},
Message={
"Body": {
"Html": {
"Charset": CHARSET,
"Data": body_html,
},
"Text": {
"Charset": CHARSET,
"Data": body_text,
},
},
"Subject": {
"Charset": CHARSET,
"Data": subject,
},
},
Source=SENDER,
)
except ClientError as e:
print(e.response["Error"]["Message"])
return False
else:
print("Email was sent :)")
return True
list_of_updates = get_list_of_updates()
if not list_of_updates:
print("No updates missing, exiting")
sys.exit()
subject = f"🎙 You need to reach out to {len(list_of_updates)} people"
body_text = "You need to reach out to\n"
body_html = """<html>
<head></head>
<body>
<p>Hi Duarte, please reach out to:</p>
<ul>
"""
if list_of_updates:
print("Updates to send:")
for update in list_of_updates:
print(update)
body_text += update + "\n"
body_html += f"<li>{update}</li>"
else:
update = "Nobody to update :)"
body_text += update + "\n"
body_html += f"<li>{update}</li>"
body_html += """
</ul>
</body>
</html>
"""
send_email_to_myself(subject=subject, body_html=body_html, body_text=body_text)