-
Notifications
You must be signed in to change notification settings - Fork 3
/
core.clj
71 lines (65 loc) · 2.09 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
(ns uap-clj.core
"Core library with entrypoint main function"
(:refer-clojure :exclude [load])
(:require [immuconf.config :as conf :refer [load]]
[uap-clj.conf :refer [base-config
local-config]]
[uap-clj.common :as common :refer :all]
[uap-clj.browser :refer [browser]]
[uap-clj.os :refer [os]]
[uap-clj.device :refer [device]]
[clj-yaml.core :refer [parse-string]]
[clojure.java.io :as io :refer [resource]]
[clojure.string :as s :refer [join trim]]))
(defn config
"Load & merge configuration from a path of configuration file
sources.
"
[]
(apply conf/load
(filter (partial not= nil)
[(base-config)
(local-config)])))
(defn useragent
"Look up all 3 sets of fields for:
- browser
- o/s
- device
"
[ua]
{:ua ua
:browser (browser ua)
:os (os ua)
:device (device ua)})
; For use in production settings where speed may be preferred
; in exchange for the tradeoff of increased memory bloat:
(def useragent-memoized (memoize useragent))
(def cfg (config))
(def columns (:output-columns cfg))
(def header
(str
(s/join \tab
(map #(s/join " " (map name (flatten %)))
columns))
\newline))
(defn -main
"Takes a filename from the commandline containing one useragent
per line, and writes (or overwrites) a TSV (tab-separated) file
with a header.
If no output file is provided as an argument,
'useragent_lookup.tsv' is the name of the default output file.
"
[in-file & opt-args]
(with-open [rdr (clojure.java.io/reader in-file)]
(let [out-file (or (first opt-args)
(:output-filename cfg))
results (doall
(map useragent (line-seq rdr)))]
(with-open
[wtr (clojure.java.io/writer out-file :append false)]
(.write wtr header)
(doseq [ua results]
(.write wtr
(str (s/join \tab
(map #(get-in ua %) columns))
\newline)))))))