-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsn-mail.py
More file actions
executable file
·209 lines (187 loc) · 6.09 KB
/
sn-mail.py
File metadata and controls
executable file
·209 lines (187 loc) · 6.09 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/python3
import argparse
import logging
import os
import smtplib
import sys
import urllib.request
import xml.etree.ElementTree as ET
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from pathlib import Path
parser = argparse.ArgumentParser(
prog="sn-mail.py",
description="Download and mail the most recent Security Now episode, if not already sent",
usage="%(prog)s [OPTIONS] USERNAME PASSWORD RECIPIENT [RECIPIENT ...]",
)
parser.add_argument("username", metavar="USERNAME", help="sender email username")
parser.add_argument("password", metavar="PASSWORD", help="sender email password")
parser.add_argument(
"recipients", metavar="RECIPIENT", help="recipient email address", nargs="+"
)
parser.add_argument(
"-e",
"--episode",
type=int,
metavar="NUMBER",
default=None,
help="instead of checking, just send the specified episode",
)
parser.add_argument(
"-b", "--body", default="", help="email body (default: empty string)"
)
parser.add_argument(
"-d",
"--dir",
default=None,
help="dir to store lastfile and downloaded content (default: script location)",
)
parser.add_argument(
"-l",
"--lastfile",
metavar="FILE",
default="last.txt",
help="file to store the last-sent episode number (default: %(default)s)",
)
parser.add_argument(
"-s",
"--server",
default="smtp.gmail.com",
help="SMTP server name (default: %(default)s)",
)
parser.add_argument(
"-p",
"--port",
type=int,
default=587,
help="SMTP server port (default %(default)s)",
)
args = parser.parse_args()
# Logging setup
levelName = os.getenv("LOG_LEVEL", "INFO").upper()
level = getattr(logging, levelName, logging.INFO)
logging.basicConfig(level=level, format="%(asctime)s %(levelname)s: %(message)s")
logger = logging.getLogger(__name__)
# Determine dir for lastFile and media
if args.dir:
dir = Path(args.dir)
else:
dir = Path(__file__).resolve().parent
# If a number was specified, use that
if args.episode:
number = args.episode
lastFile = None
# Otherwise, check last sent against newest episode
else:
# Get number of last-sent episode
lastFile = dir / args.lastfile
logger.debug(f"Checking {lastFile} for last sent ep. #...")
try:
with open(lastFile, "r") as file:
old = int(file.read())
except FileNotFoundError:
logger.warning(f"{lastFile} not found. Creating it...")
try:
with open(lastFile, "w") as file:
file.write("\n")
except FileNotFoundError:
logger.error(f"Could not create {lastFile}")
else:
logger.info(f"Successfully created {lastFile}")
finally:
logger.info("Pretending last ep. = 0")
old = 0
except ValueError:
logger.warning("Non-int lastfile value")
logger.info("Pretending last ep. = 0")
old = 0
else:
logger.info(f"Last sent ep. {old}")
# Get most recent ep. #
logger.debug("Retrieving podcast feed...")
feedURL = "https://feeds.twit.tv/podcasts/sn.xml"
with urllib.request.urlopen(feedURL) as u:
XMLFeed = u.read()
latest = int(
ET.fromstring(XMLFeed)
.find("./channel/item")
.find(
"podcast:episode",
namespaces={"podcast": "https://podcastindex.org/namespace/1.0"},
)
.text
)
logger.debug("Retrieved")
logger.info(msg=f"Most recent ep. {latest}")
if old != latest:
number = latest
else:
# Do nothing
number = None
logger.info("No new episode")
if number:
logger.debug(f"Will send episode {number}")
# Hopefully he never changes the filenames,
# but neither of them are reliably in the XML as far as I can tell
# the feed is often updated a few hours before the files become available
# so I try-excepted the requests to catch the 404s
# Download the low-quality mp3;
# the normal mp3 is too large for Gmail
logger.debug("Downloading audio...")
audio = f"sn-{number}-lq.mp3"
audioURL = f"https://media.grc.com/sn/{audio}"
audioFile = str(dir / audio)
try:
urllib.request.urlretrieve(audioURL, audioFile)
except urllib.error.HTTPError as e:
logger.critical(f"Audio download failed: {e.code} {e.reason}")
sys.exit()
else:
logger.debug("Downloaded")
# Download the show notes
logger.debug("Downloading show notes...")
pdf = f"sn-{number}-notes.pdf"
pdfURL = f"https://www.grc.com/sn/{pdf}"
pdfFile = str(dir / pdf)
try:
urllib.request.urlretrieve(pdfURL, pdfFile)
except urllib.error.HTTPError as e:
logger.critical(f"Notes download failed: {e.code} {e.reason}")
sys.exit()
else:
logger.debug("Downloaded")
# Attach both files to an email and send
logger.debug("Sending email...")
msg = MIMEMultipart()
msg["From"] = args.username
msg["To"] = ", ".join(args.recipients)
msg["Date"] = formatdate(localtime=True)
msg["Subject"] = f"Security Now #{number}"
msg.attach(MIMEText(args.body))
for f in [audioFile, pdfFile]:
with open(f, "rb") as fil:
part = MIMEApplication(fil.read(), Name=os.path.basename(f))
part["Content-Disposition"] = 'attachment; filename="%s"' % os.path.basename(f)
msg.attach(part)
session = smtplib.SMTP(args.server, args.port)
session.ehlo()
session.starttls()
session.ehlo()
session.login(args.username, args.password)
session.sendmail(args.username, args.recipients, msg.as_string())
session.quit()
logger.debug("Sent")
if lastFile:
# Update the lastfile unless episode specified
logger.debug(f"Updating {lastFile}")
try:
with open(lastFile, "w") as file:
file.write(str(number) + "\n")
except FileNotFoundError:
logger.error(f"Could not create {lastFile}")
else:
logger.debug("Updated")
finally:
logger.info(f"Sent ep. {number}")