예제 #1
0
파일: connect_core.py 프로젝트: chsqn/PyPtt
    def connect(self) -> None:
        def _wait():
            for i in range(self.config.retry_wait_time):
                log.show_value(self.config, log.level.INFO, [
                    i18n.Prepare,
                    i18n.Again,
                    i18n.Connect,
                    i18n.PTT,
                ], str(self.config.retry_wait_time - i))
                time.sleep(1)

        log.show_value(self.config, log.level.INFO, [
            i18n.connect_core,
        ], i18n.Active)

        if self.config.host == data_type.host_type.PTT1:
            telnet_host = 'ptt.cc'
            websocket_host = 'wss://ws.ptt.cc/bbs/'
            websocket_origin = 'https://term.ptt.cc'
        elif self.config.host == data_type.host_type.PTT2:
            telnet_host = 'ptt2.cc'
            websocket_host = 'wss://ws.ptt2.cc/bbs/'
            websocket_origin = 'https://term.ptt2.cc'
        elif self.config.host == data_type.host_type.LOCALHOST:
            telnet_host = 'localhost'
            websocket_host = 'wss://localhost'
            websocket_origin = 'https://term.ptt.cc'
        else:
            telnet_host = self.config.host
            websocket_host = f'wss://{self.config.host}'
            websocket_origin = 'https://term.ptt.cc'

        if self.config.connect_mode == connect_mode.TELNET:
            log.show_value(self.config, log.level.INFO, i18n.ConnectMode,
                           i18n.ConnectMode_Telnet)
        elif self.config.connect_mode == connect_mode.WEBSOCKET:
            log.show_value(self.config, log.level.INFO, i18n.ConnectMode,
                           i18n.ConnectMode_WebSocket)

        connect_success = False

        for _ in range(2):

            try:
                if self.config.connect_mode == connect_mode.TELNET:

                    self._core = telnetlib.Telnet(telnet_host,
                                                  self.config.port)

                else:

                    if not threading.current_thread() is threading.main_thread(
                    ):
                        loop = asyncio.new_event_loop()
                        asyncio.set_event_loop(loop)

                    log.show_value(self.config, log.level.DEBUG, 'USER_AGENT',
                                   websockets.http.USER_AGENT)

                    self._core = asyncio.get_event_loop().run_until_complete(
                        websockets.connect(websocket_host,
                                           origin=websocket_origin))

                connect_success = True
            except Exception as e:
                traceback.print_tb(e.__traceback__)
                print(e)
                if self.config.host == data_type.host_type.PTT1:
                    log.show_value(self.config, log.level.INFO, [
                        i18n.Connect,
                        i18n.PTT,
                    ], i18n.Fail)
                else:
                    log.show_value(self.config, log.level.INFO, [
                        i18n.Connect,
                        i18n.PTT2,
                    ], i18n.Fail)
                _wait()
                continue

            break

        if not connect_success:
            raise exceptions.ConnectError(self.config)
예제 #2
0
    def connect(self) -> None:
        def _wait():
            for i in range(self.config.retry_wait_time):
                log.show_value(
                    self.config, log.level.INFO, [
                        i18n.Prepare,
                        i18n.Again,
                        i18n.Connect,
                        i18n.PTT,
                    ],
                    str(self.config.retry_wait_time - i)
                )
                time.sleep(1)

        log.show_value(
            self.config, log.level.INFO, [
                i18n.connect_core,
            ],
            i18n.Active
        )

        if self.config.connect_mode == connect_mode.TELNET:
            log.show_value(
                self.config,
                log.level.INFO,
                i18n.ConnectMode,
                i18n.ConnectMode_Telnet
            )
        elif self.config.connect_mode == connect_mode.WEBSOCKET:
            log.show_value(
                self.config,
                log.level.INFO,
                i18n.ConnectMode,
                i18n.ConnectMode_WebSocket
            )

        connect_success = False

        global new_event_loop
        thread_id = threading.get_ident()

        for _ in range(2):

            try:
                if self.config.connect_mode == connect_mode.TELNET:

                    if self.config.host == data_type.host_type.PTT1:
                        self._core = telnetlib.Telnet('ptt.cc', self.config.port)
                    elif self.config.host == data_type.host_type.PTT2:
                        self._core = telnetlib.Telnet('ptt2.cc', self.config.port)
                    else:
                        self._core = telnetlib.Telnet('localhost', self.config.port)
                else:

                    if thread_id not in new_event_loop:
                        new_event_loop.append(thread_id)
                        try:
                            loop = asyncio.new_event_loop()
                            asyncio.set_event_loop(loop)
                        except Exception as e:
                            pass

                    if self.config.host == data_type.host_type.PTT1:
                        self._core = asyncio.get_event_loop().run_until_complete(
                            websockets.connect(
                                'wss://ws.ptt.cc/bbs/',
                                origin='https://term.ptt.cc'
                            )
                        )
                    elif self.config.host == data_type.host_type.PTT2:
                        self._core = asyncio.get_event_loop().run_until_complete(
                            websockets.connect(
                                'wss://ws.ptt2.cc/bbs',
                                origin='https://term.ptt2.cc'
                            )
                        )
                    else:
                        self._core = asyncio.get_event_loop().run_until_complete(
                            websockets.connect(
                                'wss://localhost',
                                origin='https://term.ptt.cc'
                            )
                        )

                connect_success = True
            except Exception as e:
                traceback.print_tb(e.__traceback__)
                print(e)
                if self.config.host == data_type.host_type.PTT1:
                    log.show_value(
                        self.config, log.level.INFO, [
                            i18n.Connect,
                            i18n.PTT,
                        ],
                        i18n.Fail
                    )
                else:
                    log.show_value(
                        self.config, log.level.INFO, [
                            i18n.Connect,
                            i18n.PTT2,
                        ],
                        i18n.Fail
                    )
                _wait()
                continue

            break

        if not connect_success:
            raise exceptions.ConnectError(self.config)