forked from TGExplore/TG-Files-to-Link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparalleltransfer.py
199 lines (173 loc) · 7.87 KB
/
paralleltransfer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# tgfilestream - A Telegram bot that can stream Telegram files to users over HTTP.
# Copyright (C) 2019 Tulir Asokan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Union, AsyncGenerator, AsyncContextManager, Dict, Optional, List
from contextlib import asynccontextmanager
from dataclasses import dataclass
import logging
import asyncio
import math
from telethon import TelegramClient, utils
from telethon.crypto import AuthKey
from telethon.network import MTProtoSender
from telethon.tl.functions.auth import ExportAuthorizationRequest, ImportAuthorizationRequest
from telethon.tl.functions.upload import GetFileRequest
from telethon.tl.types import (Document, InputFileLocation, InputDocumentFileLocation,
InputPhotoFileLocation, InputPeerPhotoFileLocation, DcOption)
from telethon.errors import DcIdInvalidError
from .config import connection_limit
TypeLocation = Union[Document, InputDocumentFileLocation, InputPeerPhotoFileLocation,
InputFileLocation, InputPhotoFileLocation]
root_log = logging.getLogger(__name__)
if connection_limit > 25:
root_log.warning("The connection limit should not be set above 25 to avoid"
" infinite disconnect/reconnect loops")
@dataclass
class Connection:
log: logging.Logger
sender: MTProtoSender
lock: asyncio.Lock
users: int = 0
class DCConnectionManager:
log: logging.Logger
client: TelegramClient
loop: asyncio.AbstractEventLoop
dc_id: int
dc: Optional[DcOption]
auth_key: Optional[AuthKey]
connections: List[Connection]
_list_lock: asyncio.Lock
def __init__(self, client: TelegramClient, dc_id: int) -> None:
self.log = root_log.getChild(f"dc{dc_id}")
self.client = client
self.dc_id = dc_id
self.auth_key = None
self.connections = []
self._list_lock = asyncio.Lock()
self.loop = client.loop
self.dc = None
async def _new_connection(self) -> Connection:
if not self.dc:
self.dc = await self.client._get_dc(self.dc_id)
sender = MTProtoSender(self.auth_key, self.loop, loggers=self.client._log)
index = len(self.connections) + 1
conn = Connection(sender=sender, log=self.log.getChild(f"conn{index}"), lock=asyncio.Lock())
self.connections.append(conn)
async with conn.lock:
conn.log.info("Connecting...")
connection_info = self.client._connection(self.dc.ip_address, self.dc.port, self.dc.id,
loop=self.loop, loggers=self.client._log,
proxy=self.client._proxy)
await sender.connect(connection_info)
if not self.auth_key:
await self._export_auth_key(conn)
return conn
async def _export_auth_key(self, conn: Connection) -> None:
self.log.info(f"Exporting auth to DC {self.dc.id}"
f" (main client is in {self.client.session.dc_id})")
try:
auth = await self.client(ExportAuthorizationRequest(self.dc.id))
except DcIdInvalidError:
self.log.debug("Got DcIdInvalidError")
self.auth_key = self.client.session.auth_key
conn.sender.auth_key = self.auth_key
return
req = self.client._init_with(ImportAuthorizationRequest(
id=auth.id, bytes=auth.bytes
))
await conn.sender.send(req)
self.auth_key = conn.sender.auth_key
async def _next_connection(self) -> Connection:
best_conn: Optional[Connection] = None
for conn in self.connections:
if not best_conn or conn.users < best_conn.users:
best_conn = conn
if (not best_conn or best_conn.users > 0) and len(self.connections) < connection_limit:
best_conn = await self._new_connection()
return best_conn
@asynccontextmanager
async def get_connection(self) -> AsyncContextManager[Connection]:
async with self._list_lock:
conn: Connection = await asyncio.shield(self._next_connection())
# The connection is locked so reconnections don't stack
async with conn.lock:
conn.users += 1
try:
yield conn
finally:
conn.users -= 1
class ParallelTransferrer:
log: logging.Logger = logging.getLogger(__name__)
client: TelegramClient
loop: asyncio.AbstractEventLoop
dc_managers: Dict[int, DCConnectionManager]
_counter: int
def __init__(self, client: TelegramClient) -> None:
self.client = client
self.loop = self.client.loop
self._counter = 0
self.dc_managers = {
1: DCConnectionManager(client, 1),
2: DCConnectionManager(client, 2),
3: DCConnectionManager(client, 3),
4: DCConnectionManager(client, 4),
5: DCConnectionManager(client, 5),
}
def post_init(self) -> None:
self.dc_managers[self.client.session.dc_id].auth_key = self.client.session.auth_key
@property
def next_index(self) -> int:
self._counter += 1
return self._counter
async def _int_download(self, request: GetFileRequest, first_part: int, last_part: int,
part_count: int, part_size: int, dc_id: int, first_part_cut: int,
last_part_cut: int) -> AsyncGenerator[bytes, None]:
log = self.log
try:
part = first_part
dcm = self.dc_managers[dc_id]
async with dcm.get_connection() as conn:
log = conn.log
while part <= last_part:
result = await conn.sender.send(request)
request.offset += part_size
if part == first_part:
yield result.bytes[first_part_cut:]
elif part == last_part:
yield result.bytes[:last_part_cut]
else:
yield result.bytes
log.debug(f"Part {part}/{last_part} (total {part_count}) downloaded")
part += 1
log.debug("Parallel download finished")
except (GeneratorExit, StopAsyncIteration, asyncio.CancelledError):
log.debug("Parallel download interrupted")
raise
except Exception:
log.debug("Parallel download errored", exc_info=True)
def download(self, file: TypeLocation, file_size: int, offset: int, limit: int
) -> AsyncGenerator[bytes, None]:
dc_id, location = utils.get_input_location(file)
part_size = 512 * 1024
first_part_cut = offset % part_size
first_part = math.floor(offset / part_size)
last_part_cut = part_size - (limit % part_size)
last_part = math.ceil(limit / part_size)
part_count = math.ceil(file_size / part_size)
self.log.debug(f"Starting parallel download: chunks {first_part}-{last_part}"
f" of {part_count} {location!s}")
request = GetFileRequest(location, offset=first_part * part_size, limit=part_size)
return self._int_download(request, first_part, last_part, part_count, part_size, dc_id,
first_part_cut, last_part_cut)