Upload download.py
Browse files- download.py +49 -0
download.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
from concurrent.futures import ThreadPoolExecutor
|
6 |
+
|
7 |
+
def download(url, path):
|
8 |
+
print(f'Downloading {url}...')
|
9 |
+
response = requests.get(url)
|
10 |
+
with open(path, 'wb') as f:
|
11 |
+
f.write(response.content)
|
12 |
+
|
13 |
+
def download_all(urls, path, thread_count):
|
14 |
+
with ThreadPoolExecutor(max_workers=thread_count) as executor:
|
15 |
+
for url in urls:
|
16 |
+
filename = url.split('/')[-1]
|
17 |
+
executor.submit(download, url, os.path.join(path, filename))
|
18 |
+
|
19 |
+
print(f'Downloaded {len(urls)} images to {path}')
|
20 |
+
|
21 |
+
def main(input_path, output_path, thread_count, min_fav, min_retweet):
|
22 |
+
print(f'Reading from {input_path}...')
|
23 |
+
with open(input_path, 'r', encoding='utf-8') as input_file:
|
24 |
+
users_and_images = json.load(input_file)
|
25 |
+
|
26 |
+
image_urls = []
|
27 |
+
|
28 |
+
for user in users_and_images:
|
29 |
+
for image in user['images']:
|
30 |
+
if image['likes'] >= min_fav and image['retweets'] >= min_retweet:
|
31 |
+
image_urls.append(image['url'])
|
32 |
+
|
33 |
+
print(f'Found {len(image_urls)} images with at least {min_fav} likes and {min_retweet} retweets')
|
34 |
+
|
35 |
+
print(f'\nDownloading images to {output_path}...')
|
36 |
+
download_all(image_urls, output_path, thread_count)
|
37 |
+
|
38 |
+
print('Done!')
|
39 |
+
|
40 |
+
if __name__ == '__main__':
|
41 |
+
parser = argparse.ArgumentParser()
|
42 |
+
parser.add_argument('input_path', type=str, default='data.json')
|
43 |
+
parser.add_argument('output_path', type=str, default='images')
|
44 |
+
parser.add_argument('--thread_count', type=int, default=4)
|
45 |
+
parser.add_argument('--min_fav', type=int, default=0)
|
46 |
+
parser.add_argument('--min_retweet', type=int, default=0)
|
47 |
+
args = parser.parse_args()
|
48 |
+
|
49 |
+
main(args.input_path, args.output_path, args.thread_count, args.min_fav, args.min_retweet)
|