Exemplo n.º 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!")
Exemplo n.º 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()
Exemplo n.º 3
0
    def test_parse_proxy_str(self):
        host = "127.0.0.1"
        port = 1080
        
        proxy = (socks.SOCKS5, host, port)
        proxy_str = "socks5://127.0.0.1:1080"
        self.assertEqual(parse_proxy_str(proxy_str), proxy)

        proxy_str = "http://127.0.0.1:1080"
        proxy = (socks.HTTP, host, port)
        self.assertEqual(parse_proxy_str(proxy_str), proxy)

        proxy_str = "socks4://login:[email protected]:1080"
        proxy = (socks.SOCKS4, host, port, True, "login", "password")
        self.assertEqual(parse_proxy_str(proxy_str), proxy)

        proxy_str = "bad_type://login:[email protected]:1080"
        with self.assertRaises(ValueError):
            parse_proxy_str(proxy_str)

        proxy_str = "bad_type://127.0.0.1"
        with self.assertRaises(ValueError):
            parse_proxy_str(proxy_str)

        proxy_str = "bad_type:127.0.0.1"
        with self.assertRaises(ValueError):
            parse_proxy_str(proxy_str)

        proxy_str = "127.0.0.1:1080"
        with self.assertRaises(ValueError):
            parse_proxy_str(proxy_str)