-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.clj
240 lines (222 loc) · 9.63 KB
/
core.clj
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env bb
(ns petardo.kilometertjek
(:require [babashka.curl :as curl]
[cheshire.core :as json]))
(defn crude-html-strip
"Crudely remove HTML tags from string `s`."
[s]
(clojure.string/replace
s
#"^(?:<\w+ />)*<[^>]+?>(.+)</[^>]+?>(?:<\w+ />)*$"
"$1"))
(defn crude-html-strip*
"Repeatedly apply crude HTML tag strippping until no more tags are present."
[s]
(loop [ss s
acc nil]
(if (= acc ss)
acc
(recur (crude-html-strip ss) ss))))
(defn curl-get*
[& args]
(let [{:keys [status body] :as response} (apply curl/get args)]
(when (= status 200)
body)))
(defn get-license-plates
[car-identifier]
(some->> (str
"https://nummerpladeregister.dk/"
car-identifier)
(curl-get*)
(clojure.string/split-lines)
(map clojure.string/trim)
(filter #(re-find (re-pattern (str "/" car-identifier)) %))
(map crude-html-strip*)))
(defn -plate->details
[plate]
(println "Fetching plate" plate)
(some->> plate
(str "https://www.tjekbil.dk/api/v2/nummerplade/")
(curl-get*)
(json/parse-string)))
(def plate->details
(memoize -plate->details))
(defn map-interval
"Wait `interval` ms between applying `f` to individual items in `coll`."
[f coll interval]
(reduce (fn [acc x]
(if (seq coll)
(Thread/sleep interval))
(cons
(f x)
acc))
coll))
(comment
(def car-identifier "peugeot/308/2011/16-hdi-112hk-sw")
(def plates
(get-license-plates car-identifier))
(def all-details
(map-interval plate->details plates 1000))
;; (def all-details
;; (map plate->details plates))
(def details-map
(into {}
(map
(fn [{:strs [StelNr] :as details}] [StelNr details])
all-details)))
(map (fn [{:strs [KoeretoejId]}] KoeretoejId) all-details)
(->> (vals details-map)
(map #(get % "KoeretoejMotorKilometerstand") )
(filter identity)
(sort)))
(comment
(get details-map "VF34H9HR8AS341658")
{"KoeretoejId" 1020301201111205
"FoersteRegistreringDato" "2011-02-10T00:00:00+01:00"
"AntalDoere" nil
"SynResultatSynsDato" "2019-01-30T00:00:00+01:00"
"TilkoblingsvaegtUdenBremser" 750
"MotorStoerrelse" 1.6
"MiljoeOplysningCO2Udslip" nil
"MotorKilometerstand" 286
"PassagerAntal" nil
"Tilkoblingsmulighed" true
"StelNr" "VF34H9HR8AS341658"
"SporviddenBagest" nil
"LeasingPeriode" nil
"SekundaerStatusDato" "2011-02-10T00:00:00+01:00"
"RegNr" "DF54158"
"MiljoeOplysningEmissionCO" nil
"MiljoeOplysningEmissionHCPlusNOX" nil
"MiljoeOplysningRoegtaethed" nil
"DrivkraftTypeNavn" "Diesel"
"Status" "Registreret"
"KoeretoejMotorKilometerstand" 286000
"MotorMaerkning" nil
"StatusDato" "2011-02-10T00:00:00+01:00"
"FoersteRegistreringDatoFormateret" "10. februar 2011"
"KoeretoejAnvendelseNavn" "Privat personkørsel"
"NCAPTest" nil
"MaksimumHastighed" nil
"MaerkeTypeNavn" "PEUGEOT"
"TraekkendeAksler" nil
"PaahaengVognTotalVaegt" nil
"StelNummerAnbringelse" nil
"SiddepladserMinimum" 1
"NormTypeNavn" "Ingen Norm"
"ModelTypeNavn" "308"
"ReparationsIndex" 83
"MotorKoerselStoej" nil
"MiljoeOplysningEmissionNOX" nil
"FaelgDaek" nil
"TekniskTotalVaegt" 2150
"AdressePostNummer" nil
"MotorKmPerLiter" 19.2
"MiljoeOplysningRoegtaethedOmdrejningstal" nil
"MotorSlagVolumen" 1560.0
"LeasingGyldigTil" nil
"VariantTypeNavn" "1,6 HDI 112HK SW"
"TotalVaegt" 2150
"FarveTypeNavn" "Ukendt"
"KoeretoejUdstyrSamling" []
"StoersteAkselTryk" nil
"KarrosseriTypeNavn" nil
"EgenVaegt" nil
"AdressePostNummerBy" nil
"MotorCylinderAntal" nil
"MotorStandStoejOmdrejningstal" nil
"SynResultatSynsResultat" "Godkendt"
"MiljoeOplysningPartikelFilter" true
"ModelAar" "-"
"Koeretoejstand" nil
"VogntogVaegt" nil
"MotorStoersteEffekt" 82.0
"TilkoblingsvaegtMedBremser" 1000
"SporviddenForrest" nil
"KoereklarVaegtMinimum" 1563
"AkselAfstand" nil
"AkselAntal" 2
"KoeretoejArtNavn" "Personbil"
"MiljoeOplysningPartikler" nil
"GennemsnitsPris" 7507
"LeasingGyldigFra" nil
"SynResultatSynsType" "PeriodiskSyn"
"MotorStandStoej" nil
"MotorHestekraefter" 111
"MotorElektriskForbrug" nil
"SekundaerStatus" "Registreret"}
(plate->details "CS60177")
{"KoeretoejId" 1020301201115959
"FoersteRegistreringDato" "2011-05-25T00:00:00+02:00"
"AntalDoere" nil
"SynResultatSynsDato" "2019-05-16T00:00:00+02:00"
"TilkoblingsvaegtUdenBremser" 750
"MotorStoerrelse" 1.6
"MiljoeOplysningCO2Udslip" nil
"MotorKilometerstand" 178
"PassagerAntal" nil
"Tilkoblingsmulighed" true
"StelNr" "VF34H9HR8AS270487"
"SporviddenBagest" nil
"LeasingPeriode" nil
"SekundaerStatusDato" "2020-05-26T10:53:59+02:00"
"RegNr" "CS60177"
"MiljoeOplysningEmissionCO" nil
"MiljoeOplysningEmissionHCPlusNOX" nil
"MiljoeOplysningRoegtaethed" nil
"DrivkraftTypeNavn" "Diesel"
"Status" "Registreret"
"KoeretoejMotorKilometerstand" 178000
"MotorMaerkning" nil
"StatusDato" "2020-05-26T10:53:59+02:00"
"FoersteRegistreringDatoFormateret" "25. maj 2011"
"KoeretoejAnvendelseNavn" "Privat personkørsel"
"NCAPTest" nil
"MaksimumHastighed" nil
"MaerkeTypeNavn" "PEUGEOT"
"TraekkendeAksler" nil
"PaahaengVognTotalVaegt" nil
"StelNummerAnbringelse" nil
"SiddepladserMinimum" 1
"NormTypeNavn" "Euro V"
"ModelTypeNavn" "308"
"ReparationsIndex" 83
"MotorKoerselStoej" nil
"MiljoeOplysningEmissionNOX" nil
"FaelgDaek" nil
"TekniskTotalVaegt" 2150
"AdressePostNummer" nil
"MotorKmPerLiter" 19.2
"MiljoeOplysningRoegtaethedOmdrejningstal" nil
"MotorSlagVolumen" 1560.0
"LeasingGyldigTil" nil
"VariantTypeNavn" "1,6 HDI 112HK SW"
"TotalVaegt" 2150
"FarveTypeNavn" "Ukendt"
"KoeretoejUdstyrSamling" []
"StoersteAkselTryk" nil
"KarrosseriTypeNavn" nil
"EgenVaegt" nil
"AdressePostNummerBy" nil
"MotorCylinderAntal" nil
"MotorStandStoejOmdrejningstal" nil
"SynResultatSynsResultat" "Godkendt"
"MiljoeOplysningPartikelFilter" true
"ModelAar" "-"
"Koeretoejstand" nil
"VogntogVaegt" nil
"MotorStoersteEffekt" 82.0
"TilkoblingsvaegtMedBremser" 1000
"SporviddenForrest" nil
"KoereklarVaegtMinimum" 1563
"AkselAfstand" nil
"AkselAntal" 2
"KoeretoejArtNavn" "Personbil"
"MiljoeOplysningPartikler" nil
"GennemsnitsPris" 7507
"LeasingGyldigFra" nil
"SynResultatSynsType" "PeriodiskSyn"
"MotorStandStoej" nil
"MotorHestekraefter" 111
"MotorElektriskForbrug" nil
"SekundaerStatus" "Registreret"})