史荣久

史荣久的博客

他的个人主页  他的博客

Skype4Py:归档聊天记录1

史荣久  2009年08月28日 星期五 16:02 | 1560次浏览 | 2条评论

业务交流用skype,每天有20几个窗口,闪动上千条消息.
时间久了,翻历史记录就找不到某个具体问题的讨论过程了.

有个软件,不错,可以通过IMAP归档,但是要29刀,贵!!
决定自己弄一个, just for fun (饭)

开始是java版的,但开发机器只能用jdk1.4,否则发布时容易出错.
然后,看到python的,就有了今天的话题.

代码十分乱,因为不熟,在等可爱的小长虫出版后,当工具翻.

================ SkypeChatLog.py ==================

#!/usr/bin/env python
#coding=utf-8

# See https://developer.skype.com/wiki/Skype4Py/examples/s4p_chatmessages_py
#     http://skype4py.sourceforge.net/doc/html/
# (Ctrl-C and Ctrl-V) by trydofor@gmail.com

import Skype4Py
import re
import sys
import datetime
import os

skype = Skype4Py.Skype()


def SaveSkypeLog(minDatetime):
    maxDatetime = datetime.date.today()
    logdir = minDatetime.strftime('%Y-%m-%d')+'-'+ \
             maxDatetime.strftime('%Y-%m-%d')
    if not os.path.isdir(logdir):
        os.mkdir(logdir)

    print 'get log between:'+logdir
    print('*'*64)
    print 'Connecting to Skype..'

    skype.OnAttachmentStatus = OnAttach
    skype.Attach()

    for chat in skype.Chats:
        print chat.Type +'('+ str(len(chat.Messages))+') '
        filename = chat.Name
        filename = re.sub('\W','_',filename)
        f = None
        try:
            for mess in chat.Messages[::-1]:
                if minDatetime>=mess.Datetime:
                    continue

                if f == None:
                    fn = logdir+'/'+filename+'.htm'
                    if os.path.isfile(fn):
                        f = open(fn,'a+')
                    else:
                        f = open(fn,'w')
                        f.write('<meta http-equiv="content-type" \
                                 content="text/html; charset=utf-8"/>')
                        f.write('<title>')
                        f.write(TextEscape(chat.FriendlyName))
                        f.write('</title>')

                fromw = mess.FromHandle
                tlen = len(fromw)
                step = tlen/3
                rv = 0
                for x in fromw[0:step]:
                    rv = rv + ord(x)
                gv = 0
                for x in fromw[step:step*2]:
                    gv = gv + ord(x)
                bv = 0
                for x in fromw[step*2:]:
                    bv = bv + ord(x)

                f.write('<dt style="background-color:rgb('+str(rv%255) \
                    +','+str(gv%255)+','+str(bv%255)+')">')
                f.write(TextEscape(mess.FromDisplayName))
                f.write('[')
                f.write(TextEscape(fromw))
                f.write('] - ')
                f.write(mess.Datetime.strftime('%Y-%m-%d %H:%M:%S'))
                f.write('</dt>')
                f.write('<dd><pre>')
                f.write(TextEscape(mess.Body))
                f.write('</pre></dd>\n')
        finally:
            if f != None:
                f.close()


def OnAttach(status):
    print 'status: ' + skype.Convert.AttachmentStatusToText(status)
    if status == Skype4Py.apiAttachAvailable:
        skype.Attach()

    if status == Skype4Py.apiAttachSuccess:
        print('*'*64)


def TextEscape(text):
    text = text.encode('utf-8')
    text = text.replace('&','&amp;').replace('<','&lt;').replace('>','&gt;')
    return text


def ShowHelp():
    print '''
    输入一个日期参数,格式为 yyyy-mm-dd
    用来获得该日期(包括)以后的log.
    默认值为今天.
    '''

if __name__ == '__main__':
    if len(sys.argv) == 1:
        mdt = datetime.date.today()
        SaveSkypeLog(datetime.datetime(mdt.year,mdt.month,mdt.day,0,0,0,0))
    elif len(sys.argv) == 2:
        mdt = datetime.datetime.strptime(sys.argv[1],'%Y-%m-%d')
        SaveSkypeLog(datetime.datetime(mdt.year,mdt.month,mdt.day,0,0,0,0))
    else:
        ShowHelp()

评论

我的评论:

发表评论

请 登录 后发表评论。还没有在Zeuux哲思注册吗?现在 注册 !
夏清然

回复 夏清然  2009年08月28日 星期五 18:14

自己动手,丰衣足食~

0条回复

Eric

回复 Eric  2009年08月28日 星期五 17:44

这个...

0条回复

暂时没有评论

Zeuux © 2024

京ICP备05028076号