|
80 | 80 | (mc/update db card-collection {:code code} {$addToSet {k path}}) |
81 | 81 | (mc/update db card-collection {:previous-versions {$elemMatch {:code code}}} {$addToSet {prev-k path}}))))))) |
82 | 82 |
|
| 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 | + |
83 | 124 | (defn- add-alt-images |
84 | 125 | "All all images in the specified alt directory" |
85 | 126 | [db base-path lang resolution alt-dir] |
86 | 127 | (let [alt (keyword (.getName alt-dir)) |
87 | | - images (find-files alt-dir)] |
| 128 | + images (filter-dups (find-files alt-dir))] |
88 | 129 | (run! #(add-card-image db base-path lang resolution alt %) images) |
89 | 130 | (println "Added" (count images) "images to" lang resolution alt))) |
90 | 131 |
|
91 | 132 | (defn- add-resolution-images |
92 | 133 | "Add all images in the specified resolution directory" |
93 | 134 | [db base-path lang res-dir] |
94 | 135 | (let [resolution (keyword (.getName res-dir)) |
95 | | - alts (find-dirs res-dir) |
96 | | - images (find-files res-dir)] |
| 136 | + alts (find-dirs res-dir)] |
97 | 137 | (run! #(add-alt-images db base-path lang resolution %) alts))) |
98 | 138 |
|
99 | 139 | (defn- add-language-images |
|
0 commit comments