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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
| """ Usage:
In a terminal/command line, cd to the directory where this file lives. Then...
With embedded urls: ( download the hardcoded list of files in the 'files =' block below)
python ./download-all-2025-07-15_15-33-05.py
Download all files in a Metalink/CSV: (downloaded from ASF Vertex)
python ./download-all-2025-07-15_15-33-05.py /path/to/downloads.metalink localmetalink.metalink localcsv.csv
Compatibility: python >= 3.6
If downloading from a trusted source with invalid SSL Certs, use --insecure to ignore
For more information on bulk downloads, navigate to: https://asf.alaska.edu/how-to/data-tools/data-tools/#bulk_download
This script was generated by the Alaska Satellite Facility's bulk download service. For more information on the service, navigate to: http://bulk-download.asf.alaska.edu/help """
import sys import csv import os import os.path import tempfile import shutil import re
import base64 import time import getpass import ssl import signal import socket
import xml.etree.ElementTree as ET
from urllib.request import build_opener, install_opener, Request, urlopen from urllib.request import HTTPHandler, HTTPSHandler, HTTPCookieProcessor from urllib.error import HTTPError, URLError
from http.cookiejar import MozillaCookieJar from io import StringIO
import concurrent.futures from threading import Lock
abort = False
cookie_jar_lock = Lock()
def signal_handler(sig, frame): global abort sys.stderr.write("\n > Caught Signal. Exiting!\n") abort = True raise SystemExit
def get_thread_opener(cookie_jar_path, context): cookie_jar = MozillaCookieJar() if os.path.isfile(cookie_jar_path): try: cookie_jar.load(cookie_jar_path, ignore_discard=True, ignore_expires=True) except Exception as e: sys.stderr.write(f"Warning: Could not load cookie jar in thread: {e}\n") opener = build_opener( HTTPCookieProcessor(cookie_jar), HTTPHandler(), HTTPSHandler(**context) ) return opener, cookie_jar
def download_one_file_wrapper(url, file_count, total, cookie_jar_path, context, asf_urs4): global abort
opener, thread_cookie_jar = get_thread_opener(cookie_jar_path, context) install_opener(opener)
temp_downloader = bulk_downloader(init_empty=True) temp_downloader.cookie_jar_path = cookie_jar_path temp_downloader.cookie_jar = thread_cookie_jar temp_downloader.context = context temp_downloader.asf_urs4 = asf_urs4 size, total_size = temp_downloader.download_file_with_cookiejar(url, file_count, total)
return { 'url': url, 'size': size, 'total_size': total_size, 'status': 'success' if temp_downloader.is_good_download(total_size, size) else 'failed' }
class bulk_downloader: def __init__(self, init_empty=False): if init_empty: self.files = [] self.cookie_jar_path = None self.cookie_jar = None self.asf_urs4 = {} self.context = {} return
self.files = [ "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20231116T100545_20231116T100612_051241_062E6C_0E03/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20231116T100545_20231116T100612_051241_062E6C_0E03/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20231104T100545_20231104T100608_051066_062861_1D22/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20231104T100545_20231104T100608_051066_062861_1D22/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20231011T100545_20231011T100612_050716_061C6C_EBE7/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20231011T100545_20231011T100612_050716_061C6C_EBE7/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230824T100544_20230824T100611_050016_060479_B862/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230824T100544_20230824T100611_050016_060479_B862/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230731T100543_20230731T100610_049666_05F8E6_9783/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230731T100543_20230731T100610_049666_05F8E6_9783/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230613T100540_20230613T100602_048966_05E36F_5994/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230613T100540_20230613T100602_048966_05E36F_5994/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230601T100539_20230601T100606_048791_05DE1B_47E1/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230601T100539_20230601T100606_048791_05DE1B_47E1/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230520T100539_20230520T100606_048616_05D8E5_EF6E/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230520T100539_20230520T100606_048616_05D8E5_EF6E/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230508T100538_20230508T100605_048441_05D3B5_663A/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230508T100538_20230508T100605_048441_05D3B5_663A/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230426T100537_20230426T100604_048266_05CDD9_4B44/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230426T100537_20230426T100604_048266_05CDD9_4B44/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230414T100537_20230414T100604_048091_05C7F9_CC51/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230414T100537_20230414T100604_048091_05C7F9_CC51/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230402T100537_20230402T100604_047916_05C20A_C9D3/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230402T100537_20230402T100604_047916_05C20A_C9D3/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230321T100536_20230321T100559_047741_05BC32_9E02/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230321T100536_20230321T100559_047741_05BC32_9E02/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230309T100536_20230309T100558_047566_05B642_6A57/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230309T100536_20230309T100558_047566_05B642_6A57/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230225T100536_20230225T100559_047391_05B059_A7AD/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230225T100536_20230225T100559_047391_05B059_A7AD/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230213T100536_20230213T100559_047216_05AA62_E12E/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230213T100536_20230213T100559_047216_05AA62_E12E/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230201T100537_20230201T100559_047041_05A486_907E/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230201T100537_20230201T100559_047041_05A486_907E/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230120T100537_20230120T100559_046866_059EA8_353E/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230120T100537_20230120T100559_046866_059EA8_353E/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230108T100537_20230108T100600_046691_0598BF_5C9B/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230108T100537_20230108T100600_046691_0598BF_5C9B/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221227T100538_20221227T100600_046516_0592E0_DA00/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221227T100538_20221227T100600_046516_0592E0_DA00/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221215T100539_20221215T100606_046341_058CDF_5715/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221215T100539_20221215T100606_046341_058CDF_5715/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221203T100539_20221203T100602_046166_0586F1_5712/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221203T100539_20221203T100602_046166_0586F1_5712/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221121T100540_20221121T100603_045991_0580FB_DDFA/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221121T100540_20221121T100603_045991_0580FB_DDFA/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221109T100540_20221109T100602_045816_057B19_AB1C/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221109T100540_20221109T100602_045816_057B19_AB1C/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221028T100541_20221028T100603_045641_057529_45F6/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221028T100541_20221028T100603_045641_057529_45F6/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220922T100539_20220922T100602_045116_056439_C6B0/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220922T100539_20220922T100602_045116_056439_C6B0/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220910T100540_20220910T100607_044941_055E50_7AC8/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220910T100540_20220910T100607_044941_055E50_7AC8/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220805T100538_20220805T100605_044416_054CE7_FAB0/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220805T100538_20220805T100605_044416_054CE7_FAB0/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220724T100537_20220724T100604_044241_0547C5_09A8/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220724T100537_20220724T100604_044241_0547C5_09A8/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220525T100533_20220525T100600_043366_052DBA_EB07/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220525T100533_20220525T100600_043366_052DBA_EB07/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220501T100532_20220501T100559_043016_0522D8_F388/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220501T100532_20220501T100559_043016_0522D8_F388/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220419T100531_20220419T100558_042841_051D0F_CFB9/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220419T100531_20220419T100558_042841_051D0F_CFB9/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220407T100531_20220407T100558_042666_051731_9A6B/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220407T100531_20220407T100558_042666_051731_9A6B/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220326T100531_20220326T100558_042491_05114B_42D9/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220326T100531_20220326T100558_042491_05114B_42D9/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220314T100530_20220314T100557_042316_050B55_5602/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220314T100530_20220314T100557_042316_050B55_5602/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220206T100531_20220206T100558_041791_04F952_6852/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220206T100531_20220206T100558_041791_04F952_6852/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220125T100531_20220125T100558_041616_04F34E_B897/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220125T100531_20220125T100558_041616_04F34E_B897/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220101T100532_20220101T100559_041266_04E794_1BC0/IW1/VV/1.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220101T100532_20220101T100559_041266_04E794_1BC0/IW1/VV/0.tiff", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20231116T100545_20231116T100612_051241_062E6C_0E03/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20231116T100545_20231116T100612_051241_062E6C_0E03/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20231104T100545_20231104T100608_051066_062861_1D22/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20231104T100545_20231104T100608_051066_062861_1D22/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20231011T100545_20231011T100612_050716_061C6C_EBE7/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20231011T100545_20231011T100612_050716_061C6C_EBE7/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230824T100544_20230824T100611_050016_060479_B862/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230824T100544_20230824T100611_050016_060479_B862/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230731T100543_20230731T100610_049666_05F8E6_9783/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230731T100543_20230731T100610_049666_05F8E6_9783/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230613T100540_20230613T100602_048966_05E36F_5994/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230613T100540_20230613T100602_048966_05E36F_5994/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230601T100539_20230601T100606_048791_05DE1B_47E1/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230601T100539_20230601T100606_048791_05DE1B_47E1/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230520T100539_20230520T100606_048616_05D8E5_EF6E/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230520T100539_20230520T100606_048616_05D8E5_EF6E/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230508T100538_20230508T100605_048441_05D3B5_663A/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230508T100538_20230508T100605_048441_05D3B5_663A/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230426T100537_20230426T100604_048266_05CDD9_4B44/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230426T100537_20230426T100604_048266_05CDD9_4B44/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230414T100537_20230414T100604_048091_05C7F9_CC51/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230414T100537_20230414T100604_048091_05C7F9_CC51/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230402T100537_20230402T100604_047916_05C20A_C9D3/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230402T100537_20230402T100604_047916_05C20A_C9D3/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230321T100536_20230321T100559_047741_05BC32_9E02/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230321T100536_20230321T100559_047741_05BC32_9E02/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230309T100536_20230309T100558_047566_05B642_6A57/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230309T100536_20230309T100558_047566_05B642_6A57/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230225T100536_20230225T100559_047391_05B059_A7AD/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230225T100536_20230225T100559_047391_05B059_A7AD/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230213T100536_20230213T100559_047216_05AA62_E12E/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230213T100536_20230213T100559_047216_05AA62_E12E/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230201T100537_20230201T100559_047041_05A486_907E/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230201T100537_20230201T100559_047041_05A486_907E/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230120T100537_20230120T100559_046866_059EA8_353E/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230120T100537_20230120T100559_046866_059EA8_353E/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230108T100537_20230108T100600_046691_0598BF_5C9B/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20230108T100537_20230108T100600_046691_0598BF_5C9B/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221227T100538_20221227T100600_046516_0592E0_DA00/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221227T100538_20221227T100600_046516_0592E0_DA00/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221215T100539_20221215T100606_046341_058CDF_5715/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221215T100539_20221215T100606_046341_058CDF_5715/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221203T100539_20221203T100602_046166_0586F1_5712/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221203T100539_20221203T100602_046166_0586F1_5712/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221121T100540_20221121T100603_045991_0580FB_DDFA/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221121T100540_20221121T100603_045991_0580FB_DDFA/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221109T100540_20221109T100602_045816_057B19_AB1C/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221109T100540_20221109T100602_045816_057B19_AB1C/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221028T100541_20221028T100603_045641_057529_45F6/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20221028T100541_20221028T100603_045641_057529_45F6/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220922T100539_20220922T100602_045116_056439_C6B0/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220922T100539_20220922T100602_045116_056439_C6B0/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220910T100540_20220910T100607_044941_055E50_7AC8/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220910T100540_20220910T100607_044941_055E50_7AC8/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220805T100538_20220805T100605_044416_054CE7_FAB0/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220805T100538_20220805T100605_044416_054CE7_FAB0/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220724T100537_20220724T100604_044241_0547C5_09A8/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220724T100537_20220724T100604_044241_0547C5_09A8/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220525T100533_20220525T100600_043366_052DBA_EB07/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220525T100533_20220525T100600_043366_052DBA_EB07/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220501T100532_20220501T100559_043016_0522D8_F388/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220501T100532_20220501T100559_043016_0522D8_F388/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220419T100531_20220419T100558_042841_051D0F_CFB9/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220419T100531_20220419T100558_042841_051D0F_CFB9/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220407T100531_20220407T100558_042666_051731_9A6B/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220407T100531_20220407T100558_042666_051731_9A6B/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220326T100531_20220326T100558_042491_05114B_42D9/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220326T100531_20220326T100558_042491_05114B_42D9/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220314T100530_20220314T100557_042316_050B55_5602/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220314T100530_20220314T100557_042316_050B55_5602/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220206T100531_20220206T100558_041791_04F952_6852/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220206T100531_20220206T100558_041791_04F952_6852/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220125T100531_20220125T100558_041616_04F34E_B897/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220125T100531_20220125T100558_041616_04F34E_B897/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220101T100532_20220101T100559_041266_04E794_1BC0/IW1/VV/1.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20220101T100532_20220101T100559_041266_04E794_1BC0/IW1/VV/0.xml", "https://sentinel1-burst.asf.alaska.edu/S1A_IW_SLC__1SDV_20211220T100533_20211220T100600_041091_04E1B5_EDAE/IW1/VV/1.xml" ]
self.cookie_jar_path = os.path.join( os.path.expanduser('~'), ".bulk_download_cookiejar.txt" ) self.cookie_jar = None
self.asf_urs4 = { 'url': 'https://urs.earthdata.nasa.gov/oauth/authorize', 'client': 'BO_n7nTIlMljdvU6kRRB3g', 'redir': 'https://auth.asf.alaska.edu/login'}
if os.access(os.getcwd(), os.W_OK) is False: sys.stderr.write(f"WARNING: Cannot write to current path! Check permissions for {os.getcwd()}\n") exit(-1)
self.context = {}
if len(sys.argv) > 0: download_files = [] input_files = [] for arg in sys.argv[1:]: if arg == '--insecure': try: ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE self.context['context'] = ctx except AttributeError: pass
elif arg.endswith('.metalink') or arg.endswith('.csv'): if os.path.isfile(arg): input_files.append(arg) if arg.endswith('.metalink'): new_files = self.process_metalink(arg) else: new_files = self.process_csv(arg) if new_files is not None: for file_url in (new_files): download_files.append(file_url) else: sys.stderr.write(f" > I cannot find the input file you specified: {arg}\n") else: sys.stderr.write(f" > Command line argument '{arg}' makes no sense, ignoring.\n")
if len(input_files) > 0: if len(download_files) > 0: sys.stdout.write(f" > Processing {len(download_files)} downloads from {len(input_files)} input files. \n") self.files = download_files else: sys.stdout.write(f" > I see you asked me to download files from {len(input_files)} input files, but they had no downloads!\n") sys.stdout.write(" > I'm super confused and exiting.\n") exit(-1)
self.get_cookie()
self.total_bytes = 0 self.total_time = 0 self.cnt = 0 self.success = [] self.failed = [] self.skipped = []
def get_cookie(self): if os.path.isfile(self.cookie_jar_path): self.cookie_jar = MozillaCookieJar() try: self.cookie_jar.load(self.cookie_jar_path, ignore_discard=True, ignore_expires=True) except Exception as e: sys.stderr.write(f"Error loading existing cookie jar: {e}. Will attempt to get a new one.\n") self.cookie_jar = None
if self.cookie_jar and self.check_cookie(): sys.stdout.write(" > Reusing previous cookie jar.\n") return else: sys.stdout.write(" > Could not validate old cookie Jar\n")
sys.stdout.write("No existing URS cookie found, please enter Earthdata username & password:\n") sys.stdout.write("(Credentials will not be stored, saved or logged anywhere)\n")
while self.check_cookie() is False: self.get_new_cookie()
def check_cookie(self): if self.cookie_jar is None: sys.stderr.write(f" > Cookiejar is bunk: {self.cookie_jar}\n") return False
file_check = 'https://urs.earthdata.nasa.gov/profile'
opener = build_opener( HTTPCookieProcessor(self.cookie_jar), HTTPHandler(), HTTPSHandler(**self.context) )
request = Request(file_check) request.get_method = lambda : 'HEAD' try: sys.stdout.write(f" > attempting to download {file_check}\n") response = opener.open(request, timeout=30) resp_code = response.getcode() if not self.check_cookie_is_logged_in(self.cookie_jar): return False
with cookie_jar_lock: self.cookie_jar.save(self.cookie_jar_path, ignore_discard=True, ignore_expires=True) sys.stdout.write(" > Cookie jar saved.\n")
except HTTPError: sys.stderr.write("\nIMPORTANT: \n") sys.stderr.write("Your user appears to lack permissions to download data from the ASF Datapool.\n") sys.stderr.write("\n\nNew users: you must first log into Vertex and accept the EULA. In addition, your Study Area must be set at Earthdata https://urs.earthdata.nasa.gov\n") exit(-1)
if resp_code in (300, 301, 302, 303): try: redir_url = response.info().getheader('Location') except AttributeError: redir_url = response.getheader('Location')
if ("vertex-retired.daac.asf.alaska.edu" in redir_url and "test" in self.asf_urs4['redir']): sys.stdout.write("Cough, cough. It's dusty in this test env!\n") return True
sys.stderr.write(f"Redirect ({resp_code}) occured, invalid cookie value!\n") return False
if resp_code in (200, 307): return True else: return False
def get_new_cookie(self):
new_username = input("Username: ") new_password = getpass.getpass(prompt="Password (will not be displayed): ")
auth_cookie_url = self.asf_urs4['url'] + '?client_id=' + self.asf_urs4['client'] + '&redirect_uri=' + self.asf_urs4['redir'] + '&response_type=code&state='
user_pass = base64.b64encode(bytes(new_username+":"+new_password, "utf-8")) user_pass = user_pass.decode("utf-8")
self.cookie_jar = MozillaCookieJar() opener = build_opener(HTTPCookieProcessor(self.cookie_jar), HTTPHandler(), HTTPSHandler(**self.context)) request = Request(auth_cookie_url, headers={"Authorization": "Basic {0}".format(user_pass)})
try: response = opener.open(request) except HTTPError as e: if "WWW-Authenticate" in e.headers and "Please enter your Earthdata Login credentials" in e.headers["WWW-Authenticate"]: sys.stderr.write(" > Username and Password combo was not successful. Please try again.\n") return False else: sys.stderr.write("\nIMPORTANT: There was an error obtaining a download cookie!\n") sys.stderr.write("Your user appears to lack permission to download data from the ASF Datapool.\n") sys.stderr.write("\n\nNew users: you must first log into Vertex and accept the EULA. In addition, your Study Area must be set at Earthdata https://urs.earthdata.nasa.gov\n") exit(-1) except URLError as e: sys.stderr.write("\nIMPORTANT: There was a problem communicating with URS, unable to obtain cookie. \n") sys.stderr.write("Try cookie generation later.\n") exit(-1)
if self.check_cookie_is_logged_in(self.cookie_jar): with cookie_jar_lock: self.cookie_jar.save(self.cookie_jar_path, ignore_discard=True, ignore_expires=True) return True
sys.stderr.write("WARNING: Could not generate new cookie! Cannot proceed. Please try Username and Password again.\n") sys.stderr.write(f"Response was {response.getcode()}.\n") sys.stderr.write("\n\nNew users: you must first log into Vertex and accept the EULA. In addition, your Study Area must be set at Earthdata https://urs.earthdata.nasa.gov\n") exit(-1)
def check_cookie_is_logged_in(self, cj): for cookie in cj: if cookie.name == 'urs_user_already_logged': return True
return False
def download_file_with_cookiejar(self, url, file_count, total, recursion=False): current_opener, current_cookie_jar = get_thread_opener(self.cookie_jar_path, self.context) install_opener(current_opener)
download_file = os.path.basename(url).split('?')[0]
if os.path.isfile(download_file): try: request = Request(url) request.get_method = lambda : 'HEAD' response = current_opener.open(request, timeout=30) remote_size = self.get_total_size(response)
if remote_size: local_size = os.path.getsize(download_file) if remote_size < (local_size + (local_size * .01)) and remote_size > (local_size - (local_size * .01)): sys.stdout.write(f" > Download file {download_file} exists! \n > Skipping download of {url}. \n") return None, None sys.stdout.write(f" > Found {download_file} but it wasn't fully downloaded. Removing file and downloading again.\n") os.remove(download_file)
except ssl.CertificateError as e: sys.stderr.write(f" > ERROR: {e}\n") sys.stderr.write(" > Could not validate SSL Cert. You may be able to overcome this using the --insecure flag\n") return False, None
except HTTPError as e: if e.code == 401: sys.stderr.write(" > IMPORTANT: Your user may not have permission to download this type of data!\n") elif e.code == 400: sys.stderr.write(f" > HTTP Error 400 (Bad Request) for {url}. This often means the download URL has expired or is invalid. Please try re-generating your download list and run the script again.\n") else: sys.stderr.write(f" > Unknown Error, Could not get file HEAD: {e}\n") return False, None
except URLError as e: sys.stderr.write(f"URL Error (from HEAD): {e.reason}, {url}\n") if "ssl.c" in f"{e.reason}": sys.stderr.write("IMPORTANT: Remote location may not be accepting your SSL configuration. This is a terminal error.\n") return False, None except socket.timeout as e: sys.stderr.write(f" > HEAD request timeout for: {url}; {e}\n") return False, None
try: request = Request(url) response = current_opener.open(request, timeout=60)
while response.getcode() == 202: sys.stdout.write(" > Waiting for burst extraction service...\n") time.sleep(5) response = current_opener.open(request, timeout=60)
if response.geturl() != url:
if 'https://urs.earthdata.nasa.gov/oauth/authorize' in response.geturl(): if recursion: sys.stderr.write(" > Entering seemingly endless auth loop. Aborting. \n") return False, None
new_auth_url = response.geturl() if "app_type" not in new_auth_url: new_auth_url += "&app_type=401"
sys.stdout.write(f" > While attempting to download {url}....\n") sys.stdout.write(f" > Need to obtain new cookie from {new_auth_url}\n") old_cookies = [cookie.name for cookie in current_cookie_jar] reauth_opener = build_opener(HTTPCookieProcessor(current_cookie_jar), HTTPHandler(), HTTPSHandler(**self.context)) request = Request(new_auth_url) try: response = reauth_opener.open(request) for cookie in current_cookie_jar: if cookie.name not in old_cookies: sys.stdout.write(f" > Saved new cookie: {cookie.name}\n")
if cookie.discard: cookie.expires = int(time.time()) + 60*60*24*30 sys.stdout.write(" > Saving session Cookie that should have been discarded! \n") with cookie_jar_lock: current_cookie_jar.save(self.cookie_jar_path, ignore_discard=True, ignore_expires=True) except HTTPError as e: sys.stderr.write(f"HTTP Error: {e.code}, {url}\n") return False, None
sys.stdout.write(" > Attempting download again with new cookies!\n") return self.download_file_with_cookiejar(url, file_count, total, recursion=True)
sys.stdout.write(f" > 'Temporary' Redirect download @ Remote archive:\n > {response.geturl()}\n")
sys.stdout.write(f"({file_count}/{total}) Downloading {url}\n")
content_disposition = response.headers.get('Content-Disposition')
if content_disposition and len(content_disposition): possible_filename = re.findall("filename=(\S+)", content_disposition)
if possible_filename: download_file = possible_filename.pop()
tf = tempfile.NamedTemporaryFile(mode='w+b', delete=False, dir='.') self.chunk_read(response, tf, report_hook=self.chunk_report)
tempfile_name = tf.name tf.close()
except HTTPError as e: sys.stderr.write(f"HTTP Error: {e.code}, {url}\n")
if e.code == 401: sys.stderr.write(" > IMPORTANT: Your user does not have permission to download this type of data!\n") elif e.code == 400: sys.stderr.write(f" > HTTP Error 400 (Bad Request) for {url}. This often means the download URL has expired or is invalid. Please try re-generating your download list and run the script again.\n") elif e.code == 403: sys.stderr.write(" > Got a 403 Error trying to download this file. \n") sys.stderr.write(" > You MAY need to log in this app and agree to a EULA. \n")
return False, None
except URLError as e: sys.stderr.write(f"URL Error (from GET): {e}, {e.reason}, {url}\n")
if "ssl.c" in f"{e.reason}": sys.stderr.write("IMPORTANT: Remote location may not be accepting your SSL configuration. This is a terminal error.\n")
return False, None
except socket.timeout as e: sys.stderr.write(f" > timeout requesting: {url}; {e}\n") return False, None
except ssl.CertificateError as e: sys.stderr.write(f" > ERROR: {e}\n") sys.stderr.write(" > Could not validate SSL Cert. You may be able to overcome this using the --insecure flag\n") return False, None
shutil.copy(tempfile_name, download_file) os.remove(tempfile_name) file_size = self.get_total_size(response) actual_size = os.path.getsize(download_file) if file_size is None: file_size = actual_size
return actual_size, file_size
def get_redirect_url_from_error(self, error): find_redirect = re.compile(r"id=\"redir_link\"\s+href=\"(\S+)\"") sys.stderr.write(f"error file was: {error}\n") redirect_url = find_redirect.search(error)
if redirect_url: sys.stdout.write(f"Found: {redirect_url.group(0)}\n") return (redirect_url.group(0))
return None
def chunk_report(self, bytes_so_far, file_size): if file_size is not None: percent = float(bytes_so_far) / file_size percent = round(percent*100, 2) sys.stdout.write(f" > Downloaded {bytes_so_far} of {file_size} bytes ({percent:0.2f}%) for current file...\r") else: sys.stdout.write(f" > Downloaded {bytes_so_far} of unknown Size for current file...\r") sys.stdout.flush()
def chunk_read(self, response, local_file, chunk_size=8192, report_hook=None): file_size = self.get_total_size(response) bytes_so_far = 0
while 1: try: chunk = response.read(chunk_size) except Exception: sys.stdout.write("\n > There was an error reading data. \n") break
try: local_file.write(chunk) except TypeError: try: local_file.write(chunk.decode('utf-8')) except (UnicodeDecodeError, AttributeError): sys.stderr.write("\n > Encoding error writing chunk. Skipping decode.\n") local_file.write(chunk) bytes_so_far += len(chunk)
if not chunk: break
if report_hook: report_hook(bytes_so_far, file_size)
return bytes_so_far
def get_total_size(self, response): try: file_size = response.info().getheader('Content-Length').strip() except AttributeError: try: file_size = response.getheader('Content-Length').strip() except AttributeError: sys.stderr.write("> Problem getting size\n") return None
return int(file_size)
def process_metalink(self, ml_file): sys.stdout.write(f"Processing metalink file: {ml_file}\n") with open(ml_file, 'r') as ml: xml = ml.read()
it = ET.iterparse(StringIO(xml)) for _, el in it: if '}' in el.tag: el.tag = el.tag.split('}', 1)[1] root = it.root
dl_urls = [] ml_files = root.find('files') for dl in ml_files: dl_urls.append(dl.find('resources').find('url').text)
if len(dl_urls) > 0: return dl_urls else: return None
def process_csv(self, csv_file): sys.stdout.write(f"Processing csv file: {csv_file}\n")
dl_urls = [] with open(csv_file, 'r') as csvf: try: csvr = csv.DictReader(csvf) for row in csvr: dl_urls.append(row['URL']) except csv.Error as e: sys.stderr.write(f"WARNING: Could not parse file {csv_file}, line {csvr.line_num}: {e}. Skipping.\n") return None except KeyError: sys.stderr.write(f"WARNING: Could not find URL column in file {csv_file}. Skipping.\n")
if len(dl_urls) > 0: return dl_urls else: return None
def download_files(self, max_workers=8): total_files = len(self.files) sys.stdout.write(f"Starting concurrent download of {total_files} files with {max_workers} workers.\n")
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor: future_to_url = {executor.submit( download_one_file_wrapper, url, idx + 1, total_files, self.cookie_jar_path, self.context, self.asf_urs4 ): url for idx, url in enumerate(self.files)}
for future in concurrent.futures.as_completed(future_to_url): url = future_to_url[future] if abort: executor.shutdown(wait=False, cancel_futures=True) sys.stderr.write("\n > Download aborted due to user signal.\n") break try: result = future.result() if result['status'] == 'success': self.total_bytes += result['size'] self.success.append({'file': result['url'], 'size': result['size']}) sys.stdout.write(f"({self.cnt+1}/{total_files}) Downloaded {os.path.basename(result['url'])} successfully.\n") else: self.failed.append(result['url']) sys.stderr.write(f"({self.cnt+1}/{total_files}) Failed to download {os.path.basename(result['url'])}.\n") self.cnt += 1 except SystemExit: executor.shutdown(wait=False, cancel_futures=True) raise except Exception as exc: self.failed.append(url) sys.stderr.write(f"({self.cnt+1}/{total_files}) {url} generated an exception: {exc}\n") self.cnt += 1
def is_good_download(self, total_size, size): return ( size is not False and size is not None and ( total_size is not None and total_size < (size + (size * .01)) and total_size > (size - (size * .01)) ) )
def print_summary(self): sys.stdout.write("\n\nDownload Summary \n") sys.stdout.write("--------------------------------------------------------------------------------\n") sys.stdout.write(f" Successes: {len(self.success)} files, {self.total_bytes} bytes \n") for success_file in self.success: size_mb = success_file['size']/1024.0**2 sys.stdout.write(f" - {success_file['file']} {size_mb:.2f}MB\n")
if len(self.failed) > 0: sys.stdout.write(f" Failures: {len(self.failed)} files\n") for failed_file in self.failed: sys.stdout.write(f" - {failed_file}\n")
if len(self.skipped) > 0: sys.stdout.write(f" Skipped: {len(self.skipped)} files\n") for skipped_file in self.skipped: sys.stdout.write(" - {0}\n".format(skipped_file))
if len(self.success) > 0: total_bytes_mb = self.total_bytes/1024.0**2 if self.total_time > 0: download_rate = total_bytes_mb / self.total_time sys.stdout.write(f" Average Rate: {download_rate:.2f}MB/sec\n") else: sys.stdout.write(" Average Rate: N/A (not enough data for calculation)\n")
sys.stdout.write("--------------------------------------------------------------------------------\n")
if __name__ == "__main__": signal.signal(signal.SIGINT, signal_handler)
downloader = bulk_downloader() downloader.download_files(max_workers=8) downloader.print_summary()
|