Skip to content

Commit da2dc79

Browse files
authored
Merge pull request #8549 from jwarwick/lein_fetch_dups
Only import single card art for an art package
2 parents 0c5b7cf + 579dd8e commit da2dc79

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

src/clj/tasks/images.clj

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,60 @@
8080
(mc/update db card-collection {:code code} {$addToSet {k path}})
8181
(mc/update db card-collection {:previous-versions {$elemMatch {:code code}}} {$addToSet {prev-k path}})))))))
8282

83+
(def ^:private format-rank
84+
{"gif" 0
85+
"jpg" 1
86+
"jpeg" 1
87+
"png" 2})
88+
89+
(defn- normalize-fmt [s]
90+
(some-> s str/lower-case))
91+
92+
(defn- higher-quality?
93+
"True if fmt1 is higher quality than fmt2. Unknown formats are treated as lowest quality."
94+
[fmt1 fmt2]
95+
(> (get format-rank (normalize-fmt fmt1) -1)
96+
(get format-rank (normalize-fmt fmt2) -1)))
97+
98+
(defn- add-best-card
99+
"Add a card to the map; if the card-id already exists, keep the highest quality one."
100+
[acc card]
101+
(let [card-name (.getName card)
102+
[card-id fmt] (str/split card-name #"\." 2)
103+
curr-card (get acc card-id)
104+
curr-card-name (if (nil? curr-card) "" (.getName curr-card))]
105+
(cond
106+
(nil? curr-card) (assoc acc card-id card)
107+
108+
(let [[_ curr-fmt] (str/split curr-card-name #"\." 2)]
109+
(higher-quality? fmt curr-fmt))
110+
(do
111+
(println "Replacing" curr-card-name "," card-name "is higher image quality.")
112+
(assoc acc card-id card))
113+
114+
:else
115+
(do
116+
(println "Not importing" card-name "," curr-card-name "is higher image quality.")
117+
acc))))
118+
119+
(defn- filter-dups
120+
"Some cards are available in multiple image formats. Only use the highest quality one."
121+
[cards]
122+
(vals (reduce add-best-card {} cards)))
123+
83124
(defn- add-alt-images
84125
"All all images in the specified alt directory"
85126
[db base-path lang resolution alt-dir]
86127
(let [alt (keyword (.getName alt-dir))
87-
images (find-files alt-dir)]
128+
images (filter-dups (find-files alt-dir))]
88129
(run! #(add-card-image db base-path lang resolution alt %) images)
89130
(println "Added" (count images) "images to" lang resolution alt)))
90131

91132
(defn- add-resolution-images
92133
"Add all images in the specified resolution directory"
93134
[db base-path lang res-dir]
94135
(let [resolution (keyword (.getName res-dir))
95-
alts (find-dirs res-dir)
96-
images (find-files res-dir)]
136+
alts (find-dirs res-dir)]
97137
(run! #(add-alt-images db base-path lang resolution %) alts)))
98138

99139
(defn- add-language-images

0 commit comments

Comments
 (0)