-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathxattr.groovy
executable file
·176 lines (140 loc) · 3.48 KB
/
xattr.groovy
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
#!/usr/bin/env -S filebot -script
def xattrFiles = []
def xattrFolders = [] as Set
args.flatten{ f -> f.isDirectory() ? f.listFiles{ true } : f }.each{ f ->
// sanity check input file path
if (!f.exists()) {
log.warning "File does not exist: $f"
return
}
// read / write custom xattr values
_def.each{ k, v -> setXattrKey(f, k, v) }
// select files with xattr metadata
if (f.xattr.keySet().any{ k -> !isSystemKey(k) }) {
xattrFiles += f
}
// manage .xattr folders
if (f.name == /net.filebot.metadata/ || f.name == /net.filebot.mediainfo/) {
xattrFolders += f.dir.dir
}
}
xattrFiles.each{ f ->
log.finest "$f"
f.xattr.each{ k, v ->
if (v ==~ /(?U)[\p{Print}\p{Space}]*/) {
log.fine "\t$k: $v"
} else {
// use 1 replacement character for each digit of magnitude
def holder = v == null ? '␀' : '�' * v.length().toString().length()
log.fine "\t$k: $holder"
}
}
if (_args.action =~ /clear/) {
clearXattr(f)
}
if (_args.action =~ /dump/) {
dumpXattr(f)
}
if (_args.action =~ /refresh/) {
refreshMetadata(f)
}
if (_args.action =~ /import/) {
kMDItemUserTags(f)
}
}
xattrFolders.each{ dir ->
if (_args.action =~ /clear/) {
clearXattrFolder(dir)
}
if (_args.action =~ /prune/) {
pruneXattrFolder(dir)
}
}
if (xattrFiles && _args.action =~ /clear/) {
['mediainfo', 'ffprobe', 'CRC32'].each{ n ->
def cache = Cache.getCache(n, CacheType.Monthly)
if (cache.keys) {
help "[CLEAR] ${n} cache (${cache.keys.size()})"
cache.clear()
}
}
}
if (!xattrFiles && !xattrFolders) {
log.warning "No xattr metadata found"
}
// hide system xattr keys
def isSystemKey(k) {
k.startsWith('com.apple.') && _args.strict
}
// clear xattr metadata
def setXattrKey(f, k, v) {
if (v) {
log.info "[SET] $f:$k = $v"
f.xattr[k] = v
} else if (f.xattr[k]) {
log.info "[UNSET] $f:$k"
f.xattr[k] = null
}
}
// clear xattr metadata
def clearXattr(f) {
log.info "[CLEAR] $f.metadata [$f]"
f.xattr.clear()
}
// dump xattr metadata as plain/text files
def dumpXattr(f) {
f.xattr.list().each{ k ->
def v = f.xattr.read(k).saveAs("${f}#${k}")
log.info "[DUMP] ${v} (${v.displaySize})"
}
}
// refresh xattr metadata
def refreshMetadata(f) {
def e = f.metadata
if (e instanceof Episode) {
def i = e.seriesInfo
log.finest "[UPDATE] $i | $e [$f]"
if (i instanceof SeriesInfo) {
def episodeList = WebServices.getEpisodeListProvider(i.database).getEpisodeList(i.id, i.order as SortOrder, i.language as Locale)
if (e instanceof MultiEpisode) {
e = e.episodes.collect{ p -> episodeList.find{ it.id == p.id } } as MultiEpisode
} else {
e = episodeList.find{ it.id == e.id } as Episode
}
// update xattr metadata
if (e) {
f.metadata = e
}
}
}
}
// import xattr metadata into Mac OS X Finder tags (UAYOR)
def kMDItemUserTags(f) {
def tags = getMediaInfo(f, _args.format ?: '{movie; /Movie/}|{episode; /Episode/}|{y}|{source}|{vf}|{hd}').tokenize('|').findResults{ it }
def plist = XML{
plist(version:'1.0') {
array {
tags.each{
string(it)
}
}
}
}
log.info "[IMPORT] Write tag plist to xattr [com.apple.metadata:_kMDItemUserTags]: $plist"
f.xattr['com.apple.metadata:_kMDItemUserTags'] = plist
}
// delete .xattr folders
def clearXattrFolder(dir) {
log.info "[DELETE] $dir"
dir.trash()
}
// delete .xattr/<name> folders if <name> no longer exists
def pruneXattrFolder(dir) {
dir.listFiles().each{ key ->
def f = key.dir.dir / key.name
if (!f.exists()) {
log.info "[DELETE] $key"
key.trash()
}
}
}