Skip to content

Commit 894f08e

Browse files
author
Bastien Guerry
committed
src/annuaire-service-public-enrichi.clj: Refactoring
1 parent 102155a commit 894f08e

File tree

1 file changed

+56
-40
lines changed

1 file changed

+56
-40
lines changed

src/annuaire-service-public-enrichi.clj

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,11 @@
44
;; SPDX-License-Identifier: EPL-2.0
55
;; License-Filename: LICENSE.txt
66

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+
2011
(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)})))
3212

3313
(def tops
3414
#{
@@ -72,6 +52,31 @@
7252
"b128cdf9-1bf5-4294-9470-2041e8ecd1f6"
7353
})
7454

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+
7580
(defn get-ancestor [service_sup_id]
7681
(let [seen (atom #{})]
7782
(loop [s_id service_sup_id]
@@ -83,23 +88,34 @@
8388
(do (swap! seen conj s_id)
8489
(recur sup)))))))
8590

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}))))
88109

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"})
93111

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))
96119

97-
(spit "annuaire_sup.json"
98-
(json/generate-string
99-
(map (fn [[k v]] (conj v {:id k})) @annuaire)
100-
{:pretty true}))
120+
(-main)
101121

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

Comments
 (0)