Skip to content

Instantly share code, notes, and snippets.

@203Null
Last active February 15, 2024 06:55
Show Gist options
  • Save 203Null/ffa10d3bfd473620609ea17a59c74d54 to your computer and use it in GitHub Desktop.
Save 203Null/ffa10d3bfd473620609ea17a59c74d54 to your computer and use it in GitHub Desktop.
import os
import glob
import re
import os
filter = ["bbs2048.org@","hhd800.com@","icao.me@","hhb_000","[456k.me]","[ThZu.Cc]"]
def filter_name(name):
for f in filter:
name.replace(f, "")
return name
# A to 1, B to 2, or a to 1, b to 2, or 1 to 1, 2 to 2
def convert_char_to_int(char):
if char.isalpha():
return ord(char.lower()) - 96
elif char.isdigit():
return int(char)
else:
return char
def process_file_names(file_names):
# sort the file names
file_names.sort()
# add -CD[i] to the file names before the extension
name_list = []
if len(file_names) == 1:
name_list.append([file_names[0], file_names[0]])
else:
for i in range(len(file_names)):
id = i
extension = os.path.splitext(file_names[i])[1]
name = os.path.splitext(file_names[i])[0]
name = filter_name(name)
# if name ends in -CD#, skip
match = re.search(r'-CD\d', name)
if match:
continue
# anywhere contains .part#
match = re.search(r'\.part\d', name)
if match:
id = convert_char_to_int(match.group()[-1])
name = name.replace(match.group(), '')
# if anywhere contains underscrore, dot, dash, space, followed by a single digit or a single letter, remove. Use regex
match = re.search(r"[\s_-][a-zA-Z0-9][\s_-]", name)
if match:
id = convert_char_to_int(match.group()[1])
name = name.replace(match.group(), match.group()[2])
# if name ends in a single digit
if name[-1].isdigit() and not name[-2].isdigit():
id = convert_char_to_int(name[-1])
if name[-2] == '-' or name[-2] == '_' or name[-2] == ' ':
name = name[:-2]
else:
name = name[:-1]
# if file ends in " [a-zA-Z]" or -[a-zA-Z] or _[a-zA-Z], remove
if (name[-2] == ' ' or name[-2] == '-' or name[-2] == '_') and (name[-1].isalpha() or name[-1].isdigit()):
id = convert_char_to_int(name[-1])
name = name[:-2]
new_name = name + '-CD' + str(id) + extension
name_list.append([file_names[i], new_name])
return name_list
def process_folder(folder):
# Get all files in the folder
print('Processing folder:', folder)
files = files = [os.path.join(folder, f) for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))]
if len(files) == 0:
print('No files found in folder. Skipping')
return
# check if renamed file exists
for file in files:
if file.endswith('renamed'):
print('Folder already processed. Skipping')
return
files = [f for f in files if f.endswith('.mp4') or f.endswith('.avi') or f.endswith('.mkv')]
# if file is less than 15mb delete it and drop it from the list
for file in files:
print('Processing file:', file)
if os.path.getsize(file) < 15 * 1024 * 1024:
print('File is less than 15mb. Deleting it')
os.remove(file)
files.remove(file)
new_name_list = process_file_names(files)
for name in new_name_list:
print(name[0], ' -> ', name[1])
if name[0] != name[1]:
os.rename(name[0], name[1])
# save a new empty file called renamed in folder
# print(folder + '/renamed')
with open(folder + '/renamed', 'w') as f:
f.write('')
# get all folders in the current directory
path = '.'
# Specify the full path of the directory
# path = 'Z:/test'
folders = [f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]
for folder in folders:
process_folder(os.path.join(path, folder))
print('\n\n\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment