-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_script.py
381 lines (322 loc) · 13.4 KB
/
main_script.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/bin/python3
# Copyright 2023, John Mueller
# MIT License, see LICENSE file
# SPDX-FileCopyrightText: 2023 John Mueller
# SPDX-License-Identifier: MIT
# https://github.com/softplus/midjourney_lightroom
import os
import re
import uuid
import base64
import dotenv
import hashlib
import requests
import signal
import discord
import piexif
import piexif.helper
from discord.ext import tasks, commands
from urllib.parse import urlencode, quote_plus
from PIL import Image
from lightroom import Lightroom
from io import BytesIO
from datetime import datetime
# folders
dir_root = os.getcwd()
dir_temp = os.path.join(dir_root, "tmp")
dir_output = os.path.join(dir_root, "out")
if not os.path.exists(dir_temp): os.makedirs(dir_temp)
if not os.path.exists(dir_output): os.makedirs(dir_output)
# settings
dotenv_file = dotenv.find_dotenv()
if not dotenv_file: dotenv_file = ".env"
config = dotenv.dotenv_values(dotenv_file)
def config_update(name, value):
"""Update config in dictionary and save to file"""
global dotenv_file, config
config[name] = value
dotenv.set_key(dotenv_file, name, value)
# clean up settings, save as needed
def config_missing(name, default=""):
global config
if name not in config: return True
if config[name] == default: return True
# ask for and save Discord token, if needed
if config_missing("DISCORD_TOKEN", "DISCORD_TOKEN"):
print("No Discord token found in .env settings. Please create one and enter it now:")
token = input("Token: ")
if not token:
print("No token entered, exiting.")
quit()
config_update("DISCORD_TOKEN", token)
# ask for and save Lightroom API client ID and client secret, if needed
if config_missing("LR_CLIENT_ID", "CLIENT_ID"):
print("No Lightroom API keys found in .env settings. Please create one, and get the client ID + client secret.")
token = input("Client ID: ")
if not token:
print("Nothing entered, exiting.")
quit()
config_update("LR_CLIENT_ID", token)
token = input("Client secret: ")
if not token:
print("Nothing entered, exiting.")
quit()
config_update("LR_CLIENT_SECRET", token)
# do the salt thing if needed
if config_missing("FILE_SALT", "..."):
print("Creating new salt for random file names.")
config_update("FILE_SALT", str(uuid.uuid1()))
# check whether to keep the files
if config_missing("KEEP_FILES"):
print("Defaulting to keep all files, set KEEP_FILES='0' otherwise")
config_update("KEEP_FILES", str(1))
def lightroom_login():
"""Attempt to do Lightroom login, return error text if not."""
global lr_api, lr_catalog, config
result = ""
if config_missing("LR_ACCESS_TOKEN"): config["LR_ACCESS_TOKEN"] = ""
lr_api = Lightroom(config["LR_CLIENT_ID"], config["LR_ACCESS_TOKEN"])
lr_catalog = None
try:
lr_catalog = lr_api.catalog_api()
except Exception as e:
result = e
lr_catalog = None
lr_api = None
return result
error_str = lightroom_login()
if error_str:
print("Lightroom authentication code invalid, starting fresh.")
print(error_str)
local_channel_id = None
if not config_missing("DISCORD_CHANNEL"):
local_channel_id = int(config["DISCORD_CHANNEL"])
# ready to rumble
def exif_date_to_string(in_date):
"""Formate datetime as EXIF date: "YYYY:MM:DD HH:MM:SS" """
return in_date.strftime("%Y:%m:%d %H:%M:%S")
def load_and_split_image(image_file, do_split):
"""split image into 4 equal parts"""
with Image.open(image_file) as im:
result = []
if do_split:
full_w, full_h = im.size
part_w = full_w // 2
part_h = full_h // 2
result.append(im.crop((0, 0, part_w, part_h)))
result.append(im.crop((part_w, 0, full_w, part_h)))
result.append(im.crop((0, part_h, part_w, full_h)))
result.append(im.crop((part_w, part_h, full_w, full_h)))
else:
result.append(im.copy())
return result
def append_log_csv(data_values):
"""Save values to CSV file for logging"""
global dir_output
with open(os.path.join(dir_output, "_log.csv"), "a") as f:
f.write("\t".join(data_values) + "\n")
def upload_to_form(data_values):
"""Posts list of values to the Google Form."""
global config
append_log_csv(data_values)
if config_missing("FORM_URL", "https://"): return # not set, skipping
def get_field_ids(form_url):
"""Returns list of field IDs on the form."""
html = requests.get(form_url).text
form_field_info = re.findall('(<input|<textarea)[^>]*id="([^"]*)"', html)
if form_field_info: # depends on UI version
fields = [x[1] for x in form_field_info if x[1].startswith("entry")]
else:
form_field_info = re.findall('\[\[([0-9]{3,30}),null,0\]\]', html)
fields = ["entry."+x for x in form_field_info]
return fields
form_fields = get_field_ids(config["FORM_URL"])
submit_url = config["FORM_URL"].replace("/viewform", "/formResponse")
values = {}
for counter in range(0, min(len(data_values), len(form_fields))):
values[form_fields[counter]] = data_values[counter]
data_form = urlencode(values, quote_via=quote_plus)
headers = {"Content-Type": "application/x-www-form-urlencoded"}
r = requests.post(submit_url, data=data_form, headers=headers)
# ignore response
# deal with ctrl-c
def sigint_handler(signum, frame):
"""Handle SIGINT"""
print("ctrl-c caught, closing down.")
quit()
signal.signal(signal.SIGINT, sigint_handler)
# the main kazoo
async def process_image_attachment(url, filename, message=None):
"""Do all the things"""
global config, lr_api, lr_catalog, dir_output
response = requests.get(url)
if response.status_code != 200: return
# save temp image
input_file = os.path.join(dir_temp, filename)
with open(input_file, "wb") as f:
f.write(response.content)
print(f" Image downloaded: {filename}")
file_prefix = os.path.splitext(filename)[0]
# create metadata for prompt based on message
infos = {}
infos["prompt"] = message.content.split("**")[1]
infos["message_url"] = message.jump_url
infos["orig_guid"] = filename[-40:-4] # last part of filename before extension
infos["seed"] = "-1"
infos["date"] = message.created_at
images = load_and_split_image(input_file,
do_split=not ("Upscaled by" in message.content
or " - Image #" in message.content))
# Save the output images with dynamic names in the output folder
for index, img in enumerate(images):
# create new UUID to avoid leaking MJ UUID
uuid_hash = hashlib.md5((infos["orig_guid"] + str(index) + config["FILE_SALT"]).encode("utf-8"))
infos["new_guid"] = base64.urlsafe_b64encode(uuid_hash.digest()).decode("utf-8").replace("=","")
# add exif data with MJ UUID + prompt
ifd_0 = {
piexif.ImageIFD.Software: u"Midjourney",
piexif.ImageIFD.DocumentName: infos["orig_guid"],
piexif.ImageIFD.ImageDescription:
infos["prompt"].encode("latin-1", errors="replace").decode("latin-1"),
piexif.ImageIFD.DateTime: exif_date_to_string(infos["date"]),
piexif.ImageIFD.PreviewDateTime: infos["date"].strftime("%Y-%m-%d"),
piexif.ImageIFD.Model: infos["seed"],
piexif.ImageIFD.HostComputer: infos["message_url"],
piexif.ImageIFD.TargetPrinter: infos["new_guid"]
}
exif_bytes = piexif.dump({"0th": ifd_0})
# store locally
if config["KEEP_FILES"]=="1":
img.save(os.path.join(dir_output,
f"{file_prefix}_{index}.jpg"),
exif=exif_bytes)
# upload to Lightroom
try:
asset_id = lr_catalog.create_new_asset_from_file(
infos["new_guid"]+".png", "image",
capture_date=message.created_at,
time_stamp=datetime.utcnow())
except Exception as e:
await message.channel.send("Upload to Lightroom failed!")
await message.channel.send(e)
return
img_byte_arr = BytesIO()
img.save(img_byte_arr, format="PNG", exif=exif_bytes)
print(f" Stored: {infos['new_guid']}.png")
lr_catalog.put_master(asset_id, img_byte_arr.getvalue(), "image/png")
upload_to_form([infos["new_guid"], infos["orig_guid"], infos["prompt"], infos["seed"],
infos["message_url"], filename, url,
"https://www.midjourney.com/app?jobId=" + infos["orig_guid"]])
# Delete the temp file
os.remove(input_file)
async def lr_auth_ask(channel):
"""Ask the user to authenticate with bounce-page for Lightroom"""
page_url = "https://lowly-occipital-croissant.glitch.me/"
authorization_url = "https://ims-na1.adobelogin.com/ims/authorize?"
params = {
"client_id" : config["LR_CLIENT_ID"],
"scope" : "openid,lr_partner_apis,lr_partner_rendition_apis",
"response_type" : "code",
"redirect_uri" : page_url
}
querystring = urlencode(params, quote_via=quote_plus)
url = authorization_url + querystring
await channel.send("Please authenticate here and paste the results here: ")
await channel.send(url)
async def lr_auth_set(channel, content):
"""Handle Lightroom authentication from user"""
global lr_api, lr_catalog, config, dotenv_file
auth_code = content
if auth_code.startswith("auth! "): auth_code = auth_code[6:]
# get actual token
url = "https://ims-na1.adobelogin.com/ims/token"
params = {
"grant_type" : "authorization_code",
"client_id" : config["LR_CLIENT_ID"],
"client_secret" : config["LR_CLIENT_SECRET"],
"code" : auth_code
}
data_form = urlencode(params, quote_via=quote_plus)
headers = {"Content-Type": "application/x-www-form-urlencoded"}
r = requests.post(url, data=data_form, headers=headers)
access_token = r.json()["access_token"]
config_update("LR_ACCESS_TOKEN", access_token)
error_str = lightroom_login()
if error_str:
await channel.send("Lightroom authentication is invalid.")
await channel.send(error_str)
if not lr_api:
await lr_auth_ask(channel)
else:
await channel.send("Lightroom authentication is ok.")
lr_catalog = lr_api.catalog_api()
# Basic Discord client
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def setup_hook(self) -> None:
# start the task to run in the background
self.my_background_task.start()
async def on_ready(self):
"""Discord is ready, say hi"""
global local_channel_id, lr_api
print(f'Logged in as {self.user} (ID: {self.user.id})')
print("Bot connected, enter 'hi!' to confirm")
if local_channel_id:
channel = client.get_channel(local_channel_id)
await channel.send("Your bot Mr_MJ is here.")
if not lr_api:
await lr_auth_ask(channel)
@tasks.loop(seconds=300) # task runs every X seconds
async def my_background_task(self):
"""Checks if Lightroom API is still authenticated"""
global local_channel_id, lr_api
if not local_channel_id or not lr_api: return
health = None
try:
health = lr_api.health()
except:
health = None
if not health:
lr_api = None
channel = client.get_channel(local_channel_id)
await lr_auth_ask(channel)
# done
@my_background_task.before_loop
async def before_my_task(self):
"""Waits before starting the background loop"""
await self.wait_until_ready() # wait until the bot logs in
#@client.event
async def on_message(self, message):
global local_channel_id, lr_api
print(f"Message: {message.content}")
if not local_channel_id:
local_channel_id = message.channel.id
config_update("DISCORD_CHANNEL", str(local_channel_id))
# use 'hi!' to check if the bot is around
if message.content.lower() == "hi!":
await message.channel.send("Everything ok!")
if (lr_api):
await message.channel.send("LR API is ready:")
await message.channel.send(str(lr_api.account()))
else:
await message.channel.send("LR API not connected")
await lr_auth_ask(message.channel)
return
# user wants to authenticate, give bounce URL
if message.content == "auth?":
await lr_auth_ask(message.channel)
# user has auth code, process it
if message.content.startswith("auth! "):
await lr_auth_set(message.channel, message.content)
# handle all attachments (the images)
for attachment in message.attachments:
#await message.add_reaction("✉️") # you could, but don't
if attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif", ".webp")):
await process_image_attachment(attachment.url, attachment.filename, message)
print("")
# at this point we start
client = MyClient(intents=discord.Intents.all())
client.run(config["DISCORD_TOKEN"])
# keeps running until you kill it with Ctrl-C/Ctrl-Z
# okbyeeeee