我的 Thunderbird 地址簿中有大量重复联系人。我不想与 Google 分享这些联系人,但我确实想找到一种方法来删除重复联系人。有一个重复联系人管理器插件,但它似乎已被废弃。
我可以在命令行执行此操作吗?
答案1
几年前我遇到过同样的问题,并编写了一个非常小的 Python 脚本来统一 Thunderbird 地址簿的 LDIF 导出:
- 将地址簿导出为 LDIF,例如 abook.ldif
- 跑步
cat abook.ldif | unify_ldif.py > abook_new.ldif
- 再次导入 abook_new.ldif(可能之前重命名旧地址簿)
该脚本目前匹配电子邮件地址和相同名称的重复条目,但这当然可以进行调整(在功能中find_existing_entry
)。这对您有用吗?
该程序在这里(编辑:您需要该python-ldap
包):
#!/usr/bin/env python
import sys
from ldif import LDIFParser, LDIFWriter
def find_existing_entry(ldif_entries, ldif_entry):
for dn, entry in ldif_entries.items():
if 'mail' in ldif_entry and 'mail' in entry:
for mail in ldif_entry['mail']:
if 'mail' in entry and mail in entry['mail']:
return dn
if 'cn' in ldif_entry and 'cn' in entry and ldif_entry['cn'][0] in entry['cn']:
return dn
if 'sn' in ldif_entry and 'sn' in entry and 'givenName' in ldif_entry and 'givenName' in entry and ldif_entry['sn'][0] in entry['sn'] and ldif_entry['givenName'][0] in entry['givenName']:
return dn
return ''
class MyLDIF(LDIFParser):
def __init__(self, input, output):
LDIFParser.__init__(self, input)
self.writer = LDIFWriter(output)
self.entries = {}
def merge(self, dn, entry):
if 'mail' in entry.keys():
if 'mail' in self.entries[dn].keys():
for mail in entry['mail']:
if mail not in self.entries[dn]['mail']:
self.entries[dn]['mail'].append(mail)
else:
self.entries[dn]['mail'] = entry['mail']
for key in entry.keys():
if key not in self.entries[dn].keys():
self.entries[dn][key] = entry[key]
def handle(self, dn, entry):
if dn in self.entries.keys():
self.merge(dn, entry)
else:
found_dn = find_existing_entry(self.entries, entry)
if found_dn != '':
self.merge(found_dn, entry)
else:
self.entries[dn] = entry
def output(self):
for dn, entry in self.entries.items():
self.writer.unparse(dn, entry)
parser = MyLDIF(sys.stdin, sys.stdout)
parser.parse()
parser.output()