Created
August 4, 2016 23:28
-
-
Save ericfrederich/7ab21517ecd6defff7e749d55998f0ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import subprocess | |
import re | |
regexes = map(re.compile, [ | |
r'(IMG|PANO)_(?P<YEAR>\d{4})(?P<MONTH>\d{2})(?P<DAY>\d{2})_(?P<HOUR>\d{2})(?P<MINUTE>\d{2})(?P<SECOND>\d{2})(?P<EXTRA>.*).jpg', | |
r'VID_(?P<YEAR>\d{4})(?P<MONTH>\d{2})(?P<DAY>\d{2})_(?P<HOUR>\d{2})(?P<MINUTE>\d{2})(?P<SECOND>\d{2})(?P<EXTRA>.*).mp4', | |
]) | |
def get_files(paths): | |
""" | |
Wrapper around os.walk. | |
Yeilds only full filenames | |
""" | |
for path in paths: | |
for dirpath, dirnames, filenames in os.walk(path): | |
for filename in sorted(filenames): | |
yield os.path.join(dirpath, filename) | |
def main(args=None): | |
if args is None: | |
args = sys.argv[1:] | |
for arg in args: | |
if not os.path.isdir(arg): | |
raise ValueError("Argument not a directory %r" % arg) | |
for fname in get_files(args): | |
bn = os.path.basename(fname) | |
match = None | |
for regex in regexes: | |
match = regex.match(bn) | |
if match: | |
break | |
if not match: | |
#raise RuntimeError("Couldn't find match for %r" % bn) | |
print "Couldn't find match for %r" % bn | |
continue | |
cmd = [ | |
'touch', | |
'-t', | |
'{YEAR}{MONTH}{DAY}{HOUR}{MINUTE}.{SECOND}'.format(**match.groupdict()), | |
fname, | |
] | |
print ' '.join(cmd[:-1]), cmd[-1].split('/')[-1] | |
# subprocess.check_call(cmd) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment