8b66c73668ded8d729569ae0bb1d991921485307
[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, Gmail, Linkedin, and Outlook allow
27 ;; for exporting contact data as csv files. See ASynK for syncing bbdb/google/outlook.
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 a mapping table. Predefined ones listed here:
46 ;; (setq bbdb3-csv-import-mapping-table bbdb3-csv-import-thunderbird)
47 ;; (setq bbdb3-csv-import-mapping-table bbdb3-csv-import-gmail)
48 ;; (setq bbdb3-csv-import-mapping-table bbdb3-csv-import-linkedin)
49 ;; (setq bbdb3-csv-import-mapping-table bbdb3-csv-import-outlook-web)
50 ;;
51 ;;
52 ;; Simply call `bbdb3-csv-import-buffer' or
53 ;; `bbdb3-csv-import-file'. Interactively they prompt for file/buffer. Use
54 ;; non-interactively for no prompts.
55 ;;
56 ;; If you need to define your own mapping table, it should not be too hard. Use
57 ;; the existing tables as an example, and perhaps the test data within this
58 ;; project. Please send any new mapping tables to the maintainer listed in this
59 ;; file. The maintainer should be able to help with any issues and may create a
60 ;; new mapping table given sample data.
61 ;;
62 ;; Tips for testing:
63 ;; - bbdb doesn't work if you delete the bbdb database file in
64 ;; the middle of an emacs session. If you want to empty the current bbdb database,
65 ;; do M-x bbdb then .* then C-u * d on the beginning of a record.
66 ;; - After changing a mapping table, don't forget to re-execute
67 ;; (setq bbdb3-csv-import-mapping-table ...) so that it propagates.
68 ;;
69 ;; Todo: It would be nice if we would programatically or manually merge all the
70 ;; mapping tables, then we would not have to set one.
71
72 (require 'pcsv)
73 (require 'dash)
74 (require 'bbdb-com)
75 (eval-when-compile (require 'cl))
76
77 (defconst bbdb3-csv-import-thunderbird
78 '(("firstname" "First Name")
79 ("lastname" "Last Name")
80 ("name" "Display Name")
81 ("aka" "Nickname")
82 ("mail" "Primary Email" "Secondary Email")
83 ("phone" "Work Phone" "Home Phone" "Fax Number" "Pager Number" "Mobile Number")
84 ("address"
85 (("home address"
86 (("Home Address" "Home Address 2")
87 "Home City" "Home State"
88 "Home ZipCode" "Home Country"))
89 ("work address"
90 (("Work Address" "Work Address 2")
91 "Work City" "Work State"
92 "Work ZipCode" "Work Country"))))
93 ("organization" "Organization")
94 ("xfields" "Web Page 1" "Web Page 2" "Birth Year" "Birth Month"
95 "Birth Day" "Department" "Custom 1" "Custom 2" "Custom 3"
96 "Custom 4" "Notes" "Job Title"))
97 "Thunderbird csv format")
98
99 (defconst bbdb3-csv-import-linkedin
100 '(("firstname" "First Name")
101 ("lastname" "Last Name")
102 ("middlename" "Middle Name")
103 ("mail" "E-mail Address" "E-mail 2 Address" "E-mail 3 Address")
104 ("phone"
105 "Assistant's Phone" "Business Fax" "Business Phone"
106 "Business Phone 2" "Callback" "Car Phone"
107 "Company Main Phone" "Home Fax" "Home Phone"
108 "Home Phone 2" "ISDN" "Mobile Phone"
109 "Other Fax" "Other Phone" "Pager"
110 "Primary Phone" "Radio Phone" "TTY/TDD Phone" "Telex")
111 ("address"
112 (("business address"
113 (("Business Street" "Business Street 2" "Business Street 3")
114 "Business City" "Business State"
115 "Business Postal Code" "Business Country"))
116 ("home address"
117 (("Home Street" "Home Street 2" "Home Street 3")
118 "Home City" "Home State"
119 "Home Postal Code" "Home Country"))
120 ("other address"
121 (("Other Street" "Other Street 2" "Other Street 3")
122 "Other City" "Other State"
123 "Other Postal Code" "Other Country"))))
124 ("organization" "Company")
125 ("xfields"
126 "Suffix" "Department" "Job Title" "Assistant's Name"
127 "Birthday" "Manager's Name" "Notes" "Other Address PO Box"
128 "Spouse" "Web Page" "Personal Web Page"))
129 "Linkedin export in the Outlook csv format.")
130
131
132 ;; note. PO Box and Extended Address are added as additional address street lines if they exist.
133 ;; If you don't like this, just delete them from this fiel.
134 ;; If you want some other special handling, it will need to be coded.
135 (defconst bbdb3-csv-import-gmail
136 '(("firstname" "Given Name")
137 ("lastname" "Family Name")
138 ("name" "Name")
139 ("mail" (repeat "E-mail 1 - Value"))
140 ("phone" (repeat ("Phone 1 - Type" "Phone 1 - Value")))
141 ("address"
142 (repeat (("Address 1 - Type")
143 (("Address 1 - Street" "Address 1 - PO Box" "Address 1 - Extended Address")
144 "Address 1 - City" "Address 1 - Region"
145 "Address 1 - Postal Code" "Address 1 - Country"))))
146 ("organization" (repeat "Organization 1 - Name"))
147 ("xfields"
148 "Additional Name" "Yomi Name" "Given Name Yomi"
149 "Additional Name Yomi" "Family Name Yomi" "Name Prefix"
150 "Name Suffix" "Initials" "Nickname"
151 "Short Name" "Maiden Name" "Birthday"
152 "Gender" "Location" "Billing Information"
153 "Directory Server" "Mileage" "Occupation"
154 "Hobby" "Sensitivity" "Priority"
155 "Subject" "Notes" "Group Membership"
156 ;; Gmail wouldn't let me add more than 1 organization, but no harm in
157 ;; looking for multiple since the field name implies the possibility.
158 (repeat
159 "Organization 1 - Type" "Organization 1 - Yomi Name"
160 "Organization 1 - Title" "Organization 1 - Department"
161 "Organization 1 - Symbol" "Organization 1 - Location"
162 "Organization 1 - Job Description")
163 (repeat ("Relation 1 - Type" "Relation 1 - Value"))
164 (repeat ("Website 1 - Type" "Website 1 - Value"))
165 (repeat ("Event 1 - Type" "Event 1 - Value"))
166 (repeat ("Custom Field 1 - Type" "Custom Field 1 - Value"))))
167 "Gmail csv export format")
168
169
170 (defconst bbdb3-csv-import-gmail-typed-email
171 (append (car (last bbdb3-csv-import-gmail)) '((repeat "E-mail 1 - Type")))
172 "Like the first Gmail mapping, but use custom fields to store
173 Gmail's email labels. This is separate because I assume most
174 people don't use those labels and using the default labels
175 would create useless custom fields.")
176
177 (defconst bbdb3-csv-import-outlook-typed-email
178 (append (car (last bbdb3-csv-import-outlook-web)) '((repeat "E-mail 1 - Type")))
179 "Like the previous var, but for outlook-web.
180 Adds email labels as custom fields.")
181
182
183 (defconst bbdb3-csv-import-outlook-web
184 '(("firstname" "Display Name" "First Name")
185 ("lastname" "Last Name")
186 ("middlename" "Middle Name")
187 ("mail" "E-mail Address" "E-mail 2 Address" "E-mail 3 Address")
188 ("phone"
189 "Assistant's Phone" "Business Fax" "Business Phone"
190 "Business Phone 2" "Callback" "Car Phone"
191 "Company Main Phone" "Home Fax" "Home Phone"
192 "Home Phone 2" "ISDN" "Mobile Phone"
193 "Other Fax" "Other Phone" "Pager"
194 "Primary Phone" "Radio Phone" "TTY/TDD Phone" "Telex")
195 ("address"
196 (("business address"
197 (("Business Street")
198 "Business City" "Business State"
199 "Business Postal Code" "Business Country"))
200 ("home address"
201 (("Home Street")
202 "Home City" "Home State"
203 "Home Postal Code" "Home Country"))
204 ("other address"
205 (("Other Street" "")
206 "Other City" "Other State"
207 "Other Postal Code" "Other Country"))))
208 ("organization" "Company")
209 ("xfields"
210 "Anniversary" "Family Name Yomi" "Given Name Yomi"
211 "Suffix" "Department" "Job Title" "Birthday" "Manager's Name" "Notes"
212 "Spouse" "Web Page"))
213 "Hotmail.com, outlook.com, live.com, etc.
214 Based on 'Export for outlook.com and other services',
215 not the export for Outlook 2010 and 2013.")
216
217 ;;(defconst bbdb3-csv-import-combined)
218
219
220 (defvar bbdb3-csv-import-mapping-table nil
221 "The table which maps bbdb3 fields to csv fields.
222 Use the default as an example to map non-thunderbird data.
223 Name used is firstname + lastname or name.
224 After the car, all names should map to whatever csv
225 field names are used in the first row of csv data.
226 Many fields are optional. If you aren't sure if one is,
227 best to just try it. The doc string for `bbdb-create-internal'
228 may be useful for determining which fields are required.")
229
230 ;;;###autoload
231 (defun bbdb3-csv-import-file (filename)
232 "Parse and import csv file FILENAME to bbdb3."
233 (interactive "fCSV file containg contact data: ")
234 (bbdb3-csv-import-buffer (find-file-noselect filename)))
235
236
237 ;;;###autoload
238 (defun bbdb3-csv-import-buffer (&optional buffer-or-name)
239 "Parse and import csv BUFFER-OR-NAME to bbdb3.
240 Argument is a buffer or name of a buffer.
241 Defaults to current buffer."
242 (interactive "bBuffer containing CSV contact data: ")
243 (when (null bbdb3-csv-import-mapping-table)
244 (error "error: `bbdb3-csv-import-mapping-table' is nil. Please set it and rerun."))
245 (let* ((csv-fields (pcsv-parse-buffer (get-buffer (or buffer-or-name (current-buffer)))))
246 (csv-contents (cdr csv-fields))
247 (csv-fields (car csv-fields))
248 (initial-duplicate-value bbdb-allow-duplicates)
249 csv-record rd assoc-plus flatten1)
250 ;; convenient function names
251 (fset 'rd 'bbdb3-csv-import-rd)
252 (fset 'assoc-plus 'bbdb3-csv-import-assoc-plus)
253 (fset 'flatten1 'bbdb3-csv-import-flatten1)
254 ;; Easier to allow duplicates and handle them post import vs failing as
255 ;; soon as we find one.
256 (setq bbdb-allow-duplicates t)
257 ;; loop over the csv records
258 (while (setq csv-record (map 'list 'cons csv-fields (pop csv-contents)))
259 (cl-flet*
260 ((expand-repeats (list)
261 ;; return new list where elements from LIST in form
262 ;; (repeat elem1 ...) become ((elem1 ...) [(elem2 ...)] ...)
263 ;; For as many repeating numbered fields exist in the csv fields.
264 ;; elem can be a string or a tree (a list with possibly lists inside it)
265 (--reduce-from (if (not (and (consp it) (eq (car it) 'repeat)))
266 (cons it acc)
267 (setq it (cdr it))
268 (let* ((i 1)
269 (first-field (car (flatten it))))
270 (setq acc (cons it acc))
271 ;; use first-field to test if there is another repetition.
272 (while (member (replace-num (setq i (1+ i)) first-field) csv-fields)
273 (cl-labels ((fun (cell)
274 (if (consp cell)
275 (mapcar #'fun cell)
276 (replace-num i cell))))
277 (setq acc (cons (fun it) acc))))
278 acc))
279 nil list))
280 (map-bbdb3 (root)
281 ;; ROOT = a root element from bbdb3-csv-import-mapping-table.
282 ;; Get the actual csv-fields, including variably repeated ones. flattened
283 ;; by one because repeated fields are put in sub-lists, but
284 ;; after expanding them, that extra depth is no longer
285 ;; useful. Small quirk: address mappings without 'repeat
286 ;; need to be grouped in a list because they contain sublists that we
287 ;; don't want flattened. Better this than more complex code.
288 (flatten1 (expand-repeats (cdr (assoc root bbdb3-csv-import-mapping-table)))))
289 (rd-assoc (root)
290 ;; given ROOT, return a list of data, ignoring empty fields
291 (rd (lambda (elem) (assoc-plus elem csv-record)) (map-bbdb3 root)))
292 (mapcar-assoc (list)
293 ;; given LIST of fields,return a list of data with nil in place of an empty field
294 (mapcar (lambda (elem) (cdr (assoc elem csv-record))) list))
295 (assoc-expand (e)
296 ;; E = data-field-name | (field-name-field data-field)
297 ;; get data from the csv-record and return
298 ;; (field-name data) or nil.
299 (let ((data-name (if (consp e) (cdr (assoc (car e) csv-record)) e))
300 (data (assoc-plus (if (consp e) (cadr e) e) csv-record)))
301 (if data (list data-name data))))
302 (replace-num (num string)
303 ;; in STRING, replace all groups of numbers with NUM
304 (replace-regexp-in-string "[0-9]+" (number-to-string num) string))
305 (map-assoc (field)
306 ;; For mappings with just 1 simple csv-field, get it's data
307 (car (rd-assoc field))))
308
309 (let ((name (let ((first (map-assoc "firstname"))
310 (middle (map-assoc "middlename"))
311 (last (map-assoc "lastname"))
312 (name (map-assoc "name")))
313 ;; prioritize any combination of first middle last over just "name"
314 (if (or (and first last) (and first middle) (and middle last))
315 ;; purely historical note.
316 ;; using (cons first last) as argument works the same as (concat first " " last)
317 (concat (or first middle) " " (or middle last) (when (and first middle) (concat " " last) ))
318 (or name first middle last ""))))
319 (phone (rd 'vconcat (rd #'assoc-expand (map-bbdb3 "phone"))))
320 (mail (rd-assoc "mail"))
321 (xfields (rd (lambda (list)
322 (let ((e (car list)))
323 (while (string-match "-" e)
324 (setq e (replace-match "" nil nil e)))
325 (while (string-match " +" e)
326 (setq e (replace-match "-" nil nil e)))
327 (setq e (make-symbol (downcase e)))
328 (cons e (cadr list)))) ;; change from (a b) to (a . b)
329 (rd #'assoc-expand (map-bbdb3 "xfields"))))
330 (address (rd (lambda (mapping-elem)
331 (let ((address-lines (mapcar-assoc (caadr mapping-elem)))
332 (address-data (mapcar-assoc (cdadr mapping-elem)))
333 (elem-name (car mapping-elem)))
334 ;; outlook-web has 1 address line, bbdb requires 2
335 (if (= (length address-lines) 1)
336 (setq address-lines (append address-lines '(""))))
337 (when (consp elem-name)
338 (setq elem-name (cdr (assoc (car elem-name) csv-record))))
339
340 ;; determine if non-nil and put together the minimum set
341 (when (or (not (--all? (zerop (length it)) address-data))
342 (not (--all? (zerop (length it)) address-lines)))
343 (when (> 2 (length address-lines))
344 (setcdr (max 2 (nthcdr (--find-last-index (not (null it))
345 address-lines)
346 address-lines)) nil))
347 (vconcat (list elem-name) (list address-lines) address-data))))
348 (map-bbdb3 "address")))
349 (organization (rd-assoc "organization"))
350 (affix (map-assoc "affix"))
351 (aka (rd-assoc "aka")))
352 (bbdb-create-internal name affix aka organization mail phone address xfields t))))
353 (setq bbdb-allow-duplicates initial-duplicate-value)))
354
355
356 (defun bbdb3-csv-import-flatten1 (list)
357 "flatten LIST by 1 level."
358 (--reduce-from (if (consp it)
359 (-concat acc it)
360 (-snoc acc it))
361 nil list))
362
363 ;;;###autoload
364 (defun bbdb3-csv-import-rd (func list)
365 "like mapcar but don't build nil results into the resulting list"
366 (--reduce-from (let ((funcreturn (funcall func it)))
367 (if funcreturn
368 (cons funcreturn acc)
369 acc))
370 nil list))
371
372 ;;;###autoload
373 (defun bbdb3-csv-import-assoc-plus (key list)
374 "Like `assoc' but turn an empty string result to nil."
375 (let ((result (cdr (assoc key list))))
376 (when (not (string= "" result))
377 result)))
378
379
380
381 (provide 'bbdb3-csv-import)
382
383 ;;; bbdb3-csv-import.el ends here