Agnivesh10M commited on
Commit
35e4ff3
·
verified ·
1 Parent(s): 804b8a8

Update TechVJ/TechVJ/fast_download.py

Browse files
Files changed (1) hide show
  1. 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
- queue = asyncio.Queue()
51
- for i in range(total_parts):
52
- queue.put_nowait(i)
53
 
54
- async def worker():
55
  nonlocal downloaded_bytes, last_update
56
- while not queue.empty():
57
- try:
58
- part_index = queue.get_nowait()
59
- except asyncio.QueueEmpty:
60
- break
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
- break
73
- except Exception:
74
- await asyncio.sleep(0.2)
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
- async with lock:
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
- queue.task_done()
91
-
92
- worker_count = 12
93
- tasks = [asyncio.create_task(worker()) for _ in range(worker_count)]
94
- await queue.join()
 
 
 
 
95
 
96
- for task in tasks:
97
- task.cancel()
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
- queue = asyncio.Queue()
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 worker():
119
  nonlocal uploaded_bytes, last_update
120
- while not queue.empty():
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 >= 0.8 or uploaded_bytes >= file_size:
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.2)
145
 
146
- queue.task_done()
 
 
 
 
 
 
147
 
148
- worker_count = 12
149
- tasks = [asyncio.create_task(worker()) for _ in range(worker_count)]
150
- await queue.join()
151
 
152
- for task in tasks:
153
- task.cancel()
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))