Last active
January 24, 2025 16:15
-
-
Save curegit/4d12d680511764ff823c8c26caf8606e to your computer and use it in GitHub Desktop.
Twitter から保存した画像を時系列順にリネームするスクリプト
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 | |
import os | |
import os.path | |
import pathlib | |
import sys | |
import glob | |
def map(char): | |
a = ord(char) | |
if 48 <= a <= 57: | |
return "~" + char | |
elif char == "-": | |
return "~~|" | |
elif char == "_": | |
return "~~~" | |
else: | |
return char | |
def key(str): | |
return "".join([map(c) for c in str]) | |
def new_filepath(path, new, suffix="+"): | |
root, ext = os.path.splitext(path) | |
head, tail = os.path.split(root) | |
path = os.path.join(head, new) + ext | |
if os.path.lexists(path): | |
return new_filepath(path, new + suffix, suffix) | |
return path | |
if __name__ == "__main__": | |
files = [pathlib.Path(f) for f in glob.glob(os.path.join(sys.argv[1], "*")) if os.path.isfile(f)] | |
keep = [] | |
change = [] | |
for f in files: | |
if len(f.stem) <= 7: | |
keep.append(f) | |
else: | |
change.append(f) | |
start = 1 | |
for f in keep: | |
try: | |
start = max(int(f.stem) + 1, start) | |
except Exception: | |
pass | |
new = sorted([str(f) for f in change], key=key) | |
for i, f in enumerate(new, start): | |
n = new_filepath(f, str(i)) | |
os.rename(f, n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment