-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.py
More file actions
66 lines (57 loc) · 2.13 KB
/
tempCodeRunnerFile.py
File metadata and controls
66 lines (57 loc) · 2.13 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
62
63
64
65
66
import json
import os
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from PIL import Image
from io import BytesIO
# Load the data
with open('data.json', 'r') as file:
data = json.load(file)
# Set up Selenium
service = Service(ChromeDriverManager().install())
options = webdriver.ChromeOptions()
options.add_argument("--headless") # Run in headless mode
driver = webdriver.Chrome(service=service, options=options)
def fetch_image_url(query):
search_url = f"https://www.google.com/search?tbm=isch&q={query}"
driver.get(search_url)
# Wait for the images to load and get the first image
driver.implicitly_wait(5)
images = driver.find_elements(By.CSS_SELECTOR, 'img')
if images:
return images[0].get_attribute('src')
return None
def download_image(url, filename):
try:
response = requests.get(url)
image = Image.open(BytesIO(response.content))
image.save(filename)
print(f"Downloaded image for {filename}")
except Exception as e:
print(f"Error downloading image from {url}: {e}")
# Ensure the 'images' directory exists
os.makedirs('images', exist_ok=True)
# Process each item in the data
for item in data:
composition_name = item['compositionName']
author = item['author']
query = f"{composition_name} by {author}"
print(f"Fetching image for '{query}'...")
image_url = fetch_image_url(query)
if image_url:
# Replace invalid filename characters with underscores
safe_file_name = "".join([c if c.isalnum() else "_" for c in composition_name])
file_path = os.path.join('images', f"{safe_file_name}.jpg")
download_image(image_url, file_path)
item['imageUrl'] = file_path
else:
item['imageUrl'] = None
# Save the updated data
with open('updated_data.json', 'w') as file:
json.dump(data, file, indent=2)
# Close the Selenium driver
driver.quit()
print("All images fetched and data updated.")