Created
January 8, 2019 11:23
-
-
Save aram535/2a696aab1b7b00ee42e83137cd3e2f00 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 python3 | |
| # | |
| # ** UNKNOWN AUTHOR ** MODIFIED to meet my needs ... | |
| # if you wrote the original version, please contact me and I'll add attribution | |
| # | |
| import os | |
| import sys | |
| import re | |
| import time | |
| nr_arg = len(sys.argv) | |
| if nr_arg != 2: | |
| print("Usage: pthon3 " + os.path.basename(__file__) + " .") | |
| quit() | |
| src_root_path = str(sys.argv[1]) | |
| dst_root_path = src_root_path | |
| if not os.path.isdir(src_root_path): | |
| print("source folder doesn't exist (" + src_root_path + ")") | |
| quit() | |
| if not os.path.isdir(dst_root_path): | |
| print("destination folder doesn't exist (" + dst_root_path + ")") | |
| quit() | |
| def my_rename(src, dst): | |
| new_dst_path = os.path.dirname(dst) | |
| retries = 3 | |
| for attempt in range(0, 3): | |
| try: | |
| if True: | |
| if not os.path.exists(new_dst_path): | |
| os.makedirs(new_dst_path) | |
| os.rename(src, dst) | |
| # os.rename('/foo!', '/foo!') # uncommet to simulate OSError | |
| break | |
| except OSError as err: | |
| print("OS error: {0}".format(err) + | |
| (" - retrying" if attempt < retries - 1 else " - aborting")) | |
| time.sleep(1) | |
| def do_it(src_path, dst_path, depth): | |
| for path, dirs, files in os.walk(src_path): | |
| for name in files: | |
| pathname = os.path.join(path, name) | |
| print (pathname) | |
| dst_pathname = "" | |
| # seasons | |
| r = re.match("^.+?[-\\s]s([0-9]{2})e.*", name, re.I) | |
| if not r: | |
| r = re.match("^s([0-9]{2})e.*", name, re.I) | |
| if not r: | |
| r = re.match("^s([0-9]{4})e.*", name, re.I) | |
| if r: | |
| dst_pathname = os.path.join(os.path.join(dst_path, "Season " + | |
| str(r.group(1))), name) | |
| print("series: " + pathname + " -> " + dst_pathname) | |
| my_rename(pathname, dst_pathname) | |
| continue | |
| # movies | |
| if depth == 0: | |
| r = re.match("^(.+?\\([0-9]{4}\\)).*", name, re.I) | |
| if r: | |
| dst_pathname = os.path.join(os.path.join(dst_path, | |
| str(r.group(1))), name) | |
| print("movie: " + pathname + " -> " + dst_pathname) | |
| my_rename(pathname, dst_pathname) | |
| continue | |
| print("skipped: " + pathname) | |
| if depth == 0: | |
| for name in dirs: | |
| do_it(os.path.join(src_path, name), | |
| os.path.join(dst_path, name), depth + 1) | |
| break | |
| do_it(src_root_path, dst_root_path, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment