-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_collection.py
More file actions
53 lines (46 loc) · 1.62 KB
/
data_collection.py
File metadata and controls
53 lines (46 loc) · 1.62 KB
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
###############################################################################
#
# File: store_images.py
#
# Author: Isaac Ingram
#
# Purpose: Store an image stream from a website to the local disk.
#
# Usage: This program requires the requests library to be installed.
# To use, provide two commandline arguments: <URL> and <path>. 'URL' is the URL
# to requests content from. 'path' is the relative or absolute path to where
# the images should be placed on your system.
#
###############################################################################
import sys
from datetime import datetime
import requests
def print_usage_and_exit() -> None:
"""
Print the usage statement and exit.
:return: None
"""
print("Usage: store_images.py <URL> <path>")
exit(0)
def main():
try:
while(True):
# Check command line args
if len(sys.argv) != 3:
print_usage_and_exit()
url = sys.argv[1]
storage_path = sys.argv[2]
# Get rid of last character in path if it's a slash
if storage_path[-1] == "/":
storage_path = storage_path[:-1]
try:
image_data = requests.get(url).content
img_name = f'{storage_path}/{datetime.now().strftime("%S-%M-%H-%d-%m-%Y.jpg")}'
with open(img_name, 'wb') as image_file:
image_file.write(image_data)
except Exception as e:
print(f"Failed to download from {url}: {e}")
except KeyboardInterrupt:
print("Program interrupted by user. Exiting...")
if __name__ == '__main__':
main()