Beispiel #1
0
async def main(loop):
    """
    The main telegram-export program. Goes through the
    configured dialogs and dumps them into the database.
    """
    args = parse_args()
    config = load_config(args.config_file)
    dumper = Dumper(config['Dumper'])

    if args.contexts:
        dumper.config['Whitelist'] = args.contexts

    if args.format:
        formatter = NAME_TO_FORMATTER[args.format](dumper.conn)
        fmt_contexts = args.format_contexts or formatter.iter_context_ids()
        for cid in fmt_contexts:
            formatter.format(cid, config['Dumper']['OutputDirectory'])
        return

    proxy = args.proxy_string or dumper.config.get('Proxy')
    if proxy:
        proxy = parse_proxy_str(proxy)

    absolute_session_name = os.path.join(
        config['Dumper']['OutputDirectory'],
        config['TelegramAPI']['SessionName']
    )
    client = await (TelegramClient(
        absolute_session_name,
        config['TelegramAPI']['ApiId'],
        config['TelegramAPI']['ApiHash'],
        loop=loop,
        proxy=proxy
    ).start(config['TelegramAPI']['PhoneNumber']))

    if args.list_dialogs or args.search_string:
        return await list_or_search_dialogs(args, client)

    exporter = Exporter(client, config, dumper, loop)

    try:
        if args.download_past_media:
            await exporter.download_past_media()
        else:
            await exporter.start()
    except asyncio.CancelledError:
        # This should be triggered on KeyboardInterrupt's to prevent ugly
        # traceback from reaching the user. Important code that always
        # must run (such as the Downloader saving resume info) should go
        # in their respective `finally:` blocks to ensure it gets called.
        pass
    finally:
        exporter.close()

    exporter.logger.info("Finished!")
Beispiel #2
0
async def main(loop=None, **kwargs):
    """
    The main telegram-export program. Goes through the
    configured dialogs and dumps them into the database.
    """
    global exporter
    config = load_config(kwargs.get('config_file'))
    dumper = Dumper(config['Dumper'])

    proxy = kwargs.get('proxy') or dumper.config.get('Proxy')
    if proxy:
        proxy = parse_proxy_str(proxy)

    absolute_session_name = os.path.join(config['Dumper']['OutputDirectory'], config['TelegramAPI']['SessionName'])
    client = await (TelegramClient(
        absolute_session_name,
        config['TelegramAPI']['ApiId'],
        config['TelegramAPI']['ApiHash'],
        loop=loop,
        proxy=proxy
    ).start(
        config['TelegramAPI']['PhoneNumber'],
        password=config['TelegramAPI']['SecondFactorPassword'] if config.has_option(
            'TelegramAPI', 'SecondFactorPassword') else None))
    exporter = Exporter(client, config, dumper, loop)

    main_logger.info(f'下载文件存储目录:{config["Dumper"]["OutputDirectory"]}')
    try:
        if kwargs.get('past', None):
            main_logger.info('下载过往数据')
            await exporter.download_past_media()
        else:
            main_logger.info('下载最新数据')
            await exporter.start()
    except asyncio.CancelledError:
        # This should be triggered on KeyboardInterrupt's to prevent ugly
        # traceback from reaching the user. Important code that always
        # must run (such as the Downloader saving resume info) should go
        # in their respective `finally:` blocks to ensure it gets called.
        pass
    finally:
        await exporter.close()