Ejemplo n.º 1
0
def export_chats(chats, path, format, db, messages=None, skip=True, progress=None):
    """
    Exports the specified chats from the database under path.

    @param   chats     list of chat dicts, as returned from SkypeDatabase
    @param   path      full path of directory where to save
    @param   format    export format (html|txt|xlsx|csv|filename.ext).
                       If format is filename.ext, a single file is created:
                       for single chat exports and multi chat XLSX exports
                       (for multi-file exports, filenames are named by chats).
    @param   db        SkypeDatabase instance
    @param   messages  list messages to export if a single chat
    @param   skip      whether to skip chats with no messages
    @param   progress  function called before exporting each chat, with the
                       number of messages exported so far
    @return            (list of exported filenames, number of chats exported)
    """
    files, count = [], 0
    def make_filename(chat):
        if len(format) > 4: # Filename already given in format
            filename = os.path.join(path, format)
        else:
            args = collections.defaultdict(str); args.update(chat)
            filename = "%s.%s" % (conf.ExportChatTemplate % args, format)
            filename = os.path.join(path, util.safe_filename(filename))
            filename = util.unique_path(filename)
        return filename
    main.logstatus("Exporting %s from %s %sto %s.",
                   util.plural("chat", chats), db.filename,
                   "" if len(format) > 4 else "as %s " % format.upper(),
                   format if len(format) > 4 else path)

    if format.lower().endswith(".xlsx"):
        filename = make_filename(chats[0])
        count = export_chats_xlsx(chats, filename, db, messages, skip, progress)
        files.append(filename)
    else:
        if not os.path.exists(path):
            os.makedirs(path)
        export_func = (export_chats_xlsx if format.lower().endswith("xlsx")
                       else export_chat_csv if format.lower().endswith("csv")
                       else export_chat_template)
        message_count = 0
        for chat in chats:
            if skip and not messages and not chat["message_count"]:
                main.log("Skipping exporting %s: no messages.",
                         chat["title_long_lc"])
                if progress: progress(message_count)
                continue # continue for chat in chats
            main.status("Exporting %s.", chat["title_long_lc"])
            if progress: progress(message_count)
            filename = make_filename(chat)
            msgs = messages or db.get_messages(chat)
            chatarg = [chat] if "xlsx" == format.lower() else chat
            export_func(chatarg, filename, db, msgs)
            message_count += chat["message_count"]
            files.append(filename)
        count = len(files)
    return (files, count)
Ejemplo n.º 2
0
 def callback():
     try:
         self.screenshot.SaveFile(filename, frmt)
         main.logstatus("Saved screenshot %s.", filename)
         util.start_file(filename)
     except Exception as e:
         base = "Error saving screenshot file"
         main.logstatus_flash(base + " %s.\n\n%s", filename, e)
         msg = base + "\n\n%s" % filename
         wx.MessageBox(msg, self.Title, wx.OK | wx.ICON_WARNING)
Ejemplo n.º 3
0
def export_chats_xlsx(chats,
                      filename,
                      db,
                      messages=None,
                      skip=True,
                      progress=None):
    """
    Exports the chats to a single XLSX file with chats on separate worksheets.

    @param   chats     list of chat data dicts, as returned from SkypeDatabase
    @param   filename  full path and filename of resulting file
    @param   db        SkypeDatabase instance
    @param   messages  list of messages to export if a single chat
    @param   skip      whether to skip chats with no messages
    @param   progress  function called before exporting each chat, with the
                       number of messages exported so far
    @return            number of chats exported
    """
    count, style = 0, {0: "timestamp", 2: "wrap", 3: "hidden"}

    writer = xlsx_writer(filename, autowrap=[2])
    message_count = 0
    for chat in chats:
        if skip and not messages and not chat["message_count"]:
            main.log("Skipping exporting %s: no messages.",
                     chat["title_long_lc"])
            continue  # continue for chat in chats
        main.logstatus("Exporting %s.", chat["title_long_lc"])
        if progress: progress(message_count)
        parser = skypedata.MessageParser(db, chat=chat, stats=False)
        writer.add_sheet(chat["title"])
        writer.set_header(True)
        writer.writerow(["Time", "Author", "Message", "Skype Name"],
                        {3: "boldhidden"})
        writer.set_header(False)
        msgs = messages or db.get_messages(chat, use_cache=False)
        for m in msgs:
            text = parser.parse(m, output={"format": "text"})
            try:
                text = text.decode("utf-8")
            except UnicodeError:
                pass
            values = [m["datetime"], m["from_dispname"], text, m["author"]]
            style[1] = "local" if db.id == m["author"] else "remote"
            writer.writerow(values, style)
        message_count += chat["message_count"]
        count += 1
    writer.close()
    return count
Ejemplo n.º 4
0
def export_chats_xlsx(chats, filename, db, messages=None, skip=True, progress=None):
    """
    Exports the chats to a single XLSX file with chats on separate worksheets.

    @param   chats     list of chat data dicts, as returned from SkypeDatabase
    @param   filename  full path and filename of resulting file
    @param   db        SkypeDatabase instance
    @param   messages  list of messages to export if a single chat
    @param   skip      whether to skip chats with no messages
    @param   progress  function called before exporting each chat, with the
                       number of messages exported so far
    @return            number of chats exported
    """
    count, style = 0, {0: "timestamp", 2: "wrap", 3: "hidden"}

    writer = xlsx_writer(filename, autowrap=[2])
    message_count = 0
    for chat in chats:
        if skip and not messages and not chat["message_count"]:
            main.log("Skipping exporting %s: no messages.",
                     chat["title_long_lc"])
            continue # continue for chat in chats
        main.logstatus("Exporting %s.", chat["title_long_lc"])
        if progress: progress(message_count)
        parser = skypedata.MessageParser(db, chat=chat, stats=False)
        writer.add_sheet(chat["title"])
        writer.set_header(True)
        writer.writerow(["Time", "Author", "Message", "Skype Name"],
                        {3: "boldhidden"})
        writer.set_header(False)
        msgs = messages or db.get_messages(chat, use_cache=False)
        for m in msgs:
            text = parser.parse(m, output={"format": "text"})
            try:
                text = text.decode("utf-8")
            except UnicodeError: pass
            values = [m["datetime"], m["from_dispname"], text, m["author"]]
            style[1] = "local" if db.id == m["author"] else "remote"
            writer.writerow(values, style)
        message_count += chat["message_count"]
        count += 1
    writer.close()
    return count
Ejemplo n.º 5
0
def export_chats(chats,
                 path,
                 format,
                 db,
                 messages=None,
                 skip=True,
                 progress=None):
    """
    Exports the specified chats from the database under path.

    @param   chats     list of chat dicts, as returned from SkypeDatabase
    @param   path      full path of directory where to save
    @param   format    export format (html|txt|xlsx|csv|filename.ext).
                       If format is filename.ext, a single file is created:
                       for single chat exports and multi chat XLSX exports
                       (for multi-file exports, filenames are named by chats).
    @param   db        SkypeDatabase instance
    @param   messages  list messages to export if a single chat
    @param   skip      whether to skip chats with no messages
    @param   progress  function called before exporting each chat, with the
                       number of messages exported so far
    @return            (list of exported filenames, number of chats exported)
    """
    files, count = [], 0

    def make_filename(chat):
        if len(format) > 4:  # Filename already given in format
            filename = os.path.join(path, format)
        else:
            args = collections.defaultdict(str)
            args.update(chat)
            filename = "%s.%s" % (conf.ExportChatTemplate % args, format)
            filename = os.path.join(path, util.safe_filename(filename))
            filename = util.unique_path(filename)
        return filename

    main.logstatus("Exporting %s from %s %sto %s.", util.plural("chat", chats),
                   db.filename,
                   "" if len(format) > 4 else "as %s " % format.upper(),
                   format if len(format) > 4 else path)

    if format.lower().endswith(".xlsx"):
        filename = make_filename(chats[0])
        count = export_chats_xlsx(chats, filename, db, messages, skip,
                                  progress)
        files.append(filename)
    else:
        if not os.path.exists(path):
            os.makedirs(path)
        export_func = (export_chats_xlsx if format.lower().endswith("xlsx")
                       else export_chat_csv if format.lower().endswith("csv")
                       else export_chat_template)
        message_count = 0
        for chat in chats:
            if skip and not messages and not chat["message_count"]:
                main.log("Skipping exporting %s: no messages.",
                         chat["title_long_lc"])
                if progress: progress(message_count)
                continue  # continue for chat in chats
            main.status("Exporting %s.", chat["title_long_lc"])
            if progress: progress(message_count)
            filename = make_filename(chat)
            msgs = messages or db.get_messages(chat)
            chatarg = [chat] if "xlsx" == format.lower() else chat
            export_func(chatarg, filename, db, msgs)
            message_count += chat["message_count"]
            files.append(filename)
        count = len(files)
    return (files, count)