Added linkedin mapping table and import ability
[bbdb-csv-import] / bbdb3-csv-import.el
1 ;;; bbdb3-csv-import.el --- import csv to bbdb version 3+ -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2014 by Ian Kelling
4
5 ;; Author: Ian Kelling <ian@iankelling.org>
6 ;; Created: 1 Apr 2014
7 ;; Version: 1.0
8 ;; Keywords: csv, util, bbdb
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Importer of csv (comma separated value) text into Emacs’s bbdb database,
26 ;; version 3+. Programs such as Thunderbird and Outlook allow for exporting
27 ;; contact data as csv files.
28
29 ;;; Installation:
30 ;;
31 ;; dependencies: pcsv.el, dash.el, bbdb
32 ;; These are available via marmalade/melpa or the internet
33 ;;
34 ;; Add to init file or execute manually as this may be a one time usage:
35 ;; (load-file FILENAME-OF-THIS-FILE)
36 ;; or
37 ;; (add-to-list 'load-path DIRECTORY-CONTAINING-THIS-FILE)
38 ;; (require 'bbdb3-csv-import)
39
40 ;;; Usage:
41 ;;
42 ;; Backup or rename any existing ~/.bbdb and ~/.emacs.d/bbdb while testing that
43 ;; the import works correctly.
44 ;;
45 ;; Assign bbdb3-csv-import-mapping-table to a mapping table. Some are predefined
46 ;; below, ie. bbdb3-csv-import-thunderbird.
47 ;;
48 ;; Simply call `bbdb3-csv-import-buffer' or
49 ;; `bbdb3-csv-import-file'. Interactively they prompt for file/buffer. Use
50 ;; non-interactively for no prompts.
51 ;;
52 ;; Thunderbird csv data works out of the box. Otherwise you will need to create
53 ;; a mapping table to suit your data and assign it to
54 ;; bbdb3-csv-import-mapping-table. Note that variable's doc string and perhaps
55 ;; the test data within this project for more details. Please send any new
56 ;; mapping tables upstream so I can add it to this file for other's benefit. I,
57 ;; Ian Kelling, am willing to help with any issues including creating a mapping
58 ;; table given sample data.
59 ;;
60 ;; Tips for testing: bbdb doesn't work if you delete the bbdb database file in
61 ;; the middle of an emacs session. If you want to empty the current bbdb database,
62 ;; do M-x bbdb then .* then C-u * d on the beginning of a record.
63
64 (require 'pcsv)
65 (require 'dash)
66 (require 'bbdb-com)
67 (eval-when-compile (require 'cl))
68
69 (defconst bbdb3-csv-import-thunderbird
70 '(("firstname" "First Name")
71 ("lastname" "Last Name")
72 ("name" "Display Name")
73 ("aka" "Nickname")
74 ("mail" "Primary Email" "Secondary Email")
75 ("phone" "Work Phone" "Home Phone" "Fax Number" "Pager Number" "Mobile Number")
76 ("address"
77 ("home address" (("Home Address"
78 "Home Address 2")
79 "Home City"
80 "Home State"
81 "Home ZipCode"
82 "Home Country"))
83 ("work address" (("Work Address"
84 "Work Address 2")
85 "Work City"
86 "Work State"
87 "Work ZipCode"
88 "Work Country")))
89 ("organization" "Organization")
90 ("xfields" "Web Page 1" "Web Page 2" "Birth Year" "Birth Month"
91 "Birth Day" "Department" "Custom 1" "Custom 2" "Custom 3"
92 "Custom 4" "Notes" "Job Title"))
93 "Thunderbird csv format")
94
95 (defconst bbdb3-csv-import-linkedin
96 '(("firstname" "First Name")
97 ("lastname" "Last Name")
98 ("middlename" "Middle Name")
99 ("mail" "E-mail Address" "E-mail 2 Address" "E-mail 3 Address")
100 ("phone" "Assistant's Phone" "Business Fax" "Business Phone" "Business Phone 2" "Callback" "Car Phone" "Company Main Phone" "Home Fax" "Home Phone" "Home Phone 2" "ISDN" "Mobile Phone" "Other Fax" "Other Phone" "Pager" "Primary Phone" "Radio Phone" "TTY/TDD Phone" "Telex")
101 ("address"
102 ("business address" (("Business Street"
103 "Business Street 2"
104 "Business Street 3")
105 "Business City"
106 "Business State"
107 "Business Postal Code"
108 "Business Country"))
109 ("home address" (("Home Street"
110 "Home Street 2"
111 "Home Street 3")
112 "Home City"
113 "Home State"
114 "Home Postal Code"
115 "Home Country"))
116 ("other address" (("Other Street"
117 "Other Street 2"
118 "Other Street 3")
119 "Other City"
120 "Other State"
121 "Other Postal Code"
122 "Other Country")))
123 ("organization" "Company")
124 ("xfields" "Suffix" "Department" "Job Title" "Assistant's Name" "Birthday" "Manager's Name" "Notes" "Other Address PO Box" "Spouse" "Web Page" "Personal Web Page"))
125 "Linkedin export in the Outlook csv format.")
126
127 (defvar bbdb3-csv-import-mapping-table nil
128 "The table which maps bbdb3 fields to csv fields.
129 Use the default as an example to map non-thunderbird data.
130 Name used is firstname + lastname or name.
131 After the car, all names should map to whatever csv
132 field names are used in the first row of csv data.
133 Many fields are optional. If you aren't sure if one is,
134 best to just try it. The doc string for `bbdb-create-internal'
135 may be useful for determining which fields are required.")
136
137 ;;;###autoload
138 (defun bbdb3-csv-import-file (filename)
139 "Parse and import csv file FILENAME to bbdb3."
140 (interactive "fCSV file containg contact data: ")
141 (bbdb3-csv-import-buffer (find-file-noselect filename)))
142
143
144 ;;;###autoload
145 (defun bbdb3-csv-import-buffer (&optional buffer-or-name)
146 "Parse and import csv BUFFER-OR-NAME to bbdb3.
147 Argument is a buffer or name of a buffer.
148 Defaults to current buffer."
149 (interactive "bBuffer containing CSV contact data: ")
150 (let* ((csv-fields (pcsv-parse-buffer (get-buffer (or buffer-or-name (current-buffer)))))
151 (csv-contents (cdr csv-fields))
152 (csv-fields (car csv-fields))
153 (initial-duplicate-value bbdb-allow-duplicates)
154 csv-record)
155 ;; Easier to allow duplicates and handle them post import vs failing as
156 ;; soon as we find one.
157 (setq bbdb-allow-duplicates t)
158 (while (setq csv-record (map 'list 'cons csv-fields (pop csv-contents)))
159 (cl-flet*
160 ((rd (func list) (bbdb3-csv-import-reduce func list)) ;; just a local defalias
161 (assoc-plus (key list) (bbdb3-csv-import-assoc-plus key list)) ;; defalias
162 (rd-assoc (list) (rd (lambda (elem) (assoc-plus elem csv-record)) list))
163 (mapcar-assoc (list) (mapcar (lambda (elem) (cdr (assoc elem csv-record))) list))
164 (field-map (field) (cdr (assoc field bbdb3-csv-import-mapping-table)))
165 (map-assoc (field) (assoc-plus (car (field-map field)) csv-record)))
166
167 (let ((name (let ((first (map-assoc "firstname"))
168 (middle (map-assoc "middlename"))
169 (last (map-assoc "lastname"))
170 (name (map-assoc "name")))
171 ;; prioritize any combination of first middle last over just "name"
172 (if (or (and first last) (and first middle) (and middle last))
173 ;; purely historical note.
174 ;; it works exactly the same but I don't use (cons first last) due to a bug
175 ;; http://www.mail-archive.com/bbdb-info%40lists.sourceforge.net/msg06388.html
176 (concat (or first middle) " " (or middle last) (when (and first middle) (concat " " last) ))
177 (or name first middle last ""))))
178 (phone (rd (lambda (elem)
179 (let ((data (assoc-plus elem csv-record)))
180 (if data (vconcat (list elem data)))))
181 (field-map "phone")))
182 (xfields (rd (lambda (field)
183 (let ((value (assoc-plus field csv-record)))
184 (when value
185 (while (string-match " " field)
186 ;; turn csv field names into symbols for extra fields
187 (setq field (replace-match "" nil nil field)))
188 (cons (make-symbol (downcase field)) value))))
189 (field-map "xfields")))
190 (address (rd (lambda (x)
191 (let ((address-lines (mapcar-assoc (caadr x)))
192 (address-data (mapcar-assoc (cdadr x))))
193 ;; determine if non-nil and put together the minimum set
194 (when (or (not (-all? '(lambda (arg) (zerop (length arg))) address-data))
195 (not (-all? '(lambda (arg) (zerop (length arg))) address-lines)))
196 (when (> 2 (length address-lines))
197 (setcdr (max 2 (nthcdr (-find-last-index (lambda (x) (not (null x)))
198 address-lines)
199 address-lines)) nil))
200 (vconcat (list (car x)) (list address-lines) address-data))))
201 (cdr (assoc "address" bbdb3-csv-import-mapping-table))))
202 (mail (rd-assoc (field-map "mail")))
203 (organization (rd-assoc (field-map "organization")))
204 (affix (map-assoc "affix"))
205 (aka (rd-assoc (field-map "aka"))))
206 (bbdb-create-internal name affix aka organization mail
207 phone address xfields t))))
208 (setq bbdb-allow-duplicates initial-duplicate-value)))
209
210
211 ;;;###autoload
212 (defun bbdb3-csv-import-reduce (func list)
213 "like mapcar but don't build nil results into the resulting list"
214 (-reduce-from (lambda (acc elem)
215 (let ((funcreturn (funcall func elem)))
216 (if funcreturn
217 (cons funcreturn acc)
218 acc)))
219 nil list))
220
221 ;;;###autoload
222 (defun bbdb3-csv-import-assoc-plus (key list)
223 "Like `assoc' but turn an empty string result to nil."
224 (let ((result (cdr (assoc key list))))
225 (when (not (string= "" result))
226 result)))
227
228 (provide 'bbdb3-csv-import)
229
230 ;;; bbdb3-csv-import.el ends here
231