-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresume.py
More file actions
430 lines (356 loc) · 15.8 KB
/
Copy pathresume.py
File metadata and controls
430 lines (356 loc) · 15.8 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
"""
Resume Module for UnityScraper
Implements resumable downloads with checksums and progress tracking
"""
import hashlib
import logging
import os
import time
from pathlib import Path
from typing import Optional, Callable, Dict
import requests
logger = logging.getLogger(__name__)
class DownloadProgress:
"""Track download progress"""
def __init__(self, total_size: int, filepath: Path):
self.total_size = total_size
self.filepath = filepath
self.downloaded = 0
self.start_time = time.time()
self.last_update = time.time()
self.speed_samples = [] # Track speed over time
self.peak_speed = 0.0
self.avg_speed = 0.0
def update(self, chunk_size: int):
"""Update progress"""
self.downloaded += chunk_size
self.last_update = time.time()
# Calculate instant speed
elapsed_total = time.time() - self.start_time
if elapsed_total > 0:
instant_speed = (self.downloaded / (1024 * 1024)) / elapsed_total
self.speed_samples.append(instant_speed)
if instant_speed > self.peak_speed:
self.peak_speed = instant_speed
if self.speed_samples:
self.avg_speed = sum(self.speed_samples) / len(self.speed_samples)
@property
def percentage(self) -> float:
"""Get download percentage"""
if self.total_size == 0:
return 0.0
return (self.downloaded / self.total_size) * 100
@property
def speed_mbps(self) -> float:
"""Get download speed in MB/s"""
elapsed = time.time() - self.start_time
if elapsed == 0:
return 0.0
return (self.downloaded / (1024 * 1024)) / elapsed
@property
def eta_seconds(self) -> Optional[float]:
"""Estimate time remaining in seconds"""
if self.downloaded == 0:
return None
elapsed = time.time() - self.start_time
if elapsed <= 0:
return 0.0 if self.downloaded >= self.total_size else None
rate = self.downloaded / elapsed
remaining = max(self.total_size - self.downloaded, 0)
return remaining / rate if rate > 0 else None
def get_stats(self) -> Dict[str, float]:
"""Get comprehensive speed statistics"""
return {
'current_speed': self.speed_mbps,
'peak_speed': self.peak_speed,
'average_speed': self.avg_speed,
'percentage': self.percentage,
'eta_seconds': self.eta_seconds or 0
}
def __str__(self) -> str:
eta = self.eta_seconds
eta_str = f"{int(eta)}s" if eta else "N/A"
return (f"{self.filepath.name}: {self.percentage:.1f}% "
f"({self.downloaded}/{self.total_size} bytes) "
f"Speed: {self.speed_mbps:.2f} MB/s (Peak: {self.peak_speed:.2f} MB/s) ETA: {eta_str}")
class ResumableDownloader:
"""Handles resumable downloads with progress tracking and checksums"""
def __init__(self, session: requests.Session, timeout: int = 30, bandwidth_limit: int = 0):
self.session = session
self.timeout = timeout
self.chunk_size = 8192
self.bandwidth_limit = bandwidth_limit # KB/s, 0 = unlimited
self.last_chunk_time = 0
def supports_resume(self, url: str) -> bool:
"""Check if server supports resume (Accept-Ranges header)"""
try:
response = self.session.head(url, timeout=self.timeout)
return response.headers.get('Accept-Ranges') == 'bytes'
except Exception as e:
logger.warning(f"Could not check resume support: {e}")
return False
def get_remote_size(self, url: str) -> Optional[int]:
"""Get remote file size"""
try:
response = self.session.head(url, timeout=self.timeout)
content_length = response.headers.get('Content-Length')
return int(content_length) if content_length else None
except Exception as e:
logger.error(f"Could not get remote file size: {e}")
return None
def apply_bandwidth_limit(self, chunk_size: int):
"""Apply bandwidth throttling if configured"""
if self.bandwidth_limit <= 0:
return
# Calculate sleep time: chunk_size (bytes) / bandwidth_limit (KB/s)
target_time = chunk_size / (self.bandwidth_limit * 1024)
elapsed = time.time() - self.last_chunk_time
if elapsed < target_time:
time.sleep(target_time - elapsed)
self.last_chunk_time = time.time()
def calculate_checksum(self, filepath: Path, algorithm: str = 'sha256') -> str:
"""Calculate file checksum"""
hash_func = hashlib.new(algorithm)
with open(filepath, 'rb') as f:
while chunk := f.read(self.chunk_size):
hash_func.update(chunk)
return hash_func.hexdigest()
def verify_checksum(self, filepath: Path, expected_checksum: str,
algorithm: str = 'sha256') -> bool:
"""Verify file checksum"""
actual = self.calculate_checksum(filepath, algorithm)
return actual.lower() == expected_checksum.lower()
def get_temp_filepath(self, dest_path: Path) -> Path:
"""Get temporary file path for incomplete downloads"""
return dest_path.parent / f"{dest_path.name}.partial"
def get_metadata_filepath(self, dest_path: Path) -> Path:
"""Get metadata file path"""
return dest_path.parent / f"{dest_path.name}.meta"
def save_metadata(self, dest_path: Path, url: str, total_size: int,
checksum: Optional[str] = None):
"""Save download metadata"""
import json
metadata = {
'url': url,
'total_size': total_size,
'checksum': checksum,
'algorithm': 'sha256',
'timestamp': time.time()
}
meta_path = self.get_metadata_filepath(dest_path)
with open(meta_path, 'w') as f:
json.dump(metadata, f)
def load_metadata(self, dest_path: Path) -> Optional[dict]:
"""Load download metadata"""
import json
meta_path = self.get_metadata_filepath(dest_path)
if not meta_path.exists():
return None
try:
with open(meta_path, 'r') as f:
return json.load(f)
except Exception as e:
logger.error(f"Could not load metadata: {e}")
return None
def cleanup_temp_files(self, dest_path: Path):
"""Remove temporary and metadata files"""
temp_path = self.get_temp_filepath(dest_path)
meta_path = self.get_metadata_filepath(dest_path)
for path in [temp_path, meta_path]:
if path.exists():
try:
path.unlink()
except Exception as e:
logger.warning(f"Could not remove {path}: {e}")
def download(self, url: str, dest_path: Path,
expected_checksum: Optional[str] = None,
progress_callback: Optional[Callable[[DownloadProgress], None]] = None,
resume: bool = True) -> bool:
"""
Download file with resume support
Args:
url: Download URL
dest_path: Destination file path
expected_checksum: Optional SHA256 checksum to verify
progress_callback: Optional callback for progress updates
resume: Whether to attempt resuming partial downloads
Returns:
True if download successful, False otherwise
"""
dest_path.parent.mkdir(parents=True, exist_ok=True)
temp_path = self.get_temp_filepath(dest_path)
# Check if file already exists and is valid
if dest_path.exists():
if expected_checksum:
logger.info(f"Verifying existing file: {dest_path.name}")
if self.verify_checksum(dest_path, expected_checksum):
logger.info(f"✓ File already exists and is valid: {dest_path.name}")
return True
else:
logger.warning(f"Existing file checksum mismatch, re-downloading")
dest_path.unlink()
else:
logger.info(f"✓ File already exists: {dest_path.name}")
return True
# Get remote file size
remote_size = self.get_remote_size(url)
if remote_size is None:
logger.error("Could not determine remote file size")
return False
# Check for partial download
start_byte = 0
if resume and temp_path.exists():
metadata = self.load_metadata(dest_path)
if metadata and metadata.get('url') == url:
partial_size = temp_path.stat().st_size
# Verify partial size is reasonable
if partial_size < remote_size:
start_byte = partial_size
logger.info(f"Resuming download from byte {start_byte}")
else:
logger.warning("Partial file larger than remote, starting over")
temp_path.unlink()
else:
logger.info("Metadata mismatch, starting fresh download")
temp_path.unlink()
# Save metadata
self.save_metadata(dest_path, url, remote_size, expected_checksum)
# Prepare headers for resume
headers = {}
if start_byte > 0 and self.supports_resume(url):
headers['Range'] = f'bytes={start_byte}-'
logger.info(f"Server supports resume, requesting from byte {start_byte}")
else:
start_byte = 0
# Download file
try:
response = self.session.get(
url,
headers=headers,
stream=True,
timeout=self.timeout
)
response.raise_for_status()
# Verify range request was honored
if start_byte > 0 and response.status_code != 206:
logger.warning("Server did not honor range request, starting over")
start_byte = 0
temp_path.unlink() if temp_path.exists() else None
# Get actual total size
if response.status_code == 206:
content_range = response.headers.get('Content-Range')
if content_range:
total_size = int(content_range.split('/')[-1])
else:
total_size = remote_size
else:
total_size = int(response.headers.get('Content-Length', remote_size))
# Create progress tracker
progress = DownloadProgress(total_size, dest_path)
progress.downloaded = start_byte
# Download chunks
mode = 'ab' if start_byte > 0 else 'wb'
with open(temp_path, mode) as f:
for chunk in response.iter_content(chunk_size=self.chunk_size):
if chunk:
f.write(chunk)
progress.update(len(chunk))
self.apply_bandwidth_limit(len(chunk))
# Call progress callback
if progress_callback and time.time() - progress.last_update > 0.5:
progress_callback(progress)
# Final progress update
if progress_callback:
progress_callback(progress)
# Verify checksum if provided
if expected_checksum:
logger.info(f"Verifying checksum for {dest_path.name}...")
if not self.verify_checksum(temp_path, expected_checksum):
logger.error("Checksum verification failed!")
return False
logger.info("✓ Checksum verified")
# Move temp file to final destination
if dest_path.exists():
dest_path.unlink()
temp_path.rename(dest_path)
# Cleanup
self.cleanup_temp_files(dest_path)
logger.info(f"✓ Downloaded successfully: {dest_path.name}")
return True
except requests.exceptions.RequestException as e:
logger.error(f"Download failed: {e}")
return False
except Exception as e:
logger.error(f"Unexpected error during download: {e}")
return False
class BatchDownloadManager:
"""Manages multiple resumable downloads"""
def __init__(self, downloader: ResumableDownloader):
self.downloader = downloader
self.active_downloads = {}
self.completed = []
self.failed = []
def add_download(self, url: str, dest_path: Path,
checksum: Optional[str] = None) -> str:
"""Add download to queue"""
download_id = f"{dest_path.name}_{time.time()}"
self.active_downloads[download_id] = {
'url': url,
'dest_path': dest_path,
'checksum': checksum,
'status': 'queued',
'progress': None
}
return download_id
def download_all(self, progress_callback: Optional[Callable] = None) -> dict:
"""Download all queued files"""
results = {
'completed': 0,
'failed': 0,
'total': len(self.active_downloads)
}
for download_id, info in self.active_downloads.items():
logger.info(f"Starting download: {info['dest_path'].name}")
info['status'] = 'downloading'
success = self.downloader.download(
info['url'],
info['dest_path'],
info['checksum'],
progress_callback
)
if success:
info['status'] = 'completed'
self.completed.append(download_id)
results['completed'] += 1
else:
info['status'] = 'failed'
self.failed.append(download_id)
results['failed'] += 1
return results
def get_status(self) -> dict:
"""Get current download status"""
return {
'total': len(self.active_downloads),
'completed': len(self.completed),
'failed': len(self.failed),
'active': len([d for d in self.active_downloads.values()
if d['status'] == 'downloading'])
}
# Example usage
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
# Create session
session = requests.Session()
# Create downloader
downloader = ResumableDownloader(session)
# Progress callback
def on_progress(progress: DownloadProgress):
print(f"\r{progress}", end='', flush=True)
# Download with resume support
success = downloader.download(
url='http://example.com/large_file.bin',
dest_path=Path('downloads/large_file.bin'),
progress_callback=on_progress,
resume=True
)
print(f"\nDownload {'successful' if success else 'failed'}")