Skip to content

Instantly share code, notes, and snippets.

@Alyetama
Created August 18, 2022 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alyetama/80434842b831b3cabdfb6143fdea4b07 to your computer and use it in GitHub Desktop.
Save Alyetama/80434842b831b3cabdfb6143fdea4b07 to your computer and use it in GitHub Desktop.
Stable Diffusion Scraper
python-dotenv>=0.20.0
selenium>=4.3.0
#!/usr/bin/env python
# coding: utf-8
import json
import os
import re
import time
from datetime import datetime
from discord.ext import commands
from dotenv import load_dotenv
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException, NoSuchElementException # noqa
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
load_dotenv()
CHANNEL_LINK = os.environ['CHANNEL_LINK']
CHROME_BIN = os.environ['CHROME_BIN']
CHROMEDRIVER_EXEC = os.environ['CHROMEDRIVER_EXEC']
HELPER_TOKEN = os.environ['HELPER_TOKEN']
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--mute-audio')
options.add_argument('--start-maximized')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.binary_location = CHROME_BIN
service = Service(CHROMEDRIVER_EXEC)
driver = webdriver.Chrome(options=options, service=service)
driver.get('https://discord.com/login')
driver.execute_script("let token =\"" + HELPER_TOKEN + """\";
function login(token) {
setInterval(() => {
document.body.appendChild(
document.createElement `iframe`
).contentWindow.localStorage.token = `"${token}"`
}, 50);
setTimeout(() => {
location.reload();
}, 2500);
}
login(token);""")
time.sleep(8)
driver.get(CHANNEL_LINK)
time.sleep(5)
elms = driver.find_elements(By.TAG_NAME, 'li')
try:
inbox_button = driver.find_element(
By.XPATH,
'/html/body/div[1]/div[2]/div/div[1]/div/div[2]/div/div[1]/div/div/'
'div[3]/section/div[2]/div[6]').click()
except NoSuchElementException:
elms = driver.find_elements(By.TAG_NAME, 'div')
inbox_button = [
e for e in elms if e.get_attribute('aria-label') == 'Inbox'
][0].click()
time.sleep(1)
inbox = driver.find_element(
By.XPATH, '/html/body/div[1]/div[2]/div/div[3]/div/div/div/div[2]/div[1]')
inbox.find_element(
By.XPATH,
'/html/body/div[1]/div[2]/div/div[3]/div/div/div/div[1]/div[2]/div').click(
)
inbox_filter = inbox.find_element(
By.XPATH, '/html/body/div[1]/div[2]/div/div[3]/div[2]')
include_everyone = inbox_filter.find_element(By.ID, 'mentions-filter-Everyone')
if include_everyone.get_attribute('aria-checked') == 'true':
include_everyone.click()
time.sleep(1)
include_all_servers = inbox_filter.find_element(By.ID,
'mentions-filter-All Servers')
if include_all_servers.get_attribute('aria-checked') == 'true':
include_all_servers.click()
time.sleep(1)
inbox_messages = [
e for e in inbox.find_elements(By.TAG_NAME, 'div')
if e.get_attribute('role') == 'article'
]
output = []
for message in inbox_messages:
image_container = message.find_elements(By.TAG_NAME, 'div')
if 'DreamBotMothership' not in message.text:
continue
for e in image_container:
try:
url = e.find_element(By.TAG_NAME, 'a').get_attribute('href')
filler = 'Message could not be loaded.\n'
output.append([message.text.replace(filler, ''), url])
except NoSuchElementException:
continue
data = []
for response, url in output:
out = response.split('\n')
now = datetime.now()
try:
timestamp = datetime.strptime(' '.join(out[2].split(' ')[2:]),
'%I:%M %p').replace(year=now.year,
month=now.month,
day=now.day)
except ValueError:
timestamp = datetime.strptime(out[2], '%m/%d/%Y')
took = re.search(r'\d+.\d+s', out[3])[0]
prompt = '!dream '.join(out[3].split('!dream')[1:]).strip()
commands = out[5:]
d = {
'timestamp': str(timestamp),
'took': took,
'prompt': prompt,
'commands': commands,
'url': url
}
data.append(d)
# Export as JSON
with open('data.json', 'w') as j:
json.dump(data, j, indent=4)
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment