Example #1
0
def _get_slack_data(days):
    settings = Settings.query().get()
    try:
        api_key = settings.api_key
    except AttributeError:
        api_key = os.environ.get('SLACK_API_KEY')
        if api_key is None:
            raise Exception('please set environment variable SLACK_API_KEY')
        else:
            Settings(api_key=api_key).put()

    sd = SlackDumper(api_key)
    ents = set(dir(User))
    for user_data in sd.get_user_list():
        user_id = user_data['id']
        user_data = {k: v for k, v in user_data.items() if k in ents}
        user = User.query(User.id == user_id).get()
        if user is None:
            User(**user_data).put()
        else:
            for k, v in user_data.items():
                setattr(user, k, v)
            user.put()

    ents = set(dir(Channel))
    for channel_data in sd.get_channel_list():
        channel_id = channel_data['id']
        channel_data = {k: v for k, v in channel_data.items() if k in ents}
        channel = Channel.query(Channel.id == channel_id).get()
        if channel is None:
            Channel(**channel_data).put()
        else:
            for k, v in channel_data.items():
                setattr(channel, k, v)
            channel.put()

    ents = set(dir(Message))
    for channel_id, messages in sd.get_channels_histoey(days=days).items():
        for message in messages:
            message['channel_id'] = channel_id
            user = message.get('user', '')
            ts_raw = message['ts']
            ts = float(ts_raw)
            message['ts'] = float(ts_raw)
            message['ts_raw'] = ts_raw

            message = {k: v for k, v in message.items() if k in ents}
            if not ROBUST_IMPORTING_MODE:
                msg = Message.query().filter(Message.channel_id == channel_id, Message.user == user, Message.ts == ts).get()
            else:
                msg = Message.query().filter(Message.channel_id == channel_id, Message.user ==
                                             user, Message.ts > ts - 1, Message.ts < ts + 1).get()
            if msg is None:
                Message(**message).put()
            else:
                for k, v in message.items():
                    setattr(msg, k, v)
                msg.put()
Example #2
0
def _import_zip_log(io_buffer):
    logger.debug('start')

    szd = SlackZipDumpedLog(zipfile.ZipFile(BytesIO(io_buffer.read())))

    ents = set(dir(User))
    for user_data in szd.users_data:
        user_id = user_data['id']
        user_data = {k: v for k, v in user_data.items() if k in ents}
        user = User.query(User.id == user_id).get()
        if user is None:
            User(**user_data).put()
        else:
            for k, v in user_data.items():
                setattr(user, k, v)
            user.put()
    logger.debug('user end')

    ents = set(dir(Channel))
    for channel_data in szd.channels_data:
        channel_id = channel_data['id']
        channel_data['created'] = int(channel_data['created'])

        channel_data = {k: v for k, v in channel_data.items() if k in ents}
        channel = Channel.query(Channel.id == channel_id).get()
        if channel is None:
            Channel(**channel_data).put()
        else:
            for k, v in channel_data.items():
                setattr(channel, k, v)
            channel.put()

    logger.debug('channel end')
    ents = set(dir(Message))
    for channel_id, messages in szd.map_message_data.items():
        for message in messages.values():
            message['channel_id'] = channel_id
            user = message.get('user', '')
            ts_raw = str(message['ts'])
            ts = float(ts_raw)
            message['ts'] = ts
            message['ts_raw'] = ts_raw

            message = {k: v for k, v in message.items() if k in ents}
            msg = Message.query().filter(Message.channel_id == channel_id,
                                         Message.user == user,
                                         Message.ts == ts).get()
            if msg is None:
                Message(**message).put()
            else:
                for k, v in message.items():
                    setattr(msg, k, v)
                msg.put()
    logger.debug('exit')