added try:except: to upload_ftp_ai_images

This commit is contained in:
phixxy 2024-01-27 21:06:01 -08:00
parent e2e42e8139
commit c0946d1e14

View file

@ -151,30 +151,33 @@ class PhixxyCom(commands.Cog):
os.rename(filename, 'tmp/' + new_file_name) os.rename(filename, 'tmp/' + new_file_name)
async def upload_ftp_ai_images(self, ai_dict): async def upload_ftp_ai_images(self, ai_dict):
for folder in ai_dict: try:
for filename in os.listdir(folder): for folder in ai_dict:
if filename[-4:] == '.png': for filename in os.listdir(folder):
filepath = folder + filename if filename[-4:] == '.png':
prompt = self.find_prompt_from_filename(ai_dict[folder], filename) filepath = folder + filename
html_file = f"{self.data_dir}ai-images/index.html" prompt = self.find_prompt_from_filename(ai_dict[folder], filename)
html_insert = '''<!--REPLACE THIS COMMENT--> html_file = f"{self.data_dir}ai-images/index.html"
<div> html_insert = '''<!--REPLACE THIS COMMENT-->
<img src="<!--filename-->" loading="lazy"> <div>
<p class="image-description"><!--description--></p> <img src="<!--filename-->" loading="lazy">
</div>''' <p class="image-description"><!--description--></p>
server_folder = (os.getenv('ftp_public_html') + 'ai-images/') </div>'''
new_filename = str(time.time_ns()) + ".png" server_folder = (os.getenv('ftp_public_html') + 'ai-images/')
await self.upload_sftp(filepath, server_folder, new_filename) new_filename = str(time.time_ns()) + ".png"
self.bot.logger.debug("Uploaded", new_filename) await self.upload_sftp(filepath, server_folder, new_filename)
with open(html_file, 'r') as f: self.bot.logger.debug("Uploaded", new_filename)
html_data = f.read() with open(html_file, 'r') as f:
html_insert = html_insert.replace("<!--filename-->", new_filename) html_data = f.read()
html_insert = html_insert.replace("<!--description-->", prompt) html_insert = html_insert.replace("<!--filename-->", new_filename)
html_data = html_data.replace("<!--REPLACE THIS COMMENT-->", html_insert) html_insert = html_insert.replace("<!--description-->", prompt)
with open(html_file, "w") as f: html_data = html_data.replace("<!--REPLACE THIS COMMENT-->", html_insert)
f.writelines(html_data) with open(html_file, "w") as f:
await self.upload_sftp(html_file, server_folder, "index.html") f.writelines(html_data)
os.rename(filepath, f"tmp/{new_filename}") await self.upload_sftp(html_file, server_folder, "index.html")
os.rename(filepath, f"tmp/{new_filename}")
except:
self.bot.logger.exception("Something went wrong in upload_ftp_ai_images")
async def answer_question(self, topic, model="gpt-3.5-turbo"): async def answer_question(self, topic, model="gpt-3.5-turbo"):
headers = { headers = {
@ -326,7 +329,7 @@ class PhixxyCom(commands.Cog):
"data/chatgpt/dalle2/":"data/chatgpt/logs/dalle2.log" "data/chatgpt/dalle2/":"data/chatgpt/logs/dalle2.log"
} }
await self.meme_handler('tmp/meme/') await self.meme_handler('tmp/meme/')
await self.upload_ftp_ai_images('tmp/stable_diffusion/sfw/') await self.upload_ftp_ai_images(ai_images_dict)
@tasks.loop(hours=1) @tasks.loop(hours=1)
async def blog_loop(self): async def blog_loop(self):