-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeline2sqlite.cljs
More file actions
59 lines (43 loc) · 1.89 KB
/
timeline2sqlite.cljs
File metadata and controls
59 lines (43 loc) · 1.89 KB
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
(ns timeline2sqlite
(:require ["fs" :as fs]
["sql.js$default" :as sql-js]))
(defn read-file [filename] (-> filename fs/readFileSync str))
(defn parse-json [obj] (-> obj js/JSON.parse (js->clj :keywordize-keys true)))
(defn extract-timeline [content] (-> content parse-json :timelineObjects))
(defn timeline->placevisits [timeline] (->> timeline (filterv :placeVisit) (map :placeVisit)))
(defn get-location [file-content]
(->> file-content
extract-timeline
timeline->placevisits
(map :location)))
(defn normalize-coord [coord] (/ (js/parseInt coord) 1e7))
(defn location->place [location] {:id (:placeId location)
:latitude (normalize-coord (:latitudeE7 location))
:longitude (normalize-coord (:longitudeE7 location))
:name (:name location)
:address (:address location)})
(defn create-db [] (new (.-Database sql-js)))
(defn export-db [fname db] (fs/writeFileSync fname (.from js/Buffer (.export db))))
(defn insert-place-to-db [db place]
(.run db "INSERT OR IGNORE INTO Places VALUES(?, ?, ?, ?, ?)"
(clj->js (map place [:id :latitude :longitude :name :address]))))
(defn places->db [places]
(let [db (create-db)]
(.run db "CREATE TABLE IF NOT EXISTS Places (Id TEXT UNIQUE, Latitude REAL, Longitude REAL, Name TEXT, Address TEXT)")
(doseq [place places] (insert-place-to-db db place))
db))
(defn convert [filenames]
(->> filenames
(map read-file)
(map get-location)
concat
flatten
(map location->place)
(places->db)
(export-db "places.sqlite")))
(def cli-args (not-empty (js->clj (.slice js/process.argv 3))))
(defn main [args]
(if (pos? (count args))
(convert args)
(js/console.error "Usage: timeline2sqlite somefile.json [anotherfile.json]")))
(main cli-args)