-
Notifications
You must be signed in to change notification settings - Fork 0
/
maintemail.py
executable file
·147 lines (118 loc) · 4.29 KB
/
maintemail.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
#!/usr/bin/python
import sys
import getopt
# for markdown processing
from markdown import markdown
from markdown.extensions import Extension
from markdown.treeprocessors import Treeprocessor
# for ics feed
import urllib2
import vobject
# for email
from mailer import Mailer
from mailer import Message
# This is called when markdown() is called on some text
class HeaderFinder(Extension):
def __init__(self):
self.headings = []
Extension.__init__(self)
def extendMarkdown(self, md, md_globals):
ext = MyTreeprocessor(md)
md.treeprocessors.add("toc", ext, "<prettify")
self.headings = ext.getHeadings()
def getHeadings(self):
return self.headings
# This is called from HeaderFinder()
class MyTreeprocessor(Treeprocessor):
# Prepare an array to hold the headings we find
def __init__(self, md):
self.headings = []
Treeprocessor.__init__(self, md)
def run(self, root):
# loop through each top level element
for c in range(len(root) - 1, -1, -1):
# If it's a h1 or h2, add the text to our heading
if root[c].tag == "h1" or root[c].tag == "h2":
self.headings.append(root[c].text)
return root
def getHeadings(self):
return self.headings
def usage(message=""):
# TODO fill this out
print message
print "Usage: icsmailer.py ..."
sys.exit(2)
def main(argv):
verbose = False
ics = ""
smtp = ""
template = ""
try:
opts, args = getopt.getopt(argv, "hi:u:p:s:t:f:v",
["help", "ics=", "username=", "password=", "smtp=", "template=", "from=", "verbose"])
print argv
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
elif opt in ("-i", "--ics"):
ics = arg
elif opt in ("-u", "--username"):
username = arg
elif opt in ("-p", "--password"):
password = arg
elif opt in ("-s", "--smtp"):
smtp = arg
elif opt in ("-t", "--template"):
template = arg
elif opt in ("-f", "--from"):
fromaddress = arg
elif opt in ("-v", "--verbose"):
verbose = True
# Check to make sure we have everything we need
if ics == "":
usage("--ics is required")
if smtp == "":
usage("--smtp is required")
if template == "":
usage("--template is required")
req = urllib2.Request(ics)
response = urllib2.urlopen(req)
icalstream = response.read()
# http://vobject.skyhouseconsulting.com/usage.html
parsedCal = vobject.readOne(icalstream)
description = parsedCal.vevent.description.value
if verbose:
print "Found: " + parsedCal.vevent.summary.value
headerfinder = HeaderFinder()
systemdesc = markdown(description, extensions=[headerfinder])
overview = "On "
overview += parsedCal.vevent.dtstart.value.strftime(
"%A, %B %d %Y, from %l:%M%P")
overview += " to "
overview += parsedCal.vevent.dtend.value.strftime("%l:%M%P")
overview += " will be performing maintenance on the following systems:\n\n"
for h in headerfinder.getHeadings():
overview += "* " + h + "\n"
overview += "\n"
template = open(template, 'r').read()
template = template.replace("%SYSTEMDESC%", systemdesc)
template = template.replace("%SUMMARY%", parsedCal.vevent.summary.value)
template = template.replace(
"%MONTH%", parsedCal.vevent.dtstart.value.strftime("%B"))
template = template.replace(
"%DAY%", parsedCal.vevent.dtstart.value.strftime("%d"))
template = template.replace("%STARTEND%", parsedCal.vevent.dtstart.value.strftime(
"%l:%M%P to ").replace(":00", "") + parsedCal.vevent.dtend.value.strftime("%l:%M%P").replace(":00", ""))
template = template.replace("%OVERVIEW%", markdown(overview))
message = Message(From=fromaddress,
To=parsedCal.vevent.location.value,
Subject=parsedCal.x_wr_calname.value + " - " + parsedCal.vevent.summary.value)
message.Body = overview + description
message.Html = template
sender = Mailer(smtp)
sender.send(message)
if __name__ == "__main__":
main(sys.argv[1:])