|
| 1 | +import os |
| 2 | +import random |
| 3 | +import re |
| 4 | +import requests |
| 5 | +import shutil |
| 6 | +import time |
| 7 | +import uuid |
| 8 | + |
| 9 | +from playwright.sync_api import sync_playwright |
| 10 | +import eel |
| 11 | +import openai |
| 12 | + |
| 13 | +# Set OpenAI API key. |
| 14 | +openai.api_key = os.getenv("OPENAI_API_KEY") |
| 15 | + |
| 16 | + |
| 17 | +@eel.expose |
| 18 | +def download_upscaled_images(page, prompt_text): |
| 19 | + try: |
| 20 | + messages = page.query_selector_all(".messageListItem-ZZ7v6g") |
| 21 | + last_four_messages = messages[-4:] |
| 22 | + for message in last_four_messages: |
| 23 | + message_text = message.evaluate_handle('(node) => node.innerText') |
| 24 | + message_text = str(message_text) |
| 25 | + print(message_text) |
| 26 | + if 'Make Variations' in message_text and 'Web' in message_text: |
| 27 | + try: |
| 28 | + last_image = page.query_selector_all('.originalLink-Azwuo9')[-1] |
| 29 | + second_last_image = page.query_selector_all('.originalLink-Azwuo9')[-2] |
| 30 | + third_last_image = page.query_selector_all('.originalLink-Azwuo9')[-3] |
| 31 | + fourth_last_image = page.query_selector_all('.originalLink-Azwuo9')[-4] |
| 32 | + |
| 33 | + for image in [last_image, second_last_image, third_last_image, fourth_last_image]: |
| 34 | + src = image.get_attribute('href') |
| 35 | + url = src |
| 36 | + response = re.sub(r'[^a-zA-Z0-9\s]', '', prompt_text) |
| 37 | + response = response.replace(',', '_').replace(' ', '_') |
| 38 | + response = response[:50] |
| 39 | + r = requests.get(url, stream=True) |
| 40 | + with open(f'{str(response) + str(uuid.uuid1())}.png', 'wb') as out_file: |
| 41 | + shutil.copyfileobj(r.raw, out_file) |
| 42 | + del r |
| 43 | + except Exception as e: |
| 44 | + print(f"An error occurred while downloading the images: {e}") |
| 45 | + else: |
| 46 | + download_upscaled_images(page, prompt_text) |
| 47 | + except Exception as e: |
| 48 | + print(f"An error occurred while finding the last message: {e}") |
| 49 | + |
| 50 | + |
| 51 | +@eel.expose |
| 52 | +def generate_prompt_and_submit_command(page, prompt): |
| 53 | + try: |
| 54 | + prompt_text = gpt3_midjourney_prompt(prompt) |
| 55 | + time.sleep(random.randint(1, 5)) |
| 56 | + pill_value = page.locator('xpath=//*[@id="app-mount"]/div[2]/div[1]/div[1]/div/div[2]/div/div/div/div/div[3]/div/main/form/div/div[2]/div/div[2]/div/div/div/span[2]/span[2]') |
| 57 | + pill_value.fill(prompt_text) |
| 58 | + time.sleep(random.randint(1, 5)) |
| 59 | + page.keyboard.press("Enter") |
| 60 | + print(f'Successfully submitted prompt: {prompt_text}') |
| 61 | + wait_and_select_upscale_options(page, prompt_text) |
| 62 | + except Exception as e: |
| 63 | + print(f"An error occurred while submitting the prompt: {e}") |
| 64 | + |
| 65 | + |
| 66 | +@eel.expose |
| 67 | +def gpt3_midjourney_prompt(prompt, engine='text-davinci-003', temp=0.7, top_p=1.0, tokens=400, freq_pen=0.0, pres_pen=0.0): |
| 68 | + try: |
| 69 | + if not prompt: |
| 70 | + raise ValueError("Prompt cannot be empty.") |
| 71 | + prompt = prompt.encode(encoding='ASCII', errors='ignore').decode() |
| 72 | + response = openai.Completion.create( |
| 73 | + engine=engine, |
| 74 | + prompt=prompt, |
| 75 | + temperature=temp, |
| 76 | + max_tokens=tokens, |
| 77 | + top_p=top_p, |
| 78 | + frequency_penalty=freq_pen, |
| 79 | + presence_penalty=pres_pen |
| 80 | + ) |
| 81 | + if not response.choices: |
| 82 | + raise ValueError("No response from OpenAI API.") |
| 83 | + text = response.choices[0].text.strip() |
| 84 | + if not text: |
| 85 | + raise ValueError("Response text cannot be empty.") |
| 86 | + return text |
| 87 | + except Exception as e: |
| 88 | + print(f"Error occurred: {e} while generating prompt.") |
| 89 | + return None |
| 90 | + |
| 91 | + |
| 92 | +@eel.expose |
| 93 | +def get_last_message(page): |
| 94 | + messages = page.query_selector_all(".messageListItem-ZZ7v6g") |
| 95 | + last_message = messages[-1] |
| 96 | + last_message_text = last_message.evaluate_handle('(node) => node.innerText') |
| 97 | + last_message_text = str(last_message_text) |
| 98 | + print(last_message_text) |
| 99 | + return last_message_text |
| 100 | + |
| 101 | + |
| 102 | +@eel.expose |
| 103 | +def main(bot_command, channel_url, PROMPT): |
| 104 | + with sync_playwright() as p: |
| 105 | + browser = p.chromium.launch(headless=False) |
| 106 | + page = browser.new_page() |
| 107 | + page.goto("https://www.discord.com/login") |
| 108 | + with open("credentials.txt", "r") as f: |
| 109 | + email = f.readline() |
| 110 | + password = f.readline() |
| 111 | + page.fill("input[name='email']", email) |
| 112 | + time.sleep(random.randint(1, 5)) |
| 113 | + page.fill("input[name='password']", password) |
| 114 | + time.sleep(random.randint(1, 5)) |
| 115 | + page.click("button[type='submit']") |
| 116 | + time.sleep(random.randint(5, 10)) |
| 117 | + page.wait_for_url("https://discord.com/channels/@me", timeout=15000) |
| 118 | + print("Successfully logged into Discord.") |
| 119 | + time.sleep(random.randint(1, 5)) |
| 120 | + for i in range(10): |
| 121 | + open_discord_channel(page, channel_url, bot_command, PROMPT) |
| 122 | + print(f"Iteration {i+1} completed.") |
| 123 | + |
| 124 | + |
| 125 | +@eel.expose |
| 126 | +def open_discord_channel(page, channel_url, bot_command, PROMPT): |
| 127 | + page.goto(f"{channel_url}") |
| 128 | + time.sleep(random.randint(1, 5)) |
| 129 | + page.wait_for_load_state("networkidle") |
| 130 | + print("Opened appropriate channel.") |
| 131 | + print("Entering the specified bot command.") |
| 132 | + send_bot_command(page, bot_command, PROMPT) |
| 133 | + return page |
| 134 | + |
| 135 | + |
| 136 | +@eel.expose |
| 137 | +def select_upscale_option(page, option_text): |
| 138 | + page.locator(f"button:has-text('{option_text}')").locator("nth=-1").click() |
| 139 | + print(f"Clicked {option_text} upscale option.") |
| 140 | + |
| 141 | + |
| 142 | +@eel.expose |
| 143 | +def send_bot_command(page, command, PROMPT): |
| 144 | + print("Clicking on chat bar.") |
| 145 | + chat_bar = page.locator('xpath=//*[@id="app-mount"]/div[2]/div[1]/div[1]/div/div[2]/div/div/div/div/div[3]/div[2]/main/form/div/div[1]/div/div[3]/div/div[2]/div') |
| 146 | + time.sleep(random.randint(1, 5)) |
| 147 | + print("Typing in bot command") |
| 148 | + chat_bar.fill(command) |
| 149 | + time.sleep(random.randint(1, 5)) |
| 150 | + print("Selecting the prompt option in the suggestions menu") |
| 151 | + prompt_option = page.locator('xpath=/html/body/div[1]/div[2]/div[1]/div[1]/div/div[2]/div/div/div/div/div[3]/div[2]/main/form/div/div[2]/div/div/div[2]/div[1]/div/div/div') |
| 152 | + time.sleep(random.randint(1, 5)) |
| 153 | + prompt_option.click() |
| 154 | + print("Generating prompt using OpenAI's API.") |
| 155 | + generate_prompt_and_submit_command(page, PROMPT) |
| 156 | + |
| 157 | + |
| 158 | +@eel.expose |
| 159 | +def start_bot(art_type, bot_command, channel_url, descriptors, topic): |
| 160 | + PROMPT = f"Generate a Midjourney prompt to result in an {art_type} image about {topic} include {descriptors}" |
| 161 | + print(f"Prompt: {PROMPT}") |
| 162 | + |
| 163 | + main(bot_command, channel_url, PROMPT) |
| 164 | + |
| 165 | + |
| 166 | +@eel.expose |
| 167 | +def wait_and_select_upscale_options(page, prompt_text): |
| 168 | + prompt_text = prompt_text.lower() |
| 169 | + try: |
| 170 | + last_message = get_last_message(page) |
| 171 | + if 'U1' in last_message: |
| 172 | + print("Found upscale options.") |
| 173 | + print("Attempting to upscale all generated images.") |
| 174 | + try: |
| 175 | + select_upscale_option(page, 'U1') |
| 176 | + time.sleep(random.randint(3, 5)) |
| 177 | + select_upscale_option(page, 'U2') |
| 178 | + time.sleep(random.randint(3, 5)) |
| 179 | + select_upscale_option(page, 'U3') |
| 180 | + time.sleep(random.randint(3, 5)) |
| 181 | + select_upscale_option(page, 'U4') |
| 182 | + time.sleep(random.randint(3, 5)) |
| 183 | + except Exception as e: |
| 184 | + print("An error occurred while selecting upscale options:", e) |
| 185 | + download_upscaled_images(page, prompt_text) |
| 186 | + else: |
| 187 | + print("Photo(s) not fully loaded.") |
| 188 | + wait_and_select_upscale_options(page, prompt_text) |
| 189 | + except Exception as e: |
| 190 | + print("An error occurred while finding the last message:", e) |
| 191 | + |
| 192 | + |
| 193 | +# Initialize eel with your web files folder |
| 194 | +eel.init('web') |
| 195 | + |
| 196 | +# Keep this at the bottom, it will start the web server. |
| 197 | +eel.start('index.html', mode='chrome', cmdline_args=['--kiosk']) |
0 commit comments