Agnivesh10M commited on
Commit
3cd7660
·
verified ·
1 Parent(s): 5d78b2f

Update TechVJ/TechVJ/fast_download.py

Browse files
Files changed (1) hide show
  1. TechVJ/TechVJ/fast_download.py +94 -68
TechVJ/TechVJ/fast_download.py CHANGED
@@ -5,10 +5,9 @@ import inspect
5
  import time
6
  from pyrogram import Client
7
  from pyrogram.raw.functions.upload import GetFile, SaveBigFilePart
8
- from pyrogram.raw.types import InputFileBig, InputDocumentFileLocation, InputPhotoFileLocation
9
 
10
- # टेलीग्राम रॉ API का आधिकारिक सबसे तेज चंक साइज (512 KB)
11
- CHUNK_SIZE = 512 * 1024
12
 
13
  async def _invoke_progress(progress_func, current, total, progress_args):
14
  if not progress_func:
@@ -21,75 +20,90 @@ async def _invoke_progress(progress_func, current, total, progress_args):
21
  pass
22
 
23
  async def fast_download_file(client: Client, message, out_path: str, progress_func=None, progress_args=()):
24
- media = message.document or message.video or message.audio or message.photo
25
  if not media:
26
- return None
27
 
28
  file_size = getattr(media, "file_size", 0)
29
- if hasattr(media, "thumbs") and media.thumbs and file_size == 0:
30
- file_size = media.thumbs[-1].file_size
31
-
32
  if file_size == 0:
33
  return await client.download_media(message, file_name=out_path)
34
 
35
- file_id_obj = None
36
- if message.document:
37
- doc = message.document
38
- file_id_obj = InputDocumentFileLocation(
39
- id=doc.id, access_hash=doc.access_hash, file_reference=doc.file_reference, thumb_size=""
40
- )
41
- elif message.video:
42
- vid = message.video
43
- file_id_obj = InputDocumentFileLocation(
44
- id=vid.id, access_hash=vid.access_hash, file_reference=vid.file_reference, thumb_size=""
45
- )
46
- elif message.audio:
47
- aud = message.audio
48
- file_id_obj = InputDocumentFileLocation(
49
- id=aud.id, access_hash=aud.access_hash, file_reference=aud.file_reference, thumb_size=""
50
- )
51
- else:
52
- return await client.download_media(message, file_name=out_path)
53
 
54
  total_parts = math.ceil(file_size / CHUNK_SIZE)
55
  downloaded_bytes = 0
 
 
56
 
57
- # डिस्क पर फ़ाइल प्री-एलोकेट करना ताकि पैरेलल राइटिंग में कोई I/O बॉटलनेक न आए
58
- f = open(out_path, "wb")
59
- f.truncate(file_size)
60
- f.close()
61
 
62
- # 16 पैरेलल वर्कर्स (बिना सर्वर थ्रॉटलिंग के अधिकतम बैंडविड्थ)
63
- sem = asyncio.Semaphore(16)
64
- last_update = 0
 
 
 
 
65
 
66
- async def worker_fetch(part_index):
67
  nonlocal downloaded_bytes, last_update
68
- offset = part_index * CHUNK_SIZE
69
- async with sem:
70
- for _ in range(5):
 
 
 
 
 
 
 
71
  try:
 
72
  r = await client.invoke(
73
- GetFile(location=file_id_obj, offset=offset, limit=CHUNK_SIZE)
 
74
  )
75
  chunk_bytes = r.bytes
76
- # सीधा फ़ाइल ऑफ़सेट पर राइट करें
77
- with open(out_path, "r+b") as fp:
78
- fp.seek(offset)
79
- fp.write(chunk_bytes)
 
 
 
 
 
 
 
80
 
 
81
  downloaded_bytes += len(chunk_bytes)
82
  now = time.time()
83
- if now - last_update >= 1.0 or downloaded_bytes >= file_size:
84
  last_update = now
85
  await _invoke_progress(progress_func, downloaded_bytes, file_size, progress_args)
86
- break
87
- except Exception:
88
- await asyncio.sleep(0.2)
89
 
90
- tasks = [worker_fetch(i) for i in range(total_parts)]
91
- await asyncio.gather(*tasks)
 
 
 
 
 
 
 
92
 
 
93
  return out_path
94
 
95
  async def fast_upload_file(client: Client, file_path: str, progress_func=None, progress_args=()):
@@ -97,15 +111,26 @@ async def fast_upload_file(client: Client, file_path: str, progress_func=None, p
97
  total_parts = math.ceil(file_size / CHUNK_SIZE)
98
  file_id = client.rnd_id()
99
  uploaded_bytes = 0
100
-
101
- # 16 पैरेलल अपलोड वर्कर्स
102
- sem = asyncio.Semaphore(16)
103
  last_update = 0
104
 
105
- async def worker_send(part_index, chunk):
 
 
 
 
 
 
 
 
106
  nonlocal uploaded_bytes, last_update
107
- async with sem:
108
- for _ in range(5):
 
 
 
 
 
109
  try:
110
  await client.invoke(
111
  SaveBigFilePart(
@@ -115,22 +140,23 @@ async def fast_upload_file(client: Client, file_path: str, progress_func=None, p
115
  bytes=chunk
116
  )
117
  )
118
- uploaded_bytes += len(chunk)
119
- now = time.time()
120
- if now - last_update >= 1.0 or uploaded_bytes >= file_size:
121
- last_update = now
122
- await _invoke_progress(progress_func, uploaded_bytes, file_size, progress_args)
 
123
  break
124
  except Exception:
125
- await asyncio.sleep(0.2)
126
 
127
- tasks = []
128
- with open(file_path, "rb") as fp:
129
- for part_idx in range(total_parts):
130
- chunk = fp.read(CHUNK_SIZE)
131
- if not chunk:
132
- break
133
- tasks.append(worker_send(part_idx, chunk))
 
134
 
135
- await asyncio.gather(*tasks)
136
  return InputFileBig(id=file_id, parts=total_parts, name=os.path.basename(file_path))
 
5
  import time
6
  from pyrogram import Client
7
  from pyrogram.raw.functions.upload import GetFile, SaveBigFilePart
8
+ from pyrogram.raw.types import InputFileBig, InputDocumentFileLocation
9
 
10
+ CHUNK_SIZE = 512 * 1024 # 512 KB
 
11
 
12
  async def _invoke_progress(progress_func, current, total, progress_args):
13
  if not progress_func:
 
20
  pass
21
 
22
  async def fast_download_file(client: Client, message, out_path: str, progress_func=None, progress_args=()):
23
+ media = message.document or message.video or message.audio
24
  if not media:
25
+ return await client.download_media(message, file_name=out_path)
26
 
27
  file_size = getattr(media, "file_size", 0)
 
 
 
28
  if file_size == 0:
29
  return await client.download_media(message, file_name=out_path)
30
 
31
+ # 1. सही DC ID निकालना (टेलीग्राम फ़ाइलें अक्सर अलग DC पर स्टोर होती हैं)
32
+ dc_id = getattr(media, "dc_id", client.session.dc_id)
33
+
34
+ file_id_obj = InputDocumentFileLocation(
35
+ id=media.id,
36
+ access_hash=media.access_hash,
37
+ file_reference=media.file_reference,
38
+ thumb_size=""
39
+ )
 
 
 
 
 
 
 
 
 
40
 
41
  total_parts = math.ceil(file_size / CHUNK_SIZE)
42
  downloaded_bytes = 0
43
+ lock = asyncio.Lock()
44
+ last_update = 0
45
 
46
+ # डिस्क पर स्पेस पहले से एलोकेट करें
47
+ with open(out_path, "wb") as f:
48
+ f.truncate(file_size)
 
49
 
50
+ # फ़ाइल को एक बार 'r+b' में खोलकर रखें (बार-बार open/close न करें)
51
+ fp = open(out_path, "r+b")
52
+
53
+ # कतार (Queue) आधारित पैरेलल डाउनलोड
54
+ queue = asyncio.Queue()
55
+ for i in range(total_parts):
56
+ queue.put_nowait(i)
57
 
58
+ async def worker():
59
  nonlocal downloaded_bytes, last_update
60
+ while not queue.empty():
61
+ try:
62
+ part_index = queue.get_nowait()
63
+ except asyncio.QueueEmpty:
64
+ break
65
+
66
+ offset = part_index * CHUNK_SIZE
67
+ chunk_bytes = None
68
+
69
+ for attempt in range(5):
70
  try:
71
+ # dc_id स्पष्ट रूप से पास करने से रीडायरेक्ट लेटेंसी खत्म होती है
72
  r = await client.invoke(
73
+ GetFile(location=file_id_obj, offset=offset, limit=CHUNK_SIZE),
74
+ dc_id=dc_id
75
  )
76
  chunk_bytes = r.bytes
77
+ break
78
+ except Exception:
79
+ await asyncio.sleep(0.3)
80
+
81
+ if chunk_bytes:
82
+ # I/O ऑपरेशन को थ्रेडपूल में भेजें ताकि इवेंट लूप ब्लॉक न हो
83
+ def write_chunk():
84
+ fp.seek(offset)
85
+ fp.write(chunk_bytes)
86
+
87
+ await asyncio.to_thread(write_chunk)
88
 
89
+ async with lock:
90
  downloaded_bytes += len(chunk_bytes)
91
  now = time.time()
92
+ if now - last_update >= 0.5 or downloaded_bytes >= file_size:
93
  last_update = now
94
  await _invoke_progress(progress_func, downloaded_bytes, file_size, progress_args)
 
 
 
95
 
96
+ queue.task_done()
97
+
98
+ # 10 से 12 एक्टिव पैरेलल वर्कर्स
99
+ worker_count = 12
100
+ tasks = [asyncio.create_task(worker()) for _ in range(worker_count)]
101
+ await queue.join()
102
+
103
+ for task in tasks:
104
+ task.cancel()
105
 
106
+ fp.close()
107
  return out_path
108
 
109
  async def fast_upload_file(client: Client, file_path: str, progress_func=None, progress_args=()):
 
111
  total_parts = math.ceil(file_size / CHUNK_SIZE)
112
  file_id = client.rnd_id()
113
  uploaded_bytes = 0
114
+ lock = asyncio.Lock()
 
 
115
  last_update = 0
116
 
117
+ queue = asyncio.Queue()
118
+ with open(file_path, "rb") as fp:
119
+ for part_idx in range(total_parts):
120
+ chunk = fp.read(CHUNK_SIZE)
121
+ if not chunk:
122
+ break
123
+ queue.put_nowait((part_idx, chunk))
124
+
125
+ async def worker():
126
  nonlocal uploaded_bytes, last_update
127
+ while not queue.empty():
128
+ try:
129
+ part_index, chunk = queue.get_nowait()
130
+ except asyncio.QueueEmpty:
131
+ break
132
+
133
+ for attempt in range(5):
134
  try:
135
  await client.invoke(
136
  SaveBigFilePart(
 
140
  bytes=chunk
141
  )
142
  )
143
+ async with lock:
144
+ uploaded_bytes += len(chunk)
145
+ now = time.time()
146
+ if now - last_update >= 0.5 or uploaded_bytes >= file_size:
147
+ last_update = now
148
+ await _invoke_progress(progress_func, uploaded_bytes, file_size, progress_args)
149
  break
150
  except Exception:
151
+ await asyncio.sleep(0.3)
152
 
153
+ queue.task_done()
154
+
155
+ worker_count = 12
156
+ tasks = [asyncio.create_task(worker()) for _ in range(worker_count)]
157
+ await queue.join()
158
+
159
+ for task in tasks:
160
+ task.cancel()
161
 
 
162
  return InputFileBig(id=file_id, parts=total_parts, name=os.path.basename(file_path))