/
core.clj
124 lines (107 loc) · 3.3 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
(ns linkedin-connections.core
(:require [clojure-csv.core :as csv])
(:require [clojure.java.io :as io])
(:gen-class :main true))
;; ------------------------------------------------------------------------
;; mac osx
;; TODO
;; ------------------------------------------------------------------------
;; ------------------------------------------------------------------------
;; Yahoo format
;;
(defn is-yahoo
"The Yahoo file header starts with \"First\""
[header]
(= "First" (first (first (csv/parse-csv header)))))
;; ------------------------------------------------------------------------
(def yahoo-extract
" Yahoo extractor function
|---------+-------|
| Column | Index |
|---------+-------|
| First | 0 |
| Last | 2 |
| Email | 4 |
| Company | 21 |
| Title | 20 |
|---------+-------|
"
(juxt
#(nth % 0)
#(nth % 2)
#(nth % 4)
#(nth % 21)
#(nth % 20)))
;; ------------------------------------------------------------------------
;; The Microsoft Outlook and Outlook Express file formats are the same.
;;
(defn is-outlook [header]
"The Outlook file format starts with \"Title\".
"
(not (is-yahoo header)))
;; ------------------------------------------------------------------------
(def outlook-extract
" Outlook extractor function
|------------+-------|
| Column | Index |
|------------+-------|
| First Name | 1 |
| Last Name | 3 |
| Email | 5 |
| Company | 29 |
| Job Title | 31 |
|------------+-------|
"
(juxt
#(nth % 1)
#(nth % 3)
#(nth % 5)
#(nth % 29)
#(nth % 31)))
(defmacro get-extractor
"Return the extractor function based on the type of file determined by looking at the header
using the is-yahoo and is-outlook functions"
[header]
`(if (is-yahoo ~header)
yahoo-extract
outlook-extract))
(defn parser [fn row]
"Using the extractor function return a map created by parsing the row into a vector of values"
(let [vec (first (csv/parse-csv row))]
(let [[firstname lastname email company title] (fn vec)]
{:firstname firstname :lastname lastname :email email :company company :title title})))
(defn first-last-email
"Return first,last,email from map"
[map]
(str (:firstname map) "," (:lastname map) "," (:email map)))
(defn email
"Return email from map"
[map]
(:email map))
(defn get-data [file func]
"Return list from file by parsing lines into maps and then using func to extract the desired data"
(with-open [rdr (io/reader file)]
(let [seq (line-seq rdr)
ext (get-extractor (first seq))] ;; Get the file specific extractor by passing the first line to get-extractor
(doall (map #(func (parser ext %)) (rest seq))))))
(defn get-emails [file]
"Return the emails from a given Linked export file."
(get-data file email))
(defn get-first-last-emails [file]
"Return firstname, lastname, email"
(get-data file first-last-email))
(defn get-header
"Test function to return the first line of the file"
[file]
(with-open [rdr (io/reader file)]
(let [col (first (line-seq rdr))]
col)))
(defn print-usage []
(println "linkedin-connections FILE"))
(defn -main
"Print the emails of the given file to STDIO"
[& args]
(if args
(doseq [email (get-emails (first args))]
(println email))
(print-usage)))