matrix-room-exporter/main.py

61 lines
1.8 KiB
Python
Raw Normal View History

2024-04-03 22:57:54 -06:00
import argparse
import asyncio
import logging
import os
from pathlib import Path
import yaml
from nio import InviteMemberEvent, RoomMessageText
from exporter.callbacks import MatrixBotCallbacks
from exporter.config import DEFAULT_CONFIG
from exporter.matrix import MatrixClientHelper
SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
async def main(args):
logging.basicConfig()
2024-04-03 23:16:30 -06:00
logger = logging.getLogger('ExportBot')
2024-04-03 22:57:54 -06:00
with open(args.config) as stream:
try:
yaml_config = yaml.safe_load(stream)
except yaml.YAMLError as exc:
logger.critical(f'Failed to load config: {exc}')
quit(1)
config = DEFAULT_CONFIG | yaml_config
2024-04-03 23:07:42 -06:00
logger.setLevel(logging.DEBUG if args.debug else logging.INFO)
logger.debug('Debug logging enabled!')
2024-04-03 22:57:54 -06:00
matrix_helper = MatrixClientHelper(
user_id=config['auth']['username'],
passwd=config['auth']['password'],
homeserver=config['auth']['homeserver'],
store_path=config['store_path'],
)
client = matrix_helper.client
login_success, login_response = await matrix_helper.login()
if not login_success:
2024-04-03 23:07:42 -06:00
logger.critical(f'Failed to login: {login_response}')
2024-04-03 22:57:54 -06:00
quit(1)
logger.info(f'Logged in as {client.user_id}')
callbacks = MatrixBotCallbacks(client, config)
client.add_event_callback(callbacks.handle_invite, InviteMemberEvent)
client.add_event_callback(callbacks.handle_message, RoomMessageText)
await client.sync_forever(timeout=30000)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Export a Matrix room.')
parser.add_argument('--config', default=Path(SCRIPT_PATH) / 'config.yml')
2024-04-03 23:07:42 -06:00
parser.add_argument('--debug', '-d', action='store_true', help='Enable debug logging.')
2024-04-03 22:57:54 -06:00
args = parser.parse_args()
asyncio.run(main(args))