From b4e05e03150b9bdae95ede45f6ba11a459507b8e Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Sat, 26 Aug 2017 16:22:33 +0300 Subject: [PATCH] improve email picture attachments lookup Improves the media file search by narrowing down the search path to that corresponding to the day of the event. This is possible only when the still images filename pattern starts with ""%Y-%m-%d/", which is true by default. --- motioneye/mediafiles.py | 3 +++ motioneye/sendmail.py | 33 ++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/motioneye/mediafiles.py b/motioneye/mediafiles.py index 880697a..29e27a4 100644 --- a/motioneye/mediafiles.py +++ b/motioneye/mediafiles.py @@ -109,6 +109,9 @@ def _list_media_files(directory, exts, prefix=None): prefix = '' root = os.path.join(directory, prefix) + if not os.path.exists(root): + return media_files + for name in os.listdir(root): # ignore hidden files/dirs and other unwanted files if name.startswith('.') or name == 'lastsnap.jpg': diff --git a/motioneye/sendmail.py b/motioneye/sendmail.py index 664e547..f89c505 100644 --- a/motioneye/sendmail.py +++ b/motioneye/sendmail.py @@ -64,13 +64,13 @@ def send_mail(server, port, account, password, tls, _from, to, subject, message, email['Date'] = formatdate(localtime=True) email.attach(MIMEText(message)) - for f in reversed(files): + for name in reversed(files): part = MIMEBase('image', 'jpeg') - with open(f, 'rb') as f: + with open(name, 'rb') as f: part.set_payload(f.read()) Encoders.encode_base64(part) - part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f)) + part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(name)) email.attach(part) if files: @@ -94,10 +94,12 @@ def make_message(subject, message, camera_id, moment, timespan, callback): if media_files: logging.debug('got media files') - media_files = [m for m in media_files if abs(m['timestamp'] - timestamp) < timespan] # filter out non-recent media files + + # filter out non-recent media files + media_files = [m for m in media_files if abs(m['timestamp'] - timestamp) < timespan] media_files.sort(key=lambda m: m['timestamp'], reverse=True) media_files = [os.path.join(camera_config['target_dir'], re.sub('^/', '', m['path'])) for m in media_files] - + logging.debug('selected %d pictures' % len(media_files)) format_dict = { @@ -111,6 +113,8 @@ def make_message(subject, message, camera_id, moment, timespan, callback): else: format_dict['timezone'] = 'local time' + + logging.debug('creating email message') m = message % format_dict s = subject % format_dict @@ -118,7 +122,7 @@ def make_message(subject, message, camera_id, moment, timespan, callback): m += '\n\n' m += 'motionEye.' - + callback(s, m, media_files) if not timespan: @@ -126,9 +130,19 @@ def make_message(subject, message, camera_id, moment, timespan, callback): logging.debug('waiting for pictures to be taken') time.sleep(timespan) # give motion some time to create motion pictures - - logging.debug('creating email message') - mediafiles.list_media(camera_config, media_type='picture', callback=on_media_files) + + prefix = None + picture_filename = camera_config.get('picture_filename') + snapshot_filename = camera_config.get('snapshot_filename') + + if ((picture_filename or snapshot_filename) and + not picture_filename or picture_filename.startswith('%Y-%m-%d/') and + not snapshot_filename or snapshot_filename .startswith('%Y-%m-%d/')): + + prefix = moment.strftime('%Y-%m-%d') + logging.debug('narrowing down still images path lookup to %s' % prefix) + + mediafiles.list_media(camera_config, media_type='picture', prefix=prefix, callback=on_media_files) io_loop.start() @@ -206,6 +220,7 @@ def main(parser, args): def on_message(subject, message, files): try: + logging.info('sending email') send_mail(options.server, options.port, options.account, options.password, options.tls, _from, to, subject, message, files or []) logging.info('email sent') -- 2.39.5