-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathubuntu_image_fetcher.py
More file actions
61 lines (45 loc) · 1.84 KB
/
Copy pathubuntu_image_fetcher.py
File metadata and controls
61 lines (45 loc) · 1.84 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
54
55
56
57
58
59
60
61
import os
import requests
from urllib.parse import urlparse
MAX_FILE_SIZE = 10_000_000 # 10MB
def is_valid_image_response(response):
content_type = response.headers.get("Content-Type", "")
if not content_type.startswith("image/"):
print(f"✗ Skipped: Not an image ({content_type})")
return False
content_length = response.headers.get("Content-Length")
if content_length and int(content_length) > MAX_FILE_SIZE:
print(f"✗ Skipped: Image too large ({int(content_length)/1_000_000:.2f} MB)")
return False
return True
def fetch_image(image_url):
try:
response = requests.get(image_url, timeout=10)
response.raise_for_status()
if not is_valid_image_response(response):
return
parsed = urlparse(image_url)
filename = os.path.basename(parsed.path) or "image.jpg"
file_path = os.path.join("Fetched_Images", filename)
if os.path.exists(file_path):
print(f"↪ Skipped (already exists): {filename}")
return
with open(file_path, "wb") as f:
f.write(response.content)
print(f"✓ Successfully fetched: {filename}")
print(f"✓ Image saved to {file_path}")
print("\nConnection strengthened. Community enriched.")
except requests.exceptions.RequestException as e:
print(f"✗ Failed to fetch image: {e}")
except Exception as e:
print(f"✗ Unexpected error: {e}")
def main():
print("Welcome to the Ubuntu Image Fetcher")
print("A tool for mindfully collecting images from the web\n")
urls_input = input("Please enter image URLs separated by commas: ")
image_urls = [url.strip() for url in urls_input.split(",")]
os.makedirs("Fetched_Images", exist_ok=True)
for url in image_urls:
fetch_image(url)
if __name__ == "__main__":
main()