| 1 | #! /usr/bin/env python |
|---|
| 2 | # Converts from gajim's log db to gaim format and pops it into gaim with |
|---|
| 3 | # USER_NAME as the user id. |
|---|
| 4 | import os |
|---|
| 5 | import sys |
|---|
| 6 | import time |
|---|
| 7 | import datetime |
|---|
| 8 | |
|---|
| 9 | SECONDS_IN_A_DAY=86400 # 60*60*24 |
|---|
| 10 | LOG_PATH = os.path.expanduser("~/.gajim/logs.db") |
|---|
| 11 | GAIM_LOG_DIR = os.path.expanduser("~/.gaim/logs/") |
|---|
| 12 | USER_NAME = "whatah" |
|---|
| 13 | |
|---|
| 14 | ( |
|---|
| 15 | KIND_STATUS, |
|---|
| 16 | KIND_GCSTATUS, |
|---|
| 17 | KIND_GC_MSG, |
|---|
| 18 | KIND_SINGLE_MSG_RECV, |
|---|
| 19 | KIND_CHAT_MSG_RECV, |
|---|
| 20 | KIND_SINGLE_MSG_SENT, |
|---|
| 21 | KIND_CHAT_MSG_SENT, |
|---|
| 22 | KIND_ERROR |
|---|
| 23 | ) = range(8) |
|---|
| 24 | |
|---|
| 25 | try: |
|---|
| 26 | import sqlite3 as sqlite # python 2.5 |
|---|
| 27 | except ImportError: |
|---|
| 28 | try: |
|---|
| 29 | from pysqlite2 import dbapi2 as sqlite |
|---|
| 30 | except ImportError: |
|---|
| 31 | raise exceptions.PysqliteNotAvailable |
|---|
| 32 | |
|---|
| 33 | # Get the day based on seconds. |
|---|
| 34 | def getDayInSeconds(seconds): |
|---|
| 35 | return time.mktime(datetime.date.fromtimestamp(seconds).timetuple()) |
|---|
| 36 | |
|---|
| 37 | def getSecondsFromDay(day): |
|---|
| 38 | return time.mktime(day.timetuple()) |
|---|
| 39 | def getDay(seconds): |
|---|
| 40 | return datetime.date.fromtimestamp(seconds) |
|---|
| 41 | |
|---|
| 42 | def getDayTime(seconds): |
|---|
| 43 | return datetime.datetime.fromtimestamp(seconds) |
|---|
| 44 | |
|---|
| 45 | def getTime(seconds): |
|---|
| 46 | return datetime.datetime.fromtimestamp(seconds).strftime("(%I:%M:%S %p)") |
|---|
| 47 | |
|---|
| 48 | def generateLogs(earliest=None): |
|---|
| 49 | dbcon = sqlite.connect(LOG_PATH) |
|---|
| 50 | cursor = dbcon.cursor() |
|---|
| 51 | jid_to_id = {} |
|---|
| 52 | id_to_jid = {} |
|---|
| 53 | |
|---|
| 54 | # Get all JIDs |
|---|
| 55 | cursor.execute("SELECT jid, jid_id FROM jids") |
|---|
| 56 | rows = cursor.fetchall() |
|---|
| 57 | for row in rows: |
|---|
| 58 | jid = row[0].encode("utf-8") |
|---|
| 59 | id = str(row[1]).encode("utf-8") |
|---|
| 60 | jid_to_id[jid] = id |
|---|
| 61 | id_to_jid[id] = jid |
|---|
| 62 | |
|---|
| 63 | # Build our log directory hierarchy |
|---|
| 64 | try: |
|---|
| 65 | os.makedirs("%s/gajim/%s"%(GAIM_LOG_DIR, USER_NAME)) |
|---|
| 66 | except OSError, e: |
|---|
| 67 | pass |
|---|
| 68 | |
|---|
| 69 | # Now we go through each JID and build the conversation for them, day by day. |
|---|
| 70 | if not earliest: |
|---|
| 71 | cursor.execute("SELECT time FROM logs ORDER BY time") |
|---|
| 72 | rows = cursor.fetchone() |
|---|
| 73 | earliest = rows[0] |
|---|
| 74 | |
|---|
| 75 | start_time = getDayInSeconds(earliest) |
|---|
| 76 | end_time = start_time + SECONDS_IN_A_DAY - 1 |
|---|
| 77 | today = getDayInSeconds(time.time()) |
|---|
| 78 | while start_time <= today: |
|---|
| 79 | # Find all conversations that happened on this day. |
|---|
| 80 | day = getDay(start_time) |
|---|
| 81 | for jid in jid_to_id: |
|---|
| 82 | id = jid_to_id[jid] |
|---|
| 83 | cmd = "SELECT message, time, kind, contact_name FROM logs WHERE jid_id = %s AND time BETWEEN %s AND %s ORDER BY time" % (id, start_time, end_time) |
|---|
| 84 | cursor.execute(cmd) |
|---|
| 85 | rows = cursor.fetchall() |
|---|
| 86 | if not rows: |
|---|
| 87 | continue |
|---|
| 88 | # Create the directory for this user, so we don't clutter. |
|---|
| 89 | try: |
|---|
| 90 | os.makedirs("%s/gajim/%s/%s"%(GAIM_LOG_DIR, USER_NAME, jid)) |
|---|
| 91 | except OSError, e: |
|---|
| 92 | pass |
|---|
| 93 | # Should the conversation started business be implemented? |
|---|
| 94 | # Conversation with apparentlynew at Mon 04 Dec 2006 06:16:13 PM EST on purjenikartofi (aim) |
|---|
| 95 | earliest = rows[0][1] # earliest time. |
|---|
| 96 | timestamp = getDayTime(earliest).strftime("%H%M%S") |
|---|
| 97 | filename = "%s/gajim/%s/%s/%s.%s.txt" % (GAIM_LOG_DIR, USER_NAME, jid, day, timestamp) |
|---|
| 98 | log_file = open(filename, "w") |
|---|
| 99 | print filename |
|---|
| 100 | for row in rows: |
|---|
| 101 | message, msg_time, kind, contact_name = row |
|---|
| 102 | msg_time = getTime(msg_time) |
|---|
| 103 | if contact_name: |
|---|
| 104 | sender = unicode(contact_name.encode("utf-8"), errors='ignore') |
|---|
| 105 | elif kind in (KIND_SINGLE_MSG_RECV, KIND_CHAT_MSG_RECV, KIND_GC_MSG): |
|---|
| 106 | sender = unicode(jid, errors='ignore') |
|---|
| 107 | elif kind in (KIND_SINGLE_MSG_SENT, KIND_CHAT_MSG_SENT): |
|---|
| 108 | sender = USER_NAME |
|---|
| 109 | message = unicode(message.encode("utf-8"), errors='ignore') |
|---|
| 110 | print >> log_file, "%s %s: %s" % (msg_time, sender, message) |
|---|
| 111 | # print "%s %s: %s" % (msg_time, sender, message) |
|---|
| 112 | |
|---|
| 113 | start_time += SECONDS_IN_A_DAY |
|---|
| 114 | end_time += SECONDS_IN_A_DAY |
|---|
| 115 | day = getDay(today) |
|---|
| 116 | # Record the day |
|---|
| 117 | date_file = open(os.path.expanduser("~/.gajim/gajim2gaim.txt"), "w") |
|---|
| 118 | print >> date_file, day |
|---|
| 119 | |
|---|
| 120 | import optparse |
|---|
| 121 | if __name__ == "__main__": |
|---|
| 122 | parser = optparse.OptionParser() |
|---|
| 123 | parser.add_option("-d", "--day", dest="day", |
|---|
| 124 | action="store", type="str", help="generate logs from DAY to \ |
|---|
| 125 | today days ago to today (DAY is in YYYY-MM-DD format", metavar="DAY") |
|---|
| 126 | parser.add_option("-n", "--numdays", dest="days", |
|---|
| 127 | action="store", type="int", help="generate logs from n days ago to today", metavar="DAYS") |
|---|
| 128 | options, args = parser.parse_args(sys.argv) |
|---|
| 129 | if options.day: |
|---|
| 130 | opts = options.day.split('-') |
|---|
| 131 | if len(opts) != 3: |
|---|
| 132 | print "Invalid date format (%s)" % (options.day) |
|---|
| 133 | else: |
|---|
| 134 | opts = map(int, opts) |
|---|
| 135 | seconds = getSecondsFromDay(datetime.date(opts[0], opts[1], opts[2])) |
|---|
| 136 | generateLogs(seconds) |
|---|
| 137 | elif options.days or options.days == 0: |
|---|
| 138 | generateLogs(time.time() - options.days*SECONDS_IN_A_DAY) |
|---|
| 139 | else: |
|---|
| 140 | generateLogs() |
|---|
| 141 | # vim: set expandtab shiftwidth=4 softtabstop=4 textwidth=79: |
|---|