Skip to content

Instantly share code, notes, and snippets.

@shirou
Created January 3, 2013 13:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirou/4443440 to your computer and use it in GitHub Desktop.
Save shirou/4443440 to your computer and use it in GitHub Desktop.
convert iTunes SMS database to Android backup format(xml).
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import sqlite3
def load(dbfile):
if not os.path.exists(dbfile):
raise Exception("file does not exists")
con = sqlite3.connect(dbfile)
messages = []
cur = con.cursor()
cur.execute(u"SELECT address, date, text, flags, subject, read FROM message")
# cur.execute(u"SELECT address, date, text, flags, subject, read FROM message limit 30")
for row in cur:
m = {}
m['address'] = row[0]
m['date'] = str(row[1]) + "000"
m['text'] = row[2]
if row[3] == 2: # received
m['type'] = "1"
elif row[3] == 3: # sent
m['type'] = "2"
else:
m['type'] = "2"
if row[4] == "None":
m['subject'] = row[4]
else:
m['subject'] = ""
m['read'] = row[5]
if row[3] == 3:
m['toa'] = "0"
else:
m['toa'] = "145"
m['sc'] = "null" # FIXME
m['status'] = "-1" # FIXME
messages.append(m)
con.close()
return messages
def write(messages):
F = ' <sms protocol="0" address="{0[address]}" date="{0[date]}" type="{0[type]}" subject="{0[subject]}" body="{0[text]
}" toa="{0[toa]}" sc_toa="0" service_center="{0[sc]}" read="{0[read]}" status="{0[status]}" />'
print("""<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>""")
print('<smses count="{0}">'.format(len(messages)))
for message in messages:
print(F.format(message))
print("</smses>")
if __name__ == '__main__':
dbfile = sys.argv[1]
messages = load(dbfile)
write(messages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment