|
4 | 4 | ;; SPDX-License-Identifier: EPL-2.0 |
5 | 5 | ;; License-Filename: LICENSE.txt |
6 | 6 |
|
7 | | -;; Get the json annuaire file |
8 | | -(println "Fetching annuaire as a zip file from data.gouv.fr...") |
9 | | - |
10 | | -(let [annuaire-zip-url "https://www.data.gouv.fr/fr/datasets/r/d0158eb2-6772-49c2-afb1-732e573ba1e5" |
11 | | - stream (-> (curl/get annuaire-zip-url {:as :bytes}) |
12 | | - :body |
13 | | - (io/input-stream) |
14 | | - (java.util.zip.ZipInputStream.))] |
15 | | - (.getNextEntry stream) |
16 | | - (println "Creating annuaire.json") |
17 | | - (io/copy stream (io/file "annuaire.json"))) |
18 | | - |
19 | | -;; Create a variable containing the original annuaire data |
| 7 | +(require '[clojure.tools.logging :as log]) |
| 8 | + |
| 9 | +;; Set variables |
| 10 | + |
20 | 11 | (def annuaire (atom {})) |
21 | | -(->> (json/parse-string (slurp "annuaire.json") true) |
22 | | - :service |
23 | | - (map (fn [a] [(:id a) (select-keys a [:hierarchie :nom :sigle])])) |
24 | | - (into {}) |
25 | | - (reset! annuaire)) |
26 | | - |
27 | | -;; Add service_sup |
28 | | -(println "Adding service_sup...") |
29 | | -(doseq [a (filter #(< 0 (count (:hierarchie (val %)))) @annuaire)] |
30 | | - (doseq [b (filter #(= (:type_hierarchie %) "Service Fils") (:hierarchie (val a)))] |
31 | | - (swap! annuaire update-in [(:service b)] conj {:service_sup (key a)}))) |
32 | 12 |
|
33 | 13 | (def tops |
34 | 14 | #{ |
|
72 | 52 | "b128cdf9-1bf5-4294-9470-2041e8ecd1f6" |
73 | 53 | }) |
74 | 54 |
|
| 55 | + |
| 56 | +(defn fetch-annuaire-zip [] |
| 57 | + (log/info "Fetching annuaire as a zip file from data.gouv.fr...") |
| 58 | + (let [annuaire-zip-url "https://www.data.gouv.fr/fr/datasets/r/d0158eb2-6772-49c2-afb1-732e573ba1e5" |
| 59 | + stream (-> (curl/get annuaire-zip-url {:as :bytes}) |
| 60 | + :body |
| 61 | + (io/input-stream) |
| 62 | + (java.util.zip.ZipInputStream.))] |
| 63 | + (.getNextEntry stream) |
| 64 | + (log/info "Output annuaire.json") |
| 65 | + (io/copy stream (io/file "annuaire.json")))) |
| 66 | + |
| 67 | +(defn set-annuaire! [] |
| 68 | + (->> (json/parse-string (slurp "annuaire.json") true) |
| 69 | + :service |
| 70 | + (map (fn [a] [(:id a) (select-keys a [:hierarchie :nom :sigle])])) |
| 71 | + (into {}) |
| 72 | + (reset! annuaire))) |
| 73 | + |
| 74 | +(defn add-service-sup! [] |
| 75 | + (log/info "Adding service_sup...") |
| 76 | + (doseq [a (filter #(< 0 (count (:hierarchie (val %)))) @annuaire)] |
| 77 | + (doseq [b (filter #(= (:type_hierarchie %) "Service Fils") (:hierarchie (val a)))] |
| 78 | + (swap! annuaire update-in [(:service b)] conj {:service_sup (key a)})))) |
| 79 | + |
75 | 80 | (defn get-ancestor [service_sup_id] |
76 | 81 | (let [seen (atom #{})] |
77 | 82 | (loop [s_id service_sup_id] |
|
83 | 88 | (do (swap! seen conj s_id) |
84 | 89 | (recur sup))))))) |
85 | 90 |
|
86 | | -;; Add service_top |
87 | | -(println "Adding service_top...") |
| 91 | +(defn add-service-top! [] |
| 92 | + (doseq [a (filter #(seq (:service_sup (val %))) @annuaire)] |
| 93 | + (swap! annuaire update-in |
| 94 | + [(key a)] |
| 95 | + conj {:service_top (get-ancestor (:service_sup (val a)))}))) |
| 96 | + |
| 97 | +(defn output-annuaire-sup [] |
| 98 | + (log/info "Output annuaire_sup.json...") |
| 99 | + (spit "annuaire_sup.json" |
| 100 | + (json/generate-string |
| 101 | + (map (fn [[k v]] (conj v {:id k})) @annuaire) |
| 102 | + {:pretty true}))) |
| 103 | + |
| 104 | +(defn output-annuaire-tops [] |
| 105 | + (log/info "Output annuaire_tops.json...") |
| 106 | + (spit "annuaire_tops.json" |
| 107 | + (-> (map #(hash-map % (:nom (get @annuaire %))) tops) |
| 108 | + (json/generate-string {:pretty true})))) |
88 | 109 |
|
89 | | -(doseq [a (filter #(seq (:service_sup (val %))) @annuaire)] |
90 | | - (swap! annuaire update-in |
91 | | - [(key a)] |
92 | | - conj {:service_top (get-ancestor (:service_sup (val a)))})) |
| 110 | +(some #{"c"} #{"a" "b"}) |
93 | 111 |
|
94 | | -;; Output annuaire_sup.json |
95 | | -(println "Creating annuaire_sup.json...") |
| 112 | +(defn -main [] |
| 113 | + (fetch-annuaire-zip) |
| 114 | + (set-annuaire!) |
| 115 | + (add-service-sup!) |
| 116 | + (add-service-top!) |
| 117 | + (output-annuaire-sup) |
| 118 | + (output-annuaire-tops)) |
96 | 119 |
|
97 | | -(spit "annuaire_sup.json" |
98 | | - (json/generate-string |
99 | | - (map (fn [[k v]] (conj v {:id k})) @annuaire) |
100 | | - {:pretty true})) |
| 120 | +(-main) |
101 | 121 |
|
102 | | -;; Output annuaire_tops.json |
103 | | -(spit "annuaire_tops.json" |
104 | | - (-> (map #(hash-map % (:nom (get @annuaire %))) tops) |
105 | | - (json/generate-string {:pretty true}))) |
|
0 commit comments