Spaces:
Running on Zero
Running on Zero
Update TechVJ/TechVJ/fast_download.py
Browse files- TechVJ/TechVJ/fast_download.py +42 -59
TechVJ/TechVJ/fast_download.py
CHANGED
|
@@ -7,7 +7,7 @@ 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:
|
|
@@ -47,54 +47,43 @@ async def fast_download_file(client: Client, message, out_path: str, progress_fu
|
|
| 47 |
|
| 48 |
fp = open(out_path, "r+b")
|
| 49 |
|
| 50 |
-
|
| 51 |
-
for i in range(total_parts):
|
| 52 |
-
queue.put_nowait(i)
|
| 53 |
|
| 54 |
-
async def
|
| 55 |
nonlocal downloaded_bytes, last_update
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
offset = part_index * CHUNK_SIZE
|
| 63 |
-
chunk_bytes = None
|
| 64 |
|
|
|
|
| 65 |
for attempt in range(5):
|
| 66 |
try:
|
| 67 |
r = await client.invoke(
|
| 68 |
GetFile(location=file_id_obj, offset=offset, limit=CHUNK_SIZE),
|
| 69 |
dc_id=dc_id
|
| 70 |
)
|
| 71 |
-
chunk_bytes = r.bytes
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
if chunk_bytes:
|
| 77 |
-
def write_chunk():
|
| 78 |
-
fp.seek(offset)
|
| 79 |
-
fp.write(chunk_bytes)
|
| 80 |
-
|
| 81 |
-
await asyncio.to_thread(write_chunk)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
downloaded_bytes += len(chunk_bytes)
|
| 85 |
-
now = time.time()
|
| 86 |
-
if now - last_update >= 0.8 or downloaded_bytes >= file_size:
|
| 87 |
-
last_update = now
|
| 88 |
-
await _invoke_progress(progress_func, downloaded_bytes, file_size, progress_args)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
for
|
| 97 |
-
|
| 98 |
|
| 99 |
fp.close()
|
| 100 |
return out_path
|
|
@@ -107,22 +96,11 @@ async def fast_upload_file(client: Client, file_path: str, progress_func=None, p
|
|
| 107 |
lock = asyncio.Lock()
|
| 108 |
last_update = 0
|
| 109 |
|
| 110 |
-
|
| 111 |
-
with open(file_path, "rb") as fp:
|
| 112 |
-
for part_idx in range(total_parts):
|
| 113 |
-
chunk = fp.read(CHUNK_SIZE)
|
| 114 |
-
if not chunk:
|
| 115 |
-
break
|
| 116 |
-
queue.put_nowait((part_idx, chunk))
|
| 117 |
|
| 118 |
-
async def
|
| 119 |
nonlocal uploaded_bytes, last_update
|
| 120 |
-
|
| 121 |
-
try:
|
| 122 |
-
part_index, chunk = queue.get_nowait()
|
| 123 |
-
except asyncio.QueueEmpty:
|
| 124 |
-
break
|
| 125 |
-
|
| 126 |
for attempt in range(5):
|
| 127 |
try:
|
| 128 |
await client.invoke(
|
|
@@ -136,20 +114,25 @@ async def fast_upload_file(client: Client, file_path: str, progress_func=None, p
|
|
| 136 |
async with lock:
|
| 137 |
uploaded_bytes += len(chunk)
|
| 138 |
now = time.time()
|
| 139 |
-
if now - last_update >=
|
| 140 |
last_update = now
|
| 141 |
await _invoke_progress(progress_func, uploaded_bytes, file_size, progress_args)
|
| 142 |
break
|
| 143 |
except Exception:
|
| 144 |
-
await asyncio.sleep(0.
|
| 145 |
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
await queue.join()
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
|
| 155 |
return InputFileBig(id=file_id, parts=total_parts, name=os.path.basename(file_path))
|
|
|
|
| 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:
|
|
|
|
| 47 |
|
| 48 |
fp = open(out_path, "r+b")
|
| 49 |
|
| 50 |
+
sem = asyncio.Semaphore(8)
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
async def download_part(part_index):
|
| 53 |
nonlocal downloaded_bytes, last_update
|
| 54 |
+
offset = part_index * CHUNK_SIZE
|
| 55 |
+
# आख़िरी चंक के लिए सही बाइट गणना
|
| 56 |
+
limit = min(CHUNK_SIZE, file_size - offset)
|
| 57 |
+
if limit <= 0:
|
| 58 |
+
return
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
async with sem:
|
| 61 |
for attempt in range(5):
|
| 62 |
try:
|
| 63 |
r = await client.invoke(
|
| 64 |
GetFile(location=file_id_obj, offset=offset, limit=CHUNK_SIZE),
|
| 65 |
dc_id=dc_id
|
| 66 |
)
|
| 67 |
+
chunk_bytes = r.bytes[:limit]
|
| 68 |
+
|
| 69 |
+
def write_to_disk():
|
| 70 |
+
fp.seek(offset)
|
| 71 |
+
fp.write(chunk_bytes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
await asyncio.to_thread(write_to_disk)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
async with lock:
|
| 76 |
+
downloaded_bytes += len(chunk_bytes)
|
| 77 |
+
now = time.time()
|
| 78 |
+
if now - last_update >= 1.0 or downloaded_bytes >= file_size:
|
| 79 |
+
last_update = now
|
| 80 |
+
await _invoke_progress(progress_func, downloaded_bytes, file_size, progress_args)
|
| 81 |
+
break
|
| 82 |
+
except Exception:
|
| 83 |
+
await asyncio.sleep(0.3)
|
| 84 |
|
| 85 |
+
tasks = [download_part(i) for i in range(total_parts)]
|
| 86 |
+
await asyncio.gather(*tasks)
|
| 87 |
|
| 88 |
fp.close()
|
| 89 |
return out_path
|
|
|
|
| 96 |
lock = asyncio.Lock()
|
| 97 |
last_update = 0
|
| 98 |
|
| 99 |
+
sem = asyncio.Semaphore(8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
async def upload_part(part_index, chunk):
|
| 102 |
nonlocal uploaded_bytes, last_update
|
| 103 |
+
async with sem:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
for attempt in range(5):
|
| 105 |
try:
|
| 106 |
await client.invoke(
|
|
|
|
| 114 |
async with lock:
|
| 115 |
uploaded_bytes += len(chunk)
|
| 116 |
now = time.time()
|
| 117 |
+
if now - last_update >= 1.0 or uploaded_bytes >= file_size:
|
| 118 |
last_update = now
|
| 119 |
await _invoke_progress(progress_func, uploaded_bytes, file_size, progress_args)
|
| 120 |
break
|
| 121 |
except Exception:
|
| 122 |
+
await asyncio.sleep(0.3)
|
| 123 |
|
| 124 |
+
tasks = []
|
| 125 |
+
with open(file_path, "rb") as fp:
|
| 126 |
+
for part_idx in range(total_parts):
|
| 127 |
+
chunk = fp.read(CHUNK_SIZE)
|
| 128 |
+
if not chunk:
|
| 129 |
+
break
|
| 130 |
+
tasks.append(upload_part(part_idx, chunk))
|
| 131 |
|
| 132 |
+
# सभी पार्ट्स पूरे होने का इंतज़ार
|
| 133 |
+
await asyncio.gather(*tasks)
|
|
|
|
| 134 |
|
| 135 |
+
# 100% प्रोग्रेस कॉल
|
| 136 |
+
await _invoke_progress(progress_func, file_size, file_size, progress_args)
|
| 137 |
|
| 138 |
return InputFileBig(id=file_id, parts=total_parts, name=os.path.basename(file_path))
|