(url, output_dir)
| 362 | |
| 363 | |
| 364 | def download_file(url, output_dir): |
| 365 | try: |
| 366 | if 'wikipedia.org' in url and '/wiki/File:' in url: |
| 367 | direct_url = get_wikipedia_image(url) |
| 368 | if direct_url: |
| 369 | url = direct_url |
| 370 | else: |
| 371 | prRed(f"Skipping Wikipedia file page: {url}") |
| 372 | return |
| 373 | |
| 374 | if not is_valid_url(url): |
| 375 | prRed(f"Invalid URL skipped: {url}") |
| 376 | return |
| 377 | |
| 378 | response = requests.get(url, stream=True, timeout=5) |
| 379 | response.raise_for_status() |
| 380 | |
| 381 | parsed_url = urlparse(url) |
| 382 | file_name = os.path.basename(unquote(parsed_url.path)) |
| 383 | if not file_name or '.' not in file_name: |
| 384 | file_name = "unknown_file" |
| 385 | |
| 386 | file_extension = file_name.split('.')[-1].lower() if '.' in file_name else 'unknown' |
| 387 | |
| 388 | file_type_dir = os.path.join(output_dir, file_extension) |
| 389 | os.makedirs(file_type_dir, exist_ok=True) |
| 390 | output_path = os.path.join(file_type_dir, file_name) |
| 391 | |
| 392 | with open(output_path, 'wb') as file: |
| 393 | for chunk in response.iter_content(chunk_size=8192): |
| 394 | file.write(chunk) |
| 395 | prGreen(f"Downloaded: {url} -> {output_path}") |
| 396 | except requests.RequestException as e: |
| 397 | prRed(f"Failed to download {url}: {e}") |
| 398 | |
| 399 | |
| 400 | def get_file_links(url, file_types, limit, all_files): |
no test coverage detected