-
Notifications
You must be signed in to change notification settings - Fork 0
/
photo_album_rename.py
executable file
·55 lines (50 loc) · 2.21 KB
/
photo_album_rename.py
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
#!/usr/bin/python3
from pathlib import Path
import os
import re
import sys
import click
# TODO: Maybe convert these to tuples of before and after if this gets any more complicated
sought_patterns = [
r'\d{4}:\d{2}:\d{2} \d{2}:\d{2}:\d{2}\.[^\.]+', # e.g. 2022:12:06 23:03:02.jpg
r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.[^\.]+', # e.g. 2022-12-06T23:03:02.jpg
r'\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}\.[^\.]+', # e.g. 2022-12-06-23-03-02.jpg
r'\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}-\d{3}\.[^\.]+' # e.g. 2022-12-06-23-03-02-144.jpg
]
def process_match(successful_match, matched_file):
m = successful_match.group(0)
new_filename = f'{m[:4]}-{m[5:7]}-{m[8:10]} {m[11:13]}:{m[14:16]}:{m[17:19]}'
for character in list(m[20:23]):
if character.isnumeric():
if new_filename[19:20] != '.':
new_filename += '.'
new_filename += character
else:
break
new_filename += os.path.splitext(matched_file)[1]
os.rename(matched_file, f'{matched_file.parents[0]}/{new_filename}')
click.echo(f"Renamed {matched_file} to {new_filename}")
@click.command()
@click.argument('path', type=click.Path(exists=True))
@click.option('--verbose', type=click.BOOL, default=False, help="Whether not matching files should be listed in the output")
def process_files_at_path(path, verbose):
"""
Rename all files at PATH that match the internally defined patterns to the
format YYYY-MM-DD hh:mm:ss.extension (e.g. 2022-12-06 23:03:02.jpg).
The output supports milliseconds if detected in a specific pattern.
"""
for file in Path(path).rglob('*'):
if file.is_file():
file_matches_a_pattern = False
for pattern_string in sought_patterns:
if file_matches_a_pattern:
break
pattern = re.compile(pattern_string)
successful_match = re.match(pattern, str(file.name))
if successful_match:
process_match(successful_match, file)
file_matches_a_pattern = True
if verbose and not file_matches_a_pattern:
click.echo(f"Not matched: {file}")
if __name__ == '__main__':
process_files_at_path()