|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import argparse |
| 5 | +import requests |
| 6 | +from urllib.parse import urlparse |
| 7 | + |
| 8 | +SPECIAL_FILES_PATH = "test-tree/special-files/" |
| 9 | +LARGE_FILE_URIS = [ |
| 10 | + "https://www.quintic.com/software/sample_videos/Cricket%20Bowling%20150fps%201200.avi" |
| 11 | +] |
| 12 | +ZIP_FILE_URLS = [] |
| 13 | + |
| 14 | + |
| 15 | +def parse_cli(): |
| 16 | + parser = argparse.ArgumentParser( |
| 17 | + prog="special-files-downloader", description="Download special test files" |
| 18 | + ) |
| 19 | + |
| 20 | + parser.add_argument("--large", help=f"Download large files for testing") |
| 21 | + parser.add_argument("--zip", help=f"Download zip files for testing") |
| 22 | + parser.add_argument( |
| 23 | + "--my-sources", help=f"Download files from links listed in text file path" |
| 24 | + ) |
| 25 | + parser.add_argument( |
| 26 | + "--all", |
| 27 | + help="Download all earmarked special files.", |
| 28 | + ) |
| 29 | + |
| 30 | + return parser |
| 31 | + |
| 32 | + |
| 33 | +def download_file_from_url(uri, path): |
| 34 | + response = requests.get(uri, allow_redirects=False) |
| 35 | + fname = os.path.basename(urlparse(uri).path) |
| 36 | + if response.status_code == 200: |
| 37 | + with open(path + fname, "wb") as file: |
| 38 | + file.write(response.content) |
| 39 | + |
| 40 | + |
| 41 | +def main(): |
| 42 | + parser = parse_cli() |
| 43 | + if len(sys.argv) == 1: |
| 44 | + parser.print_help() |
| 45 | + print("No downloads done....") |
| 46 | + args = parser.parse_args() |
| 47 | + |
| 48 | + if args.large: |
| 49 | + for uri in LARGE_FILE_URIS: |
| 50 | + download_file_from_url(uri, SPECIAL_FILES_PATH + "large/") |
| 51 | + if args.zip: |
| 52 | + for uri in ZIP_FILE_URLS: |
| 53 | + download_file_from_url(uri, SPECIAL_FILES_PATH + "zips/") |
| 54 | + if args.all: |
| 55 | + ALL = ZIP_FILE_URLS + LARGE_FILE_URIS |
| 56 | + for uri in ALL: |
| 57 | + download_file_from_url(uri, SPECIAL_FILES_PATH + "mixed/") |
| 58 | + if args.my_sources: |
| 59 | + sources = open(args.my_sources, "r") |
| 60 | + sources = sources.readlines() |
| 61 | + for source in sources: |
| 62 | + download_file_from_url(source, SPECIAL_FILES_PATH + "custom/") |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + main() |
0 commit comments