matrix-room-exporter/main.py

67 lines
2.1 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
2024-04-04 15:04:13 -06:00
import redis
2024-04-03 22:57:54 -06:00
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)
2024-04-04 00:28:51 -06:00
# Really lazy way to do this
2024-04-04 15:06:36 -06:00
config = DEFAULT_CONFIG | yaml_config
2024-04-04 00:28:51 -06:00
config['r2'] = DEFAULT_CONFIG['r2'] | yaml_config.get('r2', {})
2024-04-04 15:04:13 -06:00
config['allowed_to_export'] = yaml_config.get('allowed_to_export', [])
2024-04-03 22:57:54 -06:00
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
2024-04-04 15:04:13 -06:00
r = redis.Redis(host='localhost', port=6379, db=3)
r.flushdb()
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}')
2024-04-04 15:04:13 -06:00
callbacks = MatrixBotCallbacks(matrix_helper, config)
2024-04-03 22:57:54 -06:00
client.add_event_callback(callbacks.handle_invite, InviteMemberEvent)
client.add_event_callback(callbacks.handle_message, RoomMessageText)
await client.sync_forever(timeout=10000)
2024-04-03 22:57:54 -06:00
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))