Last active
August 20, 2019 18:28
-
-
Save kestel/7963419b3fe68f93d0d1e4e1d0153b07 to your computer and use it in GitHub Desktop.
Rename files from format Safari Books Online Video Downloader to more human friendly
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 | |
"""Rename files from format Safari Books Online Video Downloader | |
to more friendly""" | |
import os | |
import re | |
time_re = re.compile('(\d+m \d+s|\d+s).mp4') | |
number_re = re.compile('((\d{3})\s-\s).+.mp4') | |
chapter_re = re.compile('\d{3}-(\d+\s\d+).+.mp4') | |
def renamer(target_dir, do_work): | |
listdir = sorted(os.listdir(target_dir)) | |
for f in listdir: | |
new_filename = f | |
time_find = re.findall(time_re, new_filename) | |
if time_find: | |
new_filename = new_filename.replace(time_find[0], "") | |
number_find = re.findall(number_re, new_filename) | |
if number_find: | |
new_filename = new_filename.replace(number_find[0][0], number_find[0][1]+"-") | |
chapter_find = re.findall(chapter_re, new_filename) | |
if chapter_find: | |
new_filename = new_filename.replace(chapter_find[0], chapter_find[0].replace(" ", ".")) | |
if do_work: | |
os.rename(f, new_filename) | |
else: | |
print(f"{f} -> {new_filename}") | |
def main(): | |
import argparse | |
parser = argparse.ArgumentParser(description=__doc__) | |
parser.add_argument("-work", help="Do actual work, not only test", action="store_true") | |
do_work = parser.parse_args().work | |
dirpath = os.getcwd() | |
# print("current directory is : " + dirpath) | |
# foldername = os.path.basename(dirpath) | |
# print("Directory name is : " + foldername) | |
# scriptpath = os.path.realpath(__file__) | |
# print("Script path is : " + scriptpath) | |
renamer(dirpath, do_work) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment