我正在尝试使用vobject库生成vcard,但我遇到了几个问题。
首先,我尝试使用url和base64数据添加照片,但我无法将照片显示在OS X联系人应用程序(Mavericks)或运行Android 4.4.2的Galaxy S4上。我甚至在运行Windows Phone 8.1的Lumia 1520上试过这个,但无济于事。
代码如下:
card = vobject.vCard()
attr = card.add('n')
attr.value = vobject.vcard.Name(family=agent.last_name, given=agent.first_name)
attr = card.add('fn')
attr.value = agent.get_full_name()
attr = card.add('email')
attr.value = agent.email_address
attr.type_param = 'INTERNET'
attr = card.add('tel')
attr.value = agent.cell_number
attr.type_param = 'cell'
attr = card.add('tel')
attr.value = branch.get_telephone_number()
attr.type_param = 'work'
attr = card.add('org')
attr.value = "Moe's Co"
photo_url = "http://www.abcrealestate.co.za/resize/100/150/uploads/agents/2012/03/testagent.jpg"
f = urllib.urlopen(photo_url)
data = f.read()
f.close()
attr = card.add('photo')
attr.type_param = 'JPEG'
attr.value = photo_url
response = HttpResponse(mimetype='text/x-vcard')
response['Content-Disposition'] = 'attachment; filename="%s.vcf"' % agent.get_full_name()
response.write(card.serialize())
return response
这将生成以下文件:
BEGIN:VCARD
VERSION:3.0
EMAIL;TYPE=INTERNET:test@example.com
FN:Abad Muhammed
N:Muhammed;Abad
ORG:M;o;e;';s; ;C;o
PHOTO;TYPE=JPEG:http://www.abcrealestate.co.za/resize/100/150/uploads/agents/2012/03/testagent.jpg
TEL;TYPE=cell:012 345 6789
TEL;TYPE=work:012 345 6789
END:VCARD
上述方法将图像的URL作为照片放置。基础64实现的代码(有关详细信息,请参阅here)如下所示:
photo_url = "http://www.abcrealestate.co.za/resize/100/150/uploads/agents/2012/03/testagent.jpg"
f = urllib.urlopen(photo_url)
data = f.read()
f.close()
attr = vcard.add('photo')
attr.type_param = 'jpeg'
attr.encoding_param = 'b'
attr.value = base64.encodestring(data)
结果:
BEGIN:VCARD
VERSION:3.0
EMAIL;TYPE=INTERNET:test@example.com
FN:Abad Muhammed
N:Muhammed;Abad
ORG:M;o;e;';s; ;C;o
P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wSHgKQnArczIwYzBqV04zYlNlWkg5bFptOHZ6R1g1VmsyL0t5MTRXYVlhdGlveGpUUGZ5bkUwTU5LY3FwK2d1cGVJSVpXVm9HYVZXKzd0Kwphdmp2OXJEOXBQVDViVzY4RjZIYjZmck1kMURKRHF6U1NTSzF1Mjc1VmpaZmwzSzN6ZnhmZDIxODNQOEF0QmZFT1d6azBtNDE2U0NHCmFML1dXMzd0djlwZHkvdzE1eGVYRTNtTXpTYldiK0pmdXRYUGc4bjluVTlwVk9ySFp4N1NuN09nTnV0THQvTWE0dFZhS1RkOTNjeTEKSHFFdXhvMmpadDIzNWxhbS9OdC85bHFuY2RLK3BQbFJmdFVuL1BScUtvbDZLeTVnUDZrcW82bHA5dHFtbjNkamRRclBhWEVaaG5oYgo3cnhzTnJLZndvb3FBUDUxdjJpL2h2RjhIL2poNHk4RTI5eDlwczlGMU9XQzNrSSs5RnkwZTcvYThzaFc5eFhtMGtoMk1lN2ZMUlJRCkJQQ3lydFhiV3JONGowZGRIYTNqMGtyZHRHc1lueU9HL3ZVVVZyRURsZFcxaG5tYU9KZHErOVhORmtTR3lCU01lYS9WNktLVWZpQWYKUGNlZnNWOG5MZkkvOFMxSC9hakxONU1xN20vdkxSUlRBY1pWQzhMVks3a0xMUlJSSUNsUlJSV1FILy9aCg==
TEL;TYPE=cell:012 345 6789
TEL;TYPE=work:012 345 6789
END:VCARD
除了两张照片都没有显示外,我还注意到设置为“Moe's Co”的公司名称在输出文件中的每个字符之间都有半冒号。
有人能指出我做错了什么吗?任何建议将不胜感激。
提前致谢。
答案 0 :(得分:1)
在第二种情况下,您对Base64数据进行了双重编码。您无需在base64.encodestring
上使用data
,因为vobject代码会在设置encoding_param = 'b'
时为您执行此操作。在我查看消息来源以发现正在发生的事情之前,这一点并不明显。
至于您使用基于URL的方法的第一种情况,我还没有看到一个客户端(Ubuntu上的Evolution,Win 7上的Outlook 2010)在vCard中正常工作。您的客户正在忽略该URL,我的也是。
http://microformats.org/wiki/vcard-implementations处的vCard实施摘要提供了关于不同应用程序中的怪癖的想法,但它并不完整。
答案 1 :(得分:0)
对于公司名称,值应为数组(例如,您可以在公司内部拥有一个部门)。所以只需将公司名称括在括号中:
attr = card.add('org')
attr.value = ["Moe's Co"]
答案 2 :(得分:0)
要获取图像数据,请改用请求库。就像提到的@davidjb一样,您无需将其编码为base64。
import requests
attr.value = requests.get(photo_url).content