Skip to content

Commit

Permalink
prototype main file
Browse files Browse the repository at this point in the history
  • Loading branch information
elder-plinius committed Oct 14, 2023
1 parent 150c5dd commit 45f6633
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
File renamed without changes.
97 changes: 97 additions & 0 deletions dalle/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from undetected_chromedriver import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import datetime
import logging
import requests
import os
import time

def get_cookie_value():
options = ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
driver = Chrome(options=options)

# Go to Bing Image Creator and log in manually here.
# Once logged in, the script will proceed to get the cookie.
driver.get('https://www.bing.com/images/create')

input("Press Enter after you've logged in...")

# Open developer tools
ActionChains(driver).key_down(Keys.CONTROL).key_down(Keys.SHIFT).send_keys('j').key_up(Keys.CONTROL).key_up(Keys.SHIFT).perform()
time.sleep(2) # Allow devtools to open

# Execute JavaScript code to return the value of '_U' cookie
cookie_value = driver.execute_script('return document.cookie.split("; ").find(row => row.startsWith("_U=")).split("=")[1]')

Check failure on line 29 in dalle/main.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle/main.py:29:89: E501 Line too long (127 > 88 characters)
driver.quit()

return cookie_value

# Function to get the current time in a specific format
def get_time():
return datetime.datetime.now().strftime("[%d/%m/%Y %H:%M:%S]")

# Function to get the current time in a format suitable for file and folder names
def get_time_save():
return datetime.datetime.now().strftime("%d-%m-%Y %H-%M-%S")
# Function to download images from given URLs
def download_images(urls, save_folder):
save_folder = (save_folder)[:225]
try:
timestamp_folder = os.path.join(save_folder, get_time_save())
if not os.path.exists(timestamp_folder):
os.makedirs(timestamp_folder)

for index, url in enumerate(urls):
response = requests.get(url)
response.raise_for_status()
filename = os.path.join(timestamp_folder, f"{save_folder} ({index + 1}).png")

Check failure on line 52 in dalle/main.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle/main.py:52:89: E501 Line too long (89 > 88 characters)
with open(filename, 'wb') as file:
file.write(response.content)

logging.info(f'{get_time()} Image downloaded successfully and saved to "{filename}"')

Check failure on line 56 in dalle/main.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle/main.py:56:89: E501 Line too long (97 > 88 characters)

except requests.exceptions.RequestException as e:
logging.critical(f"Image download failed: {str(e)}")

# Function to open the Bing website and set cookies
def open_website(query, cookie_value):
options = ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--headless")
driver = Chrome(options=options)

cookie = {"name": "_U", "value": cookie_value}
driver.get(f'https://www.bing.com/images/create?q={query}')
logging.info(f"{get_time()} Bing Image Creator (Dalle-3) Opened")

driver.add_cookie(cookie)
driver.refresh()
logging.info(f"{get_time()} Cookie values added ")
return driver

# Function to get image URLs
def get_urls(driver):
try:
urls = list(set([element.get_attribute("src") for element in WebDriverWait(driver, 600).until(

Check failure on line 80 in dalle/main.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle/main.py:80:89: E501 Line too long (102 > 88 characters)
EC.presence_of_all_elements_located((By.CLASS_NAME, "mimg")))]))

urls = [url.split('?')[0] for url in urls]
return urls
except Exception as e:
logging.critical(
f"Error while extracting image urls. Maybe something is wrong about your prompt. (You can check your prompt manually) \n{e}")

Check failure on line 87 in dalle/main.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle/main.py:87:89: E501 Line too long (137 > 88 characters)

# Main part of the script
if __name__ == "__main__":
query = "your_image_query_here"
save_folder = "your_save_folder_here"

cookie_value = get_cookie_value()
driver = open_website(query, cookie_value)
urls = get_urls(driver)
download_images(urls, save_folder)
Empty file removed package/main.py
Empty file.
Empty file removed package/subfolder/__init__.py
Empty file.
Empty file removed package/subfolder/main.py
Empty file.

0 comments on commit 45f6633

Please sign in to comment.