def list_rooms(pattern):
    """List room ids and keys."""
    rooms = matrix_client().get_rooms()
    data = [(rid, room.display_name) for rid, room in rooms.items()]
    if pattern:
        data = [(rid, name) for rid, name in data
                if re.search(pattern.strip('/'), name)]
    print(tabulate(data, headers=['Room ID', 'Display Name']))
Beispiel #2
0
def get_room_events(room_id):
    """Iterate room events, starting at the cursor."""
    room = matrix_client().get_rooms()[room_id]
    print(f"Reading events from room {room.display_name!r}…")
    yield from room.events
    batch_size = 1000  # empirically, this is the largest honored value
    prev_batch = room.prev_batch
    while True:
        res = room.client.api.get_room_messages(room.room_id,
                                                prev_batch,
                                                'b',
                                                limit=batch_size)
        events = res['chunk']
        if not events:
            break
        print(f"Read {len(events)} events...")
        yield from events
        prev_batch = res['end']
def export_archive(room_id, local_images, filename):
    if room_id and not re.match(r'!.+:matrix.org', room_id):
        from matrix_connection import matrix_client
        rooms = matrix_client().get_rooms()
        room_id = next(id for id, room in rooms.items() if room_id in room.display_name)
    if not room_id:
        room_id, *_ = MATRIX_ROOM_IDS
    fmt = Path(filename).suffix.lstrip('.')
    if fmt not in ARCHIVE_FORMATS:
        raise click.BadParameter(f"{fmt} is not in {ARCHIVE_FORMATS}")
    messages = Message.objects(room_id=room_id).order_by('timestamp')
    data = map(encode_message, messages)
    print(f"Writing {len(messages)} messages to {filename!r}")
    with open(filename, 'w') as fp:
        if fmt in ('text', 'txt', 'html'):
            if local_images:
                data = map(replace_by_local_image, data)
            template_path = f'templates/default.{fmt}.tpl'
            dump_html_archive(data, fp, template_path=template_path)
        elif fmt == 'json':
            json.dump(list(data), fp, indent=2)
        elif fmt == 'yaml':
            yaml.dump(list(data), fp, default_flow_style=None)