-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag-anime-screenshot.py
executable file
·245 lines (233 loc) · 8.22 KB
/
tag-anime-screenshot.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/python3
import anime
import os
import logging
import shutil
import re
import exiv2
import configparser
import argparse
from sqlalchemy.exc import NoResultFound
def get_config(config_file=os.path.expanduser("~/.config/weltschmerz/weltschmerz.cfg")):
config = configparser.ConfigParser()
config.read_file(open(config_file))
parser = argparse.ArgumentParser(
description="Tag screenshot file with source file metadata and add title card screenshot to database"
)
parser.add_argument(
"--database",
help="database to use",
type=str,
default=config.get("tag-screenshot", "database"),
)
parser.add_argument(
"--log-file",
dest="log_file",
help="logfile to use",
type=str,
default=config.get("tag-screenshot", "logfile"),
)
parser.add_argument(
"-S",
"--screenshot-file",
help="screenshot file to tag",
default=None,
type=str,
dest="screenshot_file",
)
parser.add_argument(
"-F",
"--file-path",
help="file to query for",
type=str,
default=None,
dest="file_path",
)
parser.add_argument(
"-s",
"--file-size",
help="filesize to query for",
default=None,
type=int,
dest="file_size",
)
parser.add_argument("-d", "--duration", help="file duration", default=None)
parser.add_argument(
"-D", "--duration-raw", help="file duration (raw)", default=None
)
parser.add_argument("-t", "--timepos", help="timepos of screenshot", default=None)
parser.add_argument(
"-T",
"--timepos-raw",
help="timepos (raw) of screenshot",
default=None,
dest="timepos_raw",
)
parser.add_argument(
"-B",
"--target-basedir",
help="timepos (raw) of screenshot",
default=config.get("tag-screenshot", "basedir"),
dest="target_basedir",
)
parser.add_argument(
"--screenshot-type", help="screenshot type", type=int, default=1
)
args = parser.parse_args()
logging.basicConfig(
filename=args.log_file,
format="%(asctime)s - %(levelname)s - %(message)s",
level=logging.DEBUG,
)
return args
def tag_screenshot(
screenshot: str,
filename: str,
filesize: int,
duration: str,
duration_raw: str,
timepos: str,
timepos_raw: str,
screenshot_type: int,
hash_crc: str,
hash_md5: str,
hash_sha1: str,
hash_ed2k: str,
):
logging.info(f"tagging {config.screenshot_file}...")
exiv2.XmpProperties.registerNs("http://ns.eroforce.one/anime/1.0/", "anime")
screenshot_image = exiv2.ImageFactory.open(screenshot)
screenshot_image.readMetadata()
metadata = screenshot_image.xmpData()
metadata["Xmp.anime.AnimeFileName"] = filename
metadata["Xmp.anime.AnimeFileSize"] = filesize
metadata["Xmp.anime.AnimeFileDuration"] = duration
metadata["Xmp.anime.AnimeFileDurationRaw"] = duration_raw
metadata["Xmp.anime.AnimeFileTime"] = timepos
metadata["Xmp.anime.AnimeFileTimeRaw"] = timepos_raw
metadata["Xmp.anime.AnimeFileCrc32"] = hash_crc
metadata["Xmp.anime.AnimeFileMd5"] = hash_md5
metadata["Xmp.anime.AnimeFileSha1"] = hash_sha1
metadata["Xmp.anime.AnimeFileEd2k"] = hash_ed2k
metadata["Xmp.anime.ScreenshotType"] = screenshot_type
screenshot_image.setXmpData(metadata)
screenshot_image.writeMetadata()
logging.info(f"tagged {config.screenshot_file}")
def tag_screenshot_min(
screenshot: str,
filename: str,
filesize: int,
duration: str,
duration_raw: str,
timepos: str,
timepos_raw: str,
screenshot_type: int,
):
logging.info(f"tagging (min) {config.screenshot_file}...")
exiv2.XmpProperties.registerNs("http://ns.eroforce.one/anime/1.0/", "anime")
screenshot_image = exiv2.ImageFactory.open(screenshot)
screenshot_image.readMetadata()
metadata = screenshot_image.xmpData()
metadata["Xmp.anime.AnimeFileName"] = filename
metadata["Xmp.anime.AnimeFileSize"] = filesize
metadata["Xmp.anime.AnimeFileDuration"] = duration
metadata["Xmp.anime.AnimeFileDurationRaw"] = duration_raw
metadata["Xmp.anime.AnimeFileTime"] = timepos
metadata["Xmp.anime.AnimeFileTimeRaw"] = timepos_raw
metadata["Xmp.anime.ScreenshotType"] = screenshot_type
screenshot_image.setXmpData(metadata)
screenshot_image.writeMetadata()
logging.info(f"tagged (min) {config.screenshot_file}")
if __name__ == "__main__":
config = get_config()
dbs = anime.DatabaseSession(config.database, False)
real_path = os.path.realpath(os.path.abspath(config.file_path))
logging.info(f"processing {config.screenshot_file}")
(directory, filename) = os.path.split(real_path)
try:
known_file = (
dbs.session.query(anime.LocalFile)
.filter(
anime.LocalFile.directory == directory.rstrip("/"),
anime.LocalFile.filename == filename,
anime.LocalFile.filesize == config.file_size,
)
.one()
)
logging.info(
f"found file '{filename}' in DB, adding metadata for {config.timepos} ..."
)
tag_screenshot(
screenshot=config.screenshot_file,
filename=known_file.filename,
filesize=config.file_size,
duration=config.duration,
duration_raw=config.duration_raw,
timepos=config.timepos,
timepos_raw=config.timepos_raw,
screenshot_type=config.screenshot_type,
hash_crc=known_file.hash_crc,
hash_md5=known_file.hash_md5,
hash_sha1=known_file.hash_sha1,
hash_ed2k=known_file.hash_ed2k,
)
tss = anime.TitleScreenShot(
filename=config.screenshot_file,
source_file_name=known_file.filename,
source_file_size=config.file_size,
source_file_hash_ed2k=known_file.hash_ed2k,
source_file_hash_crc=known_file.hash_crc,
source_file_hash_md5=known_file.hash_md5,
source_file_hash_sha1=known_file.hash_sha1,
source_file_duration=config.duration,
source_file_duration_raw=config.duration_raw,
time_position=config.timepos,
time_position_raw=config.timepos_raw,
title_type=config.screenshot_type,
)
if re.search("(by-id(/[0-9]{2}){3})", real_path):
target_folder = os.path.join(
config.target_basedir,
re.search("(by-id(/[0-9]{2}){3})", real_path).group(1),
)
target_file = os.path.join(
target_folder, os.path.basename(config.screenshot_file)
)
if os.path.isfile(target_file):
logging.warning(
f"screenshot '{target_file}' already present, deleting '{config.screenshot_file}' ..."
)
os.unlink(config.screenshot_file)
else:
logging.info(f"found anime id, moving file to '{target_folder}'...")
os.makedirs(os.path.abspath(target_folder), exist_ok=True)
shutil.move(config.screenshot_file, target_file)
tss.filename = target_file
logging.info(
f"adding title screenshot to DB for '{tss.filename}' at {config.timepos} ..."
)
try:
anidb_file = (
dbs.session.query(anime.File)
.filter(anime.File.hash_ed2k == known_file.hash_ed2k)
.one()
)
tss.aid = anidb_file.aid
tss.eid = anidb_file.eid
tss.fid = anidb_file.fid
except:
pass
dbs.session.merge(tss)
dbs.session.commit()
except NoResultFound as e:
tag_screenshot_min(
screenshot=config.screenshot_file,
filename=filename,
filesize=config.file_size,
duration=config.duration,
duration_raw=config.duration_raw,
timepos=config.timepos,
timepos_raw=config.timepos_raw,
screenshot_type=config.screenshot_type,
)
logging.warning(f"'{filename}' not found in DB, only tagged minimal info ...")