-
Notifications
You must be signed in to change notification settings - Fork 0
/
newsletter.py
130 lines (100 loc) · 3.79 KB
/
newsletter.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
from trello import TrelloClient
from datetime import datetime
from sys import exit, argv
from mailchimp_marketing import Client
from mailchimp_marketing.api_client import ApiClientError
from bs4 import BeautifulSoup
import markdown
from mdx_gfm import GithubFlavoredMarkdownExtension
import config
def debug_out(str):
if config.debug:
print("-- %s" % str)
if len(argv) != 2 or argv[1] not in ["preview", "final"]:
print("Usage: newsletter.py [preview|final]")
exit(1)
newsletter_type = argv[1]
def getTrelloCards():
client = TrelloClient(
api_key=config.api_key,
api_secret=config.api_secret,
token=config.token,
token_secret=config.token_secret
)
org_name = config.org_name
brd_name = config.brd_name
list_name = datetime.now().strftime("%Y-%V")
orgs = list(filter(lambda x: x.name == org_name,
client.list_organizations()))
if len(orgs) != 1:
print("Error while filtering organzation")
exit(1)
debug_out("Organization found")
brds = list(filter(lambda x: x.name == brd_name,
orgs[0].get_boards("open")))
if len(brds) != 1:
print("Error while filtering boards")
exit(1)
debug_out("Board found")
lists = list(filter(lambda x: x.name == list_name,
brds[0].get_lists("open")))
if len(lists) != 1:
print("Error while filtering lists")
exit(1)
cards = lists[0].list_cards()
debug_out("List found, with %s cards" % len(cards))
if len(cards) == 0:
print("Not sending empty newsletter")
exit(1)
return cards
def sendNewsletter(items, newsletter_type):
subject = datetime.now().strftime("MuMaNews - CW %V")
title = "%s %s" % (datetime.now().strftime("%Y-%V"), newsletter_type)
try:
client = Client()
client.set_config({
"api_key": config.mailchimp_api_key,
"server": config.mailchimp_server
})
response = client.campaigns.create({
"type": "regular",
"recipients": {
"list_id": config.mailchimp_list_id
},
"settings": {
"template_id": config.mailchimp_template_id,
"subject_line": subject,
"title": title,
"from_name": config.MAIL_FROM_NAME,
"reply_to": config.MAIL_FROM
}
})
# print(response)
campaign_id = response["id"]
debug_out("Mailchimp Campaign: %s / %s" % (campaign_id, response["web_id"]))
response = client.campaigns.get_content(campaign_id)
soup = BeautifulSoup(response["html"], "html.parser")
template_elem_src = soup.find(
string="%TITLE%").find_parent(class_="mcnTextBlock")
template_txt = str(template_elem_src)
output = []
for item in items:
txt = template_txt.replace("%TITLE%", item.name)
txt = txt.replace("%CONTENT%", markdown.markdown(item.description,extensions=[GithubFlavoredMarkdownExtension()]))
output.append(txt)
new_elem = BeautifulSoup("".join(output), "html.parser")
template_elem_src.replace_with(new_elem)
response = client.campaigns.set_content(campaign_id, {
"html": str(soup)
})
if newsletter_type == "preview":
response = client.campaigns.send_test_email(
campaign_id, {"test_emails": [ config.MAIL_TO_PREVIEW ], "send_type": "html"})
debug_out(str(response))
else:
response = client.campaigns.send(campaign_id)
debug_out(str(response))
except ApiClientError as error:
print("Error: {}".format(error.text))
items = getTrelloCards()
sendNewsletter(items, newsletter_type)