2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 21:07:59 +00:00

Add takeout parameter in Client

This lets the client use a takeout session instead of a normal one.
Takeout sessions are useful for exporting Telegram data. Methods invoked
inside a takeout session are less prone to throw FloodWait exceptions.
This commit is contained in:
Dan 2019-01-03 12:20:42 +01:00
parent 7c008ca4e3
commit 4f6990d735
2 changed files with 23 additions and 1 deletions

View File

@ -157,6 +157,13 @@ class Client(Methods, BaseClient):
When updates are disabled your client can't receive any new message. When updates are disabled your client can't receive any new message.
Useful for batch programs that don't need to deal with updates. Useful for batch programs that don't need to deal with updates.
Defaults to False (updates enabled and always received). Defaults to False (updates enabled and always received).
takeout (``bool``, *optional*):
Pass True to let the client use a takeout session instead of a normal one, implies no_updates.
Useful for exporting your Telegram data. Methods invoked inside a takeout session (such as get_history,
download_media, ...) are less prone to throw FloodWait exceptions.
Only available for users, bots will ignore this parameter.
Defaults to False (normal session).
""" """
def __init__(self, def __init__(self,
@ -180,7 +187,8 @@ class Client(Methods, BaseClient):
workdir: str = BaseClient.WORKDIR, workdir: str = BaseClient.WORKDIR,
config_file: str = BaseClient.CONFIG_FILE, config_file: str = BaseClient.CONFIG_FILE,
plugins_dir: str = None, plugins_dir: str = None,
no_updates: bool = None): no_updates: bool = None,
takeout: bool = None):
super().__init__() super().__init__()
self.session_name = session_name self.session_name = session_name
@ -205,6 +213,7 @@ class Client(Methods, BaseClient):
self.config_file = config_file self.config_file = config_file
self.plugins_dir = plugins_dir self.plugins_dir = plugins_dir
self.no_updates = no_updates self.no_updates = no_updates
self.takeout = takeout
self.dispatcher = Dispatcher(self, workers) self.dispatcher = Dispatcher(self, workers)
@ -261,6 +270,10 @@ class Client(Methods, BaseClient):
self.save_session() self.save_session()
if self.bot_token is None: if self.bot_token is None:
if self.takeout:
self.takeout_id = self.send(functions.account.InitTakeoutSession()).id
log.warning("Takeout session {} initiated".format(self.takeout_id))
now = time.time() now = time.time()
if abs(now - self.date) > Client.OFFLINE_SLEEP: if abs(now - self.date) > Client.OFFLINE_SLEEP:
@ -316,6 +329,10 @@ class Client(Methods, BaseClient):
if not self.is_started: if not self.is_started:
raise ConnectionError("Client is already stopped") raise ConnectionError("Client is already stopped")
if self.takeout_id:
self.send(functions.account.FinishTakeoutSession())
log.warning("Takeout session {} finished".format(self.takeout_id))
Syncer.remove(self) Syncer.remove(self)
self.dispatcher.stop() self.dispatcher.stop()
@ -954,6 +971,9 @@ class Client(Methods, BaseClient):
if self.no_updates: if self.no_updates:
data = functions.InvokeWithoutUpdates(data) data = functions.InvokeWithoutUpdates(data)
if self.takeout_id:
data = functions.InvokeWithTakeout(self.takeout_id, data)
r = self.session.send(data, retries, timeout) r = self.session.send(data, retries, timeout)
self.fetch_peers(getattr(r, "users", [])) self.fetch_peers(getattr(r, "users", []))

View File

@ -90,6 +90,8 @@ class BaseClient:
self.is_started = None self.is_started = None
self.is_idle = None self.is_idle = None
self.takeout_id = None
self.updates_queue = Queue() self.updates_queue = Queue()
self.updates_workers_list = [] self.updates_workers_list = []
self.download_queue = Queue() self.download_queue = Queue()