#!/usr/bin/env python # vim: set fileencoding=utf-8 : from csv import reader from datetime import datetime from sys import argv, stdout from xml.dom.minidom import getDOMImplementation doc = getDOMImplementation().createDocument(None, 'smses', None) smses = doc.documentElement count = 0 def convert_message(msg, doc): global count sms = doc.createElement('sms') sms.setAttribute('protocol', '0') if msg[1] == 'deliver': sms.setAttribute('address', msg[2]) sms.setAttribute('type', '1') elif msg[1] == 'submit': sms.setAttribute('address', msg[3]) sms.setAttribute('type', '2') else: raise Exception('Unknown type %s' % (msg[1],)) sms.setAttribute('date', datetime.strptime(msg[5], '%Y.%m.%d %H:%M').strftime('%s000')) sms.setAttribute('body', msg[7].replace('’',"'").decode('utf8')) sms.setAttribute('read', '1') sms.setAttribute('status', '-1') doc.documentElement.appendChild(sms) count += 1 for infile in argv[1:]: for row in reader(open(infile)): convert_message(row, doc) smses.setAttribute('count', str(count)) stdout.write(doc.toprettyxml(encoding='UTF-8'))