]> www.vanbest.org Git - motioneye-debian/commitdiff
improve email picture attachments lookup
authorCalin Crisan <ccrisan@gmail.com>
Sat, 26 Aug 2017 13:22:33 +0000 (16:22 +0300)
committerCalin Crisan <ccrisan@gmail.com>
Sat, 26 Aug 2017 13:22:33 +0000 (16:22 +0300)
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
motioneye/sendmail.py

index 880697ad22b3485be7cdf7ada4681db97e1cb216..29e27a42942ed2c0c11ed080973b1075cac61785 100644 (file)
@@ -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':
index 664e5471cbb23d0a993699a54a858eb808433857..f89c50533a61c9c4d62ccaf55399caa754fc4cd0 100644 (file)
@@ -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')