-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_files.py
executable file
·98 lines (91 loc) · 3.38 KB
/
rename_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python3
import anime
import configparser
import argparse
import os.path
def get_config(config_file="weltschmerz.cfg"):
config = configparser.ConfigParser()
config.read_file(open(config_file))
parser = argparse.ArgumentParser(
description="Return unknown files from the database."
)
parser.add_argument(
"--database", help="database to use", default=config.get("client", "database")
)
parser.add_argument(
"--log-file",
dest="log_file",
help="logfile to use",
default=config.get("client", "log"),
)
parser.add_argument("--folder", help="folder to query", default=None)
parser.add_argument(
"--dry-run",
"-n",
help="dry run, do not rename",
default=False,
dest="dry_run",
action="store_true",
)
parser.add_argument(
"--target-crc32",
"-C",
help="add crc32 not target filename",
default=False,
dest="target_crc32",
action="store_true",
)
args = parser.parse_args()
return args
if __name__ == "__main__":
extension = "backup"
config = get_config()
dbs = anime.DatabaseSession(config.database, False)
if config.folder:
unknown_files = (
dbs.session.query(anime.LocalFile)
.filter(
anime.LocalFile.filename.like(f"%.{extension}"),
anime.LocalFile.directory.like(f'{config.folder.rstrip("/")}%'),
)
.order_by(anime.LocalFile.directory, anime.LocalFile.filename)
.all()
)
# unknown_files = dbs.session.query(anime.LocalFile).filter(anime.LocalFile.filename.like(f'%.{extension}'), anime.LocalFile.directory == config.folder.rstrip('/')).order_by(anime.LocalFile.directory, anime.LocalFile.filename).all()
[print(unknown_file.ed2k_link()) for unknown_file in unknown_files]
else:
unknown_files = (
dbs.session.query(anime.LocalFile)
.filter(anime.LocalFile.filename.like(f"%.{extension}"))
.order_by(anime.LocalFile.directory, anime.LocalFile.filename)
.all()
)
# sys.exit(0)
for local_file in unknown_files:
# TODO: replace with removesuffix()
target_filename = local_file.filename[: -len(extension) - 1]
if config.target_crc32:
target_filename = (
target_filename[: -len(target_filename.split(".")[-1]) - 1]
+ f'[{local_file.hash_crc}].{target_filename.split(".")[-1]}'
)
print(f"{local_file.filename} -> {target_filename}")
target_path = os.path.join(local_file.directory, target_filename)
if os.path.isfile(target_path):
print(
f"local file {local_file.filename} could *not* be renamed, target exists: {target_path}"
)
else:
print(
f"local file {local_file.filename} could be renamed to {target_filename}"
)
if not config.dry_run:
try:
os.rename(
os.path.join(local_file.directory, local_file.filename),
target_path,
)
local_file.filename = target_filename
dbs.session.commit()
except Exception:
print(f"something wrong with {local_file.filename}")