Exemple #1
0
    def __init__(self,
                 session_name: Union[str, Storage],
                 api_id: Union[int, str] = None,
                 api_hash: str = None,
                 app_version: str = None,
                 device_model: str = None,
                 system_version: str = None,
                 lang_code: str = None,
                 ipv6: bool = False,
                 proxy: dict = None,
                 test_mode: bool = False,
                 bot_token: str = None,
                 phone_number: str = None,
                 phone_code: str = None,
                 password: str = None,
                 force_sms: bool = False,
                 workers: int = Scaffold.WORKERS,
                 workdir: str = Scaffold.WORKDIR,
                 config_file: str = Scaffold.CONFIG_FILE,
                 plugins: dict = None,
                 parse_mode: str = Scaffold.PARSE_MODES[0],
                 no_updates: bool = None,
                 takeout: bool = None,
                 sleep_threshold: int = Session.SLEEP_THRESHOLD,
                 hide_password: bool = False):
        super().__init__()

        self.session_name = session_name
        self.api_id = int(api_id) if api_id else None
        self.api_hash = api_hash
        self.app_version = app_version
        self.device_model = device_model
        self.system_version = system_version
        self.lang_code = lang_code
        self.ipv6 = ipv6
        # TODO: Make code consistent, use underscore for private/protected fields
        self._proxy = proxy
        self.test_mode = test_mode
        self.bot_token = bot_token
        self.phone_number = phone_number
        self.phone_code = phone_code
        self.password = password
        self.force_sms = force_sms
        self.workers = workers
        self.workdir = Path(workdir)
        self.config_file = Path(config_file)
        self.plugins = plugins
        self.parse_mode = parse_mode
        self.no_updates = no_updates
        self.takeout = takeout
        self.sleep_threshold = sleep_threshold
        self.hide_password = hide_password
        self.downloading_batch_size = int(os.environ.get('DOWNLOADING_BATCH_SIZE')) \
            if os.environ.get('DOWNLOADING_BATCH_SIZE') else 1024 * 1024

        print('Downloading batch size ' + str(self.downloading_batch_size))
        self.downloading_sleep_on_each_batches = int(os.environ.get('DOWNLOADING_SLEEP_EACH_BATCHES')) \
            if os.environ.get('DOWNLOADING_SLEEP_EACH_BATCHES') else None

        self.downloading_sleep_time_after_batches = float(
            os.environ.get('DOWNLOADING_SLEEP_TIME_AFTER_BATCHES', '10'))

        self.after_batches_downloading_sleep_logging = os.environ.get(
            'AFTER_BATCHES_DOWNLOADING_SLEEP_LOGGING', 'False') == 'True'

        if self.downloading_sleep_on_each_batches:
            print('Sleep ' + str(self.downloading_sleep_time_after_batches) +
                  ' after download ' +
                  str(self.downloading_sleep_on_each_batches) +
                  ' logging enabled ' +
                  str(self.after_batches_downloading_sleep_logging))

        self.executor = ThreadPoolExecutor(self.workers,
                                           thread_name_prefix="Handler")

        if isinstance(session_name, str):
            if session_name == ":memory:" or len(
                    session_name) >= MemoryStorage.SESSION_STRING_SIZE:
                session_name = re.sub(r"[\n\s]+", "", session_name)
                self.storage = MemoryStorage(session_name)
            else:
                self.storage = FileStorage(session_name, self.workdir)
        elif isinstance(session_name, Storage):
            self.storage = session_name
        else:
            raise ValueError("Unknown storage engine")

        self.dispatcher = Dispatcher(self)
        self.loop = asyncio.get_event_loop()
Exemple #2
0
    def __init__(self,
                 session_name: Union[str, Storage],
                 api_id: Union[int, str] = None,
                 api_hash: str = None,
                 app_version: str = None,
                 device_model: str = None,
                 system_version: str = None,
                 lang_code: str = None,
                 ipv6: bool = False,
                 proxy: dict = None,
                 test_mode: bool = False,
                 bot_token: str = None,
                 phone_number: str = None,
                 phone_code: str = None,
                 password: str = None,
                 force_sms: bool = False,
                 workers: int = Scaffold.WORKERS,
                 workdir: str = Scaffold.WORKDIR,
                 config_file: str = Scaffold.CONFIG_FILE,
                 plugins: dict = None,
                 parse_mode: str = Scaffold.PARSE_MODES[0],
                 no_updates: bool = None,
                 takeout: bool = None,
                 sleep_threshold: int = Session.SLEEP_THRESHOLD,
                 hide_password: bool = False):
        super().__init__()

        self.session_name = session_name
        self.api_id = int(api_id) if api_id else None
        self.api_hash = api_hash
        self.app_version = app_version
        self.device_model = device_model
        self.system_version = system_version
        self.lang_code = lang_code
        self.ipv6 = ipv6
        # TODO: Make code consistent, use underscore for private/protected fields
        self._proxy = proxy
        self.test_mode = test_mode
        self.bot_token = bot_token
        self.phone_number = phone_number
        self.phone_code = phone_code
        self.password = password
        self.force_sms = force_sms
        self.workers = workers
        self.workdir = Path(workdir)
        self.config_file = Path(config_file)
        self.plugins = plugins
        self.parse_mode = parse_mode
        self.no_updates = no_updates
        self.takeout = takeout
        self.sleep_threshold = sleep_threshold
        self.hide_password = hide_password

        self.executor = ThreadPoolExecutor(self.workers,
                                           thread_name_prefix="Handler")

        if isinstance(session_name, str):
            if session_name == ":memory:" or len(
                    session_name) >= MemoryStorage.SESSION_STRING_SIZE:
                session_name = re.sub(r"[\n\s]+", "", session_name)
                self.storage = MemoryStorage(session_name)
            else:
                self.storage = FileStorage(session_name, self.workdir)
        elif isinstance(session_name, Storage):
            self.storage = session_name
        else:
            raise ValueError("Unknown storage engine")

        self.dispatcher = Dispatcher(self)
        self.loop = asyncio.get_event_loop()
Exemple #3
0
    def __init__(self,
                 name: str,
                 api_id: Union[int, str] = None,
                 api_hash: str = None,
                 app_version: str = APP_VERSION,
                 device_model: str = DEVICE_MODEL,
                 system_version: str = SYSTEM_VERSION,
                 lang_code: str = LANG_CODE,
                 ipv6: bool = False,
                 proxy: dict = None,
                 test_mode: bool = False,
                 bot_token: str = None,
                 session_string: str = None,
                 in_memory: bool = None,
                 phone_number: str = None,
                 phone_code: str = None,
                 password: str = None,
                 workers: int = WORKERS,
                 workdir: str = WORKDIR,
                 plugins: dict = None,
                 parse_mode: "enums.ParseMode" = enums.ParseMode.DEFAULT,
                 no_updates: bool = None,
                 takeout: bool = None,
                 sleep_threshold: int = Session.SLEEP_THRESHOLD,
                 hide_password: bool = False):
        super().__init__()

        self.name = name
        self.api_id = int(api_id) if api_id else None
        self.api_hash = api_hash
        self.app_version = app_version
        self.device_model = device_model
        self.system_version = system_version
        self.lang_code = lang_code
        self.ipv6 = ipv6
        self.proxy = proxy
        self.test_mode = test_mode
        self.bot_token = bot_token
        self.session_string = session_string
        self.in_memory = in_memory
        self.phone_number = phone_number
        self.phone_code = phone_code
        self.password = password
        self.workers = workers
        self.workdir = Path(workdir)
        self.plugins = plugins
        self.parse_mode = parse_mode
        self.no_updates = no_updates
        self.takeout = takeout
        self.sleep_threshold = sleep_threshold
        self.hide_password = hide_password

        self.executor = ThreadPoolExecutor(self.workers,
                                           thread_name_prefix="Handler")

        if self.session_string:
            self.storage = MemoryStorage(self.name, self.session_string)
        elif self.in_memory:
            self.storage = MemoryStorage(self.name)
        else:
            self.storage = FileStorage(self.name, self.workdir)

        self.dispatcher = Dispatcher(self)

        self.rnd_id = MsgId

        self.parser = Parser(self)

        self.session = None

        self.media_sessions = {}
        self.media_sessions_lock = asyncio.Lock()

        self.is_connected = None
        self.is_initialized = None

        self.takeout_id = None

        self.disconnect_handler = None

        # Username used for mentioned bot commands, e.g.: /start@usernamebot
        self.username = None

        self.message_cache = Cache(10000)

        self.loop = asyncio.get_event_loop()