{"id":19428883,"url":"https://github.com/eleutherai/best-download","last_synced_at":"2025-04-24T18:31:35.207Z","repository":{"id":49424789,"uuid":"308091584","full_name":"EleutherAI/best-download","owner":"EleutherAI","description":"URL downloader supporting checkpointing and continuous checksumming.","archived":false,"fork":false,"pushed_at":"2023-11-29T10:39:17.000Z","size":36,"stargazers_count":19,"open_issues_count":2,"forks_count":7,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-02T07:39:59.597Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EleutherAI.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-28T17:26:34.000Z","updated_at":"2024-04-22T16:14:50.000Z","dependencies_parsed_at":"2023-11-28T19:27:28.510Z","dependency_job_id":"95a67b2a-414a-4656-ad77-076fa5ccfb65","html_url":"https://github.com/EleutherAI/best-download","commit_stats":{"total_commits":27,"total_committers":3,"mean_commits":9.0,"dds":0.07407407407407407,"last_synced_commit":"374bd5ccc6dc1f3a98091982726a2b898a01b7cb"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EleutherAI%2Fbest-download","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EleutherAI%2Fbest-download/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EleutherAI%2Fbest-download/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EleutherAI%2Fbest-download/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EleutherAI","download_url":"https://codeload.github.com/EleutherAI/best-download/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223961111,"owners_count":17232251,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T14:17:04.741Z","updated_at":"2024-11-10T14:17:05.417Z","avatar_url":"https://github.com/EleutherAI.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# best-download\n![python badge](https://github.com/EleutherAI/best-download/actions/workflows/python-app.yml/badge.svg)  \nURL downloader supporting checkpointing and continuous checksumming.\n\nNOTE: When the local_file already exists we automatically overwrite unless there is a checkpoint file there. When the download successfully completes the checkpoint will be deleted and True returned. This avoids leaving rubbish in the file system or doing full checksum calculations for large files. You will need to manage existing files if your scripts are re-runnable, either maintain your own database/done files or do a manual checksum.\n\n## Recent Updates:\n1. Added multiple urls option for failover.\n2. Parameter changes to 'download_file'\n- *local_file* is now optional, and will be set to the url basepath if not provided\n- Added *local_directory* option, will be prepended to *local_file*. Mainly useful\n  for downloading to a directory and using automatic local_file\n3. Improved SIGINT handling. We now raise a KeyboardInterrupt exception after handling it safely internally.\n4. Added a decent set of tests:\n```\npip install -r requirements-dev.txt\npytest\n```\n\n## Install\n```bash\npip install best-download\n```\n\n##  Quickstart\n```python\nfrom best_download import download_file\n\nurl = \"http://ipv4.download.thinkbroadband.com/10MB.zip\"\nchecksum = \"d076d819249a9827c8a035bb059498bf49f391a989a1f7e166bc70d028025135\"\nlocal_file = \"10MB.zip\"\ntry:\n  success = download_file(url, local_file=local_file, expected_checksum=checksum)\nexcept KeyboardInterrupt:\n  print(\"Ctrl-C (SIGINT) is passed up\")\n```\n\n## API\nThere's only one entry point: \n\n```python\ndef download_file(urls, expected_checksum=None, local_file=None, local_directory=None, max_retries=3)\n```\n\n| Parameter      | Description |\n| -----------: | ----------- |\n| `urls` | Either a single url or a list of urls to iterate over if failover required. |\n| `expected_checksum` | (Optional) Checksum to validate against after download complete. Will not validate if not provided. |\n| `local_file` | (Optional) Output path for saving the file. If not provided we default to the url basepath. | \n| `local_directory` | (Optional) If provided will be prepended to *local_file*. Mainly useful for downloading to a directory and using automatic local_file. |\n| `max_retries` | (Default: 3) Number of retry attmpts per url (per failover if list is provided). |\n\n## Examples\nThe following example can be found in \"examples/basic_example.py\". There are some example urls in the tests array, including test cases for a server not supporting ranges (github) and a server defaulting to gzip encoding which we don't use. We demo resuming at the end.\n\n```python\nimport os\nfrom best_download import download_file\n\nimport logging\nlogger = logging.getLogger()\nconsole_handler = logging.StreamHandler()\nconsole_handler.setLevel(logging.INFO)\nlogger.addHandler(console_handler)\nlogger.setLevel(logging.INFO)\n\ntests = []\ntests.append((\"http://ipv4.download.thinkbroadband.com/10MB.zip\", \"10MB.zip\",\n    \"d076d819249a9827c8a035bb059498bf49f391a989a1f7e166bc70d028025135\"))\n\n# Larger file used for cancel test\ntests.append((\"http://ipv4.download.thinkbroadband.com/100MB.zip\", \"100MB.zip\",\n    \"cc844cac4b2310321d0fd1f9945520e2c08a95cefd6b828d78cdf306b4990b3a\"))\n\n# Github example doesn't support resuming\ntests.append((\"https://github.com/Nealcly/MuTual/archive/master.zip\", \"master.zip\", None))\n\n# Testing Accept-Encoding: identity (no gzip)\ntests.append((\"https://raw.githubusercontent.com/openai/gpt-3/master/data/two_digit_addition.jsonl\",\n             \"two_digit_addition.jsonl\", \"75a54b7a3db3b23369df74fe440c23025f3d3c51f664300bd3d56632b2617b3d\"))\n\ndef main():\n    logger.info(\"Commence Demo\")\n    url, local_file, checksum = tests[0]\n\n    # local_file provided\n    logger.info(f\"\\nTesting download of file {url} to {local_file}\")\n    logger.info(\"-----------------------------------------------------------------------\")\n    download_file(url, local_file=local_file, expected_checksum=checksum)\n    assert os.path.exists(local_file)\n    os.remove(local_file)\n\n    # local_file automatically discovered from url basepath    \n    logger.info(f\"\\nTesting download of file {url} to {local_file} without providing local_file\")\n    logger.info(\"-----------------------------------------------------------------------\")    \n    download_file(url, expected_checksum=checksum)\n    assert os.path.exists(local_file)\n    os.remove(local_file)\n\n    # local_directory provided\n    local_directory = \"testing_download\"\n    local_file_path = os.path.join(local_directory, local_file)\n    logger.info(f\"\\nTesting download of file {url} to {local_file_path}\")\n    logger.info(\"-----------------------------------------------------------------------\")    \n    download_file(url, expected_checksum=checksum, local_file=local_file, local_directory=local_directory)\n    assert os.path.exists(local_file_path)\n    os.remove(local_file_path)\n    os.rmdir(local_directory)\n\n    # local_directory provided + local_file automatically discovered from url basepath\n    local_directory = \"testing_download\"\n    local_file_path = os.path.join(local_directory, local_file)\n    logger.info(f\"\\nTesting download of file {url} to {local_file_path} without providing local_file\")\n    logger.info(\"-----------------------------------------------------------------------\")    \n    download_file(url, expected_checksum=checksum, local_directory=local_directory)\n    assert os.path.exists(local_file_path)\n    os.remove(local_file_path)\n    os.rmdir(local_directory)\n\n    # Resume Test    \n    logger.info(\"\\nResume Test\")\n    logger.info(\"-----------------------------------------------------------------------\")\n    url, local_file, checksum = tests[1]\n    logger.info(\"Please cancel half way through and re-run this example to test resuming\")\n    try:\n        download_file(url, local_file=local_file, expected_checksum=checksum)\n    except KeyboardInterrupt:\n        pass\n    logger.info(\"Attempting resume if you cancelled in time.\")\n    download_file(url, local_file=local_file, expected_checksum=checksum)\n    assert os.path.exists(local_file)\n    os.remove(local_file)\n\nif __name__ == '__main__':\n    main()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feleutherai%2Fbest-download","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feleutherai%2Fbest-download","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feleutherai%2Fbest-download/lists"}