Skip to content

Instantly share code, notes, and snippets.

@hkusoft
Last active March 5, 2023 13:28
Show Gist options
  • Save hkusoft/a2924b92464756fc8c22a859325e7eef to your computer and use it in GitHub Desktop.
Save hkusoft/a2924b92464756fc8c22a859325e7eef to your computer and use it in GitHub Desktop.
Use this script to scan all files in an input folder : Any file that has the ISBN format name, e.g. 9781803239736.epub etc. will be renamed to the real file name, e.g. ·Microsoft Excel.epub·
# Use this function scans all files in an input folder
# Any file that has the ISBN format name, e.g. 9781803239736.epub etc.
# will be renamed to the real file name, e.g. ·Microsoft Excel.epub·
# Install dependencies with virtualdev
# ```
# pip install virtualenv
# virtualenv winenv # winenv is any name your like to create
# Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass # important, winOS only
# pip install -r .\requirements.txt # install dependencies
# Python3 ./main.py /Some/InputFolder/ /Some/OutputDire # or
# Python main.py C:\Temp\ D:\output\
# ```
# usage:
# Python3 ./main.py /Some/InputFolder/ /Some/OutputFolder
# Python3 ./main.py /Some/InputFolder/
# Troubleshooting
# If you don't want to use virutal env, install dependencies manually
# pip install isbnlib
# pip install tabulate
# pip install termcolor
# pip install pathlib
import os, sys
import isbnlib
from pathlib import Path
from tabulate import tabulate
from termcolor import colored
# Please change your default output directory as you see fit
default_out_dir_osx = '/Users/david/Downloads/Compressed/_eBooks/'
default_out_dir_win = "m:\\Downloads\\Compressed\\_eBooks\\"
table = []
def get_isbn_file_counts(input_dir):
n = 0
for filename in os.listdir(input_dir):
f = os.path.join(input_dir, filename)
if os.path.isfile(f) and isbnlib.ISBN13(f):
n += 1
return n
def get_output_dir():
argv = sys.argv
if sys.platform == 'win32':
default_out_dir = default_out_dir_win
elif sys.platform == 'linux' or sys.platform == 'darwin':
default_out_dir = default_out_dir_osx
if len(argv) < 3:
print('Use defualt output directory: ' + default_out_dir)
if not os.path.isdir(default_out_dir):
print(colored('✘ ' + "Output folder does NOT exist --" + default_out_dir, 'red'))
return None
output_dir = default_out_dir if len(argv) < 3 else argv[2]
return output_dir
def get_input_dir():
if len(sys.argv) < 2:
print(colored('No input directory is input, use current directory instead', 'red'))
source_dir = './'
else:
print(colored('Scanning ISBN files in input directory ' + sys.argv[1], 'green'))
source_dir = sys.argv[1]
return source_dir
def rename_all_isbn_files(input_dir, output_dir):
n = get_isbn_file_counts(input_dir)
if n < 1:
print("No files for ISBN Books found in " + input_dir)
return
i = 0
for filename in os.listdir(input_dir):
f = os.path.join(input_dir, filename)
if os.path.isfile(f) and isbnlib.ISBN13(f):
i += 1
isbn = Path(f).stem;
meta = isbnlib.meta(isbn)
ext = os.path.splitext(f)[1]
progress = (str(int(i * 100 / n)) + '% ').rjust(6)
if meta:
new_name = meta.get('Title')
table.append([os.path.basename(f), new_name])
new_path = str(Path(output_dir, new_name).with_suffix(ext))
print(colored('✔ ', 'green') + progress + os.path.basename(f)[-13:] + ' ⟼➤ ' + new_path)
os.rename(f, new_path)
# break
else:
print(colored('✘ ', 'red') + progress + os.path.basename(f)[
-13:] + ' ⟼➤ ' + 'Failed to find ISBN book for this!')
if __name__ == '__main__':
source_dir = get_input_dir()
output_dir = get_output_dir()
if not output_dir:
sys.exit()
table = [['Source File in\n' + source_dir, 'Renamed File in\n' + output_dir]]
rename_all_isbn_files(source_dir, output_dir)
print(tabulate(table, headers='firstrow', tablefmt='fancy_grid'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment