added env variable to run the extension or not

This commit is contained in:
phixxy 2024-01-28 22:32:01 -08:00
parent 1f982b908e
commit 8d0389372d

View file

@ -11,6 +11,7 @@ class PhixxyCom(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.upload_enabled = os.getenv('upload_phixxy')
self.SERVER = os.getenv('ftp_server') self.SERVER = os.getenv('ftp_server')
self.USERNAME = os.getenv('ftp_username') self.USERNAME = os.getenv('ftp_username')
self.PASSWORD = os.getenv('ftp_password') self.PASSWORD = os.getenv('ftp_password')
@ -41,10 +42,11 @@ class PhixxyCom(commands.Cog):
return "Unknown Prompt" return "Unknown Prompt"
async def upload_sftp(self, local_filename, server_folder, server_filename): async def upload_sftp(self, local_filename, server_folder, server_filename):
remotepath = server_folder + server_filename if self.upload_enabled.lower() == "true":
async with asyncssh.connect(self.SERVER, username=self.USERNAME, password=self.PASSWORD) as conn: remotepath = server_folder + server_filename
async with conn.start_sftp_client() as sftp: async with asyncssh.connect(self.SERVER, username=self.USERNAME, password=self.PASSWORD) as conn:
await sftp.put(local_filename, remotepath=remotepath) async with conn.start_sftp_client() as sftp:
await sftp.put(local_filename, remotepath=remotepath)
async def delete_local_pngs(self, local_folder): async def delete_local_pngs(self, local_folder):
for filename in os.listdir(local_folder): for filename in os.listdir(local_folder):
@ -152,32 +154,33 @@ class PhixxyCom(commands.Cog):
async def upload_ftp_ai_images(self, ai_dict): async def upload_ftp_ai_images(self, ai_dict):
try: try:
for folder in ai_dict: if self.upload_enabled.lower() == "true":
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':
self.bot.logger.info(f"Found file = {filename}") filepath = folder + filename
prompt = self.find_prompt_from_filename(ai_dict[folder], filename) self.bot.logger.info(f"Found file = {filename}")
self.bot.logger.info(f"Found prompt = {prompt}") prompt = self.find_prompt_from_filename(ai_dict[folder], filename)
html_file = f"{self.data_dir}ai-images/index.html" self.bot.logger.info(f"Found prompt = {prompt}")
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.info(f"Uploaded {new_filename}") await self.upload_sftp(filepath, server_folder, new_filename)
with open(html_file, 'r') as f: self.bot.logger.info(f"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: except:
self.bot.logger.exception("Something went wrong in upload_ftp_ai_images") self.bot.logger.exception("Something went wrong in upload_ftp_ai_images")
@ -220,62 +223,61 @@ class PhixxyCom(commands.Cog):
await ctx.send("Saved suggestion!") await ctx.send("Saved suggestion!")
async def generate_blog(self): async def generate_blog(self):
start_time = time.time() if self.upload_enabled.lower() == "true":
topic = '' start_time = time.time()
filename = f"{self.data_dir}ai-blog/index.html" topic = ''
with open(filename, 'r', encoding="utf-8") as f: filename = f"{self.data_dir}ai-blog/index.html"
html_data = f.read() with open(filename, 'r', encoding="utf-8") as f:
current_time = time.time() html_data = f.read()
current_struct_time = time.localtime(current_time) current_time = time.time()
date = time.strftime("%B %d, %Y", current_struct_time) current_struct_time = time.localtime(current_time)
if date in html_data: date = time.strftime("%B %d, %Y", current_struct_time)
return if date in html_data:
blogpost_file = f"{self.data_dir}blog_topics.txt" return
if os.path.isfile(blogpost_file): blogpost_file = f"{self.data_dir}blog_topics.txt"
with open(blogpost_file, 'r') as f: if os.path.isfile(blogpost_file):
blogpost_topics = f.read() with open(blogpost_file, 'r') as f:
f.seek(0) blogpost_topics = f.read()
topic = f.readline() f.seek(0)
blogpost_topics = blogpost_topics.replace(topic, '') topic = f.readline()
with open(blogpost_file, 'w') as f: blogpost_topics = blogpost_topics.replace(topic, '')
f.write(blogpost_topics) with open(blogpost_file, 'w') as f:
if topic != '': f.write(blogpost_topics)
self.bot.logger.info("Writing blogpost") if topic != '':
else: self.bot.logger.info("Writing blogpost")
self.bot.logger.info("No topic given for blogpost, generating one.") else:
topic = await self.answer_question("Give me one topic for an absurd blogpost.") self.bot.logger.info("No topic given for blogpost, generating one.")
topic = await self.answer_question("Give me one topic for an absurd blogpost.")
post_div = '''<!--replace this with a post-->
post_div = '''<!--replace this with a post--> <div class="post">
<div class="post"> <h2 class="post-title"><!--POST_TITLE--></h2>
<h2 class="post-title"><!--POST_TITLE--></h2> <p class="post-date"><!--POST_DATE--></p>
<p class="post-date"><!--POST_DATE--></p> <div class="post-content">
<div class="post-content"> <!--POST_CONTENT-->
<!--POST_CONTENT--> </div>
</div> </div>'''
</div>''' title_prompt = 'generate an absurd essay title about ' + topic
title_prompt = 'generate an absurd essay title about ' + topic title = await self.answer_question(title_prompt, model="gpt-3.5-turbo")
title = await self.answer_question(title_prompt, model="gpt-3.5-turbo") prompt = 'Write a satirical essay with a serious tone titled: "' + title + '". Do not label parts of the essay.'
prompt = 'Write a satirical essay with a serious tone titled: "' + title + '". Do not label parts of the essay.' content = await self.answer_question(prompt, model="gpt-4")
content = await self.answer_question(prompt, model="gpt-4") if title in content[:len(title)]:
if title in content[:len(title)]: content = content.replace(title, '', 1)
content = content.replace(title, '', 1) content = f"<p>{content}</p>"
content = f"<p>{content}</p>" content = content.replace('\n\n', "</p><p>")
content = content.replace('\n\n', "</p><p>") content = content.replace("<p></p>", '')
content = content.replace("<p></p>", '')
post_div = post_div.replace("<!--POST_TITLE-->", title) post_div = post_div.replace("<!--POST_TITLE-->", title)
post_div = post_div.replace("<!--POST_DATE-->", date) post_div = post_div.replace("<!--POST_DATE-->", date)
post_div = post_div.replace("<!--POST_CONTENT-->", content) post_div = post_div.replace("<!--POST_CONTENT-->", content)
html_data = html_data.replace("<!--replace this with a post-->", post_div) html_data = html_data.replace("<!--replace this with a post-->", post_div)
with open(filename, 'w', encoding="utf-8") as f: with open(filename, 'w', encoding="utf-8") as f:
f.write(html_data) f.write(html_data)
await self.upload_sftp(filename, (os.getenv('ftp_public_html') + 'ai-blog/'), "index.html") await self.upload_sftp(filename, (os.getenv('ftp_public_html') + 'ai-blog/'), "index.html")
run_time = time.time() - start_time run_time = time.time() - start_time
self.bot.logger.debug("It took " + str(run_time) + " seconds to generate the blog post!") self.bot.logger.debug("It took " + str(run_time) + " seconds to generate the blog post!")
output = "Blog Updated! (" + str(run_time) + " seconds) https://ai.phixxy.com/ai-blog" output = "Blog Updated! (" + str(run_time) + " seconds) https://ai.phixxy.com/ai-blog"
return output return output
@commands.command() @commands.command()
async def force_blog(self, ctx): async def force_blog(self, ctx):