|
2 | 2 |
|
3 | 3 | import os |
4 | 4 | import shutil |
| 5 | +from urllib.parse import urlparse |
5 | 6 |
|
6 | 7 | import requests |
7 | 8 |
|
8 | 9 | TEST_DIR = os.path.abspath(os.path.dirname(__file__)) |
9 | 10 | os.chdir(os.path.join(TEST_DIR, "testdata")) |
10 | 11 |
|
11 | 12 |
|
12 | | -def download_img(url, img): |
13 | | - r = requests.get(url + img, stream=True) |
| 13 | +def download_img(url, img_name): |
| 14 | + r = requests.get(url, stream=True) |
14 | 15 | if r.status_code == 200: |
15 | | - with open(img, "wb") as f: |
| 16 | + parse = urlparse(url) |
| 17 | + img_name = os.path.basename(parse.path) |
| 18 | + with open(img_name, "wb") as f: |
16 | 19 | r.raw.decode_content = True |
17 | 20 | shutil.copyfileobj(r.raw, f) |
18 | 21 | else: |
19 | | - raise Exception("Failed to download " + url + img) |
| 22 | + raise Exception("Failed to download " + url) |
20 | 23 |
|
21 | 24 |
|
22 | 25 | with open("TEST_IMAGES.txt", "r") as f: |
23 | | - required_imgs = f.read().splitlines() |
24 | | -download_url = required_imgs.pop(0) |
| 26 | + img_urls = f.read().splitlines() |
| 27 | + img_names = [os.path.basename(urlparse(url).path) for url in img_urls] |
25 | 28 |
|
26 | | -for img in required_imgs: |
27 | | - if not os.path.isfile(img): |
28 | | - print("Downloading " + img) |
29 | | - download_img(download_url, img) |
| 29 | + for url, img_name in zip(img_urls, img_names): |
| 30 | + if not os.path.isfile(img_name): |
| 31 | + print("Downloading " + img_name) |
| 32 | + download_img(url, img_name) |
0 commit comments