Skip to content

Commit

Permalink
Add argument "--file-type" to be able to choose between image or video (
Browse files Browse the repository at this point in the history
#114)

* Introduce file_type to enable separation of image and video

* Add test for get_file_type

* Fix linting errors

* Add .vscode to .gitignore

* Remove -x arg from file-type command

* Add choices to file-type argument

* Make file-type arg more solid

* Update readme to reflect correct argument

* Fix typo in log output

* Minor log output change to be compliant with other log outputs
  • Loading branch information
pabera authored Aug 22, 2021
1 parent 67b97b8 commit b047003
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea
/.vscode
/prime
/stage
/snap
Expand Down
13 changes: 13 additions & 0 deletions phockup.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ def parse_args(args=sys.argv[1:]):
""",
)

parser.add_argument(
'--file-type',
type=str,
choices=['image', 'video'],
metavar='image|video',
help="""\
By default, Phockup addresses both image and video files.
If you want to restrict your command to either images or
videos only, use `--file-type=[image|video]`.
""",
)

return parser.parse_args(args)


Expand Down Expand Up @@ -255,6 +267,7 @@ def main(options):
dry_run=options.dry_run,
quiet=options.quiet,
max_depth=options.maxdepth,
file_type=options.file_type,
)


Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ Instead of copying the process will create hard link all files from the INPUTDIR
### Original filenames
Organize the files in selected format or using the default year/month/day format but keep original filenames by using the flag `-o | --original-names`.

### File Type
By default, Phockup addresses both image and video files. If you want to restrict your command to either images or videos only, use `--file-type=[image|video]`.

### Fix incorrect dates
If date extracted from photos is incorrect, you can use the `-f | --date-field` option to set the correct exif field to get date information from. Use this command to list which fields are available for a file:
```
Expand Down
36 changes: 27 additions & 9 deletions src/phockup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self, input_dir, output_dir, **args):
self.max_depth = args.get('max_depth', -1)
self.stop_depth = self.input_dir.count(os.sep) + self.max_depth \
if self.max_depth > -1 else sys.maxsize
self.file_type = args.get('file_type', None)

if self.dry_run:
logger.warning("Dry-run phockup (does a trial run with no permanent changes)...")
Expand Down Expand Up @@ -92,14 +93,20 @@ def checksum(self, filename):
sha256.update(block)
return sha256.hexdigest()

def is_image_or_video(self, mimetype):
def get_file_type(self, mimetype):
"""
Check if given file_type is image or video
Return None if other
Use mimetype to determine if the file is an image or video.
"""
pattern = re.compile('^(image/.+|video/.+|application/vnd.adobe.photoshop)$')
if pattern.match(mimetype):
return True
return False
patternImage = re.compile('^(image/.+|application/vnd.adobe.photoshop)$')
if patternImage.match(mimetype):
return 'image'

patternVideo = re.compile('^(video/.*)$')
if patternVideo.match(mimetype):
return 'video'
return None

def get_output_dir(self, date):
"""
Expand Down Expand Up @@ -157,12 +164,19 @@ def process_file(self, filename):

progress = f'{filename}'

output, target_file_name, target_file_path = self.get_file_name_and_path(filename)
output, target_file_name, target_file_path, target_file_type = self.get_file_name_and_path(filename)

suffix = 1
target_file = target_file_path

while True:
if self.file_type is not None \
and self.file_type != target_file_type:
progress = f"{progress} => skipped, file is '{target_file_type}' \
but looking for '{self.file_type}'"
logger.info(progress)
break

if os.path.isfile(target_file):
if self.checksum(filename) == self.checksum(target_file):
progress = f'{progress} => skipped, duplicated file {target_file}'
Expand Down Expand Up @@ -203,8 +217,12 @@ def get_file_name_and_path(self, filename):
Returns target file name and path
"""
exif_data = Exif(filename).data()
if exif_data and 'MIMEType' in exif_data \
and self.is_image_or_video(exif_data['MIMEType']):
target_file_type = None

if exif_data and 'MIMEType' in exif_data:
target_file_type = self.get_file_type(exif_data['MIMEType'])

if target_file_type in ['image', 'video']:
date = Date(filename).from_exif(exif_data, self.timestamp, self.date_regex,
self.date_field)
output = self.get_output_dir(date)
Expand All @@ -217,7 +235,7 @@ def get_file_name_and_path(self, filename):
target_file_name = os.path.basename(filename)
target_file_path = os.path.sep.join([output, target_file_name])

return output, target_file_name, target_file_path
return output, target_file_name, target_file_path, target_file_type

def process_xmp(self, original_filename, file_name, suffix, output):
"""
Expand Down
8 changes: 4 additions & 4 deletions tests/test_phockup.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ def test_dry_run():
assert not os.path.isdir(dir4)


def test_is_image_or_video(mocker):
def test_get_file_type(mocker):
mocker.patch.object(Phockup, 'check_directories')
assert Phockup('in', '.').is_image_or_video("image/jpeg")
assert Phockup('in', '.').is_image_or_video("video/mp4")
assert not Phockup('in', '.').is_image_or_video("foo/bar")
assert Phockup('in', '.').get_file_type("image/jpeg")
assert Phockup('in', '.').get_file_type("video/mp4")
assert not Phockup('in', '.').get_file_type("foo/bar")


def test_get_file_name(mocker):
Expand Down

0 comments on commit b047003

Please sign in to comment.