From 4834f8fa6e5d50f210c2dd0a229175316e12ddb7 Mon Sep 17 00:00:00 2001
From: Antoine VAN ELSTRAETE <antoine@van-elstraete.net>
Date: Fri, 17 May 2019 18:32:58 +0200
Subject: [PATCH] Recursive if directory

---
 exv.py | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/exv.py b/exv.py
index eeb7687..9499933 100755
--- a/exv.py
+++ b/exv.py
@@ -4,13 +4,15 @@
 from subprocess import Popen, PIPE
 from datetime import datetime
 import json
-from os.path import isfile
+from os.path import isfile, isdir
+from os import walk
 
 
-def extractor(input_files, start, end):
+def extractor(input_files, start, end, recursive):
     if not isinstance(input_files, type(list())):
         raise ValueError("Input files must be a list.")
     exif_dict_list = []
+    files = []
     usefull_exif = [
         "Image.Model",
         "Photo.LensModel",
@@ -24,6 +26,16 @@ def extractor(input_files, start, end):
         "Image.ImageLength",
         "Photo.DateTimeOriginal"
     ]
+    for item in input_files:
+        if isdir(item):
+            for (dirpath, dirnames, filenames) in walk(item):
+                for filename in filenames:
+                    files.append("{}/{}".format(dirpath, filename))
+                if not recursive:
+                    break
+        else:
+            files.append(item)
+    input_files = files
     for input_file in input_files:
         exif_dict = {}
         if not isfile(input_file):
@@ -134,11 +146,13 @@ def extractor(input_files, start, end):
 
 if __name__ == '__main__':
     import argparse
+    recursive = False
     parser = argparse.ArgumentParser()
     parser.add_argument(
         "infile", help="input file (EXV, JPG, CR2, ...). Can be more than one.", nargs='+')
     parser.add_argument("-s", "--start-date", help="start date as YYYYMMDD. If omitted, begin of the collection.", const=None)
     parser.add_argument("-e", "--end-date", help="end date as YYYYMMDD. If omitted, today", const=None)
+    parser.add_argument("-R", "--recursive", help="Walk throught directory to find all files", action='store_true')
     args = parser.parse_args()
     if not args.start_date:
         args.start_date = "18250101" # Ok, that's the year of the 1st photography by Nicéphore Niépce. Shoul'd be enough for a start date.
@@ -150,4 +164,5 @@ if __name__ == '__main__':
     except ValueError:
         print("Date must be YYYYMMDD.")
         exit()
-    print(json.dumps(extractor(args.infile, datetime.strptime(args.start_date, "%Y%m%d"), datetime.strptime(args.end_date, "%Y%m%d")), indent=4, sort_keys=True))
+    recursive = args.recursive
+    print(json.dumps(extractor(args.infile, datetime.strptime(args.start_date, "%Y%m%d"), datetime.strptime(args.end_date, "%Y%m%d"), recursive), indent=4, sort_keys=True))