Skip to content

Instantly share code, notes, and snippets.

@silvasur
Created February 25, 2011 19:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save silvasur/844345 to your computer and use it in GitHub Desktop.
Save silvasur/844345 to your computer and use it in GitHub Desktop.
Convert GMails CSV contacts to Operas propertiary format.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# gmail2opera.py
# Copyright (c) 2010 Kevin Chabowski
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import time
import csv
from optparse import OptionParser
import sys
def fmt_opera_contact(contact):
mail, name = contact
return "#CONTACT\n\tNAME=%s\n\tCREATED=%d\n\tMAIL=%s\n" % (name, int(time.time()), mail)
def fmt_opera_adr(adr_list):
data = "Opera Hotlist version 2.0\nOptions: encoding = utf8, version=3\n"
if '' in adr_list:
for contact in adr_list['']:
data += '\n' + fmt_opera_contact(contact)
for dirname in adr_list:
if dirname != '':
data += '\n#FOLDER\n\tNAME=%s\n\tCREATED=%d\n\n' % (dirname, int(time.time()))
for contact in adr_list[dirname]:
data += fmt_opera_contact(contact) + '\n'
data += '-\n'
return data
def utf_8_encoder(unicode_csv_data):
for line in unicode_csv_data:
yield line.encode('utf-8')
def read_google_csv(lines):
contacts = []
gmail_data = csv.reader(utf_8_encoder(lines), delimiter=',')
fields = gmail_data.next()
ind = dict(zip(fields, range(len(fields))))
for contact in gmail_data:
mail = contact[ind['E-mail 1 - Value']]
name = contact[ind['Name']]
if name == '':
name = mail
contacts.append((mail, name))
return contacts
if __name__ == '__main__':
parser = OptionParser(usage="usage: %prog [options] INPUT", epilog="INPUT \
is the GMail CSV file.\nIf INPUT is -, standard input will be used.",
version="%prog 0.5", description="Convert a GMail CSV file to a Opera ADR file \
in order to import GMail contacts to Opera's mail module.")
parser.add_option("-o", "--output", dest="output", help="Write output to \
the OUTPUT file instead of using the standard output", metavar="OUTPUT",
default="-")
(options, args) = parser.parse_args()
# Open the file handlers
if len(args) == 0:
parser.error("INPUT must be given.")
try:
f_in = sys.stdin if args[0] == '-' else open(args[0], 'r')
except:
sys.stderr.write("Could not open file '%s'. Abort.\n" % args[0])
sys.exit(2)
try:
f_out = sys.stdout if options.output == '-' else open(options.output,
'w')
except:
sys.stderr.write("Could not open file '%s'. Abort.\n" % options.output)
sys.exit(3)
lines = f_in.read().decode('utf-16').splitlines()
adr_list={}
adr_list[''] = read_google_csv(lines)
try:
f_out.write(fmt_opera_adr(adr_list))
except:
sys.stderr.write("Could not write file. Abort.\n")
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment