From 8d0389372dd02a371843ffebdc98dfb4468a1f1a Mon Sep 17 00:00:00 2001
From: phixxy
Date: Sun, 28 Jan 2024 22:32:01 -0800
Subject: [PATCH] added env variable to run the extension or not
---
extensions/phixxycom.py | 172 ++++++++++++++++++++--------------------
1 file changed, 87 insertions(+), 85 deletions(-)
diff --git a/extensions/phixxycom.py b/extensions/phixxycom.py
index b77a919..afca2b2 100644
--- a/extensions/phixxycom.py
+++ b/extensions/phixxycom.py
@@ -11,6 +11,7 @@ class PhixxyCom(commands.Cog):
def __init__(self, bot):
self.bot = bot
+ self.upload_enabled = os.getenv('upload_phixxy')
self.SERVER = os.getenv('ftp_server')
self.USERNAME = os.getenv('ftp_username')
self.PASSWORD = os.getenv('ftp_password')
@@ -41,10 +42,11 @@ class PhixxyCom(commands.Cog):
return "Unknown Prompt"
async def upload_sftp(self, local_filename, server_folder, server_filename):
- remotepath = server_folder + server_filename
- async with asyncssh.connect(self.SERVER, username=self.USERNAME, password=self.PASSWORD) as conn:
- async with conn.start_sftp_client() as sftp:
- await sftp.put(local_filename, remotepath=remotepath)
+ if self.upload_enabled.lower() == "true":
+ remotepath = server_folder + server_filename
+ async with asyncssh.connect(self.SERVER, username=self.USERNAME, password=self.PASSWORD) as conn:
+ async with conn.start_sftp_client() as sftp:
+ await sftp.put(local_filename, remotepath=remotepath)
async def delete_local_pngs(self, 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):
try:
- for folder in ai_dict:
- for filename in os.listdir(folder):
- if filename[-4:] == '.png':
- filepath = folder + filename
- self.bot.logger.info(f"Found file = {filename}")
- prompt = self.find_prompt_from_filename(ai_dict[folder], filename)
- self.bot.logger.info(f"Found prompt = {prompt}")
- html_file = f"{self.data_dir}ai-images/index.html"
- html_insert = '''
-
-

-
-
'''
- server_folder = (os.getenv('ftp_public_html') + 'ai-images/')
- new_filename = str(time.time_ns()) + ".png"
- await self.upload_sftp(filepath, server_folder, new_filename)
- self.bot.logger.info(f"Uploaded {new_filename}")
- with open(html_file, 'r') as f:
- html_data = f.read()
- html_insert = html_insert.replace("", new_filename)
- html_insert = html_insert.replace("", prompt)
- html_data = html_data.replace("", html_insert)
- with open(html_file, "w") as f:
- f.writelines(html_data)
- await self.upload_sftp(html_file, server_folder, "index.html")
- os.rename(filepath, f"tmp/{new_filename}")
+ if self.upload_enabled.lower() == "true":
+ for folder in ai_dict:
+ for filename in os.listdir(folder):
+ if filename[-4:] == '.png':
+ filepath = folder + filename
+ self.bot.logger.info(f"Found file = {filename}")
+ prompt = self.find_prompt_from_filename(ai_dict[folder], filename)
+ self.bot.logger.info(f"Found prompt = {prompt}")
+ html_file = f"{self.data_dir}ai-images/index.html"
+ html_insert = '''
+
+

+
+
'''
+ server_folder = (os.getenv('ftp_public_html') + 'ai-images/')
+ new_filename = str(time.time_ns()) + ".png"
+ await self.upload_sftp(filepath, server_folder, new_filename)
+ self.bot.logger.info(f"Uploaded {new_filename}")
+ with open(html_file, 'r') as f:
+ html_data = f.read()
+ html_insert = html_insert.replace("", new_filename)
+ html_insert = html_insert.replace("", prompt)
+ html_data = html_data.replace("", html_insert)
+ with open(html_file, "w") as f:
+ f.writelines(html_data)
+ 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")
@@ -220,62 +223,61 @@ class PhixxyCom(commands.Cog):
await ctx.send("Saved suggestion!")
async def generate_blog(self):
- start_time = time.time()
- topic = ''
- filename = f"{self.data_dir}ai-blog/index.html"
- with open(filename, 'r', encoding="utf-8") as f:
- html_data = f.read()
- current_time = time.time()
- current_struct_time = time.localtime(current_time)
- date = time.strftime("%B %d, %Y", current_struct_time)
- if date in html_data:
- return
- blogpost_file = f"{self.data_dir}blog_topics.txt"
- if os.path.isfile(blogpost_file):
- with open(blogpost_file, 'r') as f:
- blogpost_topics = f.read()
- f.seek(0)
- topic = f.readline()
- blogpost_topics = blogpost_topics.replace(topic, '')
- with open(blogpost_file, 'w') as f:
- f.write(blogpost_topics)
- if topic != '':
- self.bot.logger.info("Writing blogpost")
- else:
- 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 = '''
- '''
- title_prompt = 'generate an absurd essay title about ' + topic
- 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.'
- content = await self.answer_question(prompt, model="gpt-4")
- if title in content[:len(title)]:
- content = content.replace(title, '', 1)
- content = f"{content}
"
- content = content.replace('\n\n', "
")
- content = content.replace("
", '')
+ if self.upload_enabled.lower() == "true":
+ start_time = time.time()
+ topic = ''
+ filename = f"{self.data_dir}ai-blog/index.html"
+ with open(filename, 'r', encoding="utf-8") as f:
+ html_data = f.read()
+ current_time = time.time()
+ current_struct_time = time.localtime(current_time)
+ date = time.strftime("%B %d, %Y", current_struct_time)
+ if date in html_data:
+ return
+ blogpost_file = f"{self.data_dir}blog_topics.txt"
+ if os.path.isfile(blogpost_file):
+ with open(blogpost_file, 'r') as f:
+ blogpost_topics = f.read()
+ f.seek(0)
+ topic = f.readline()
+ blogpost_topics = blogpost_topics.replace(topic, '')
+ with open(blogpost_file, 'w') as f:
+ f.write(blogpost_topics)
+ if topic != '':
+ self.bot.logger.info("Writing blogpost")
+ else:
+ 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 = '''
+ '''
+ title_prompt = 'generate an absurd essay title about ' + topic
+ 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.'
+ content = await self.answer_question(prompt, model="gpt-4")
+ if title in content[:len(title)]:
+ content = content.replace(title, '', 1)
+ content = f"{content}
"
+ content = content.replace('\n\n', "")
+ content = content.replace("
", '')
- post_div = post_div.replace("", title)
- post_div = post_div.replace("", date)
- post_div = post_div.replace("", content)
-
- html_data = html_data.replace("", post_div)
- with open(filename, 'w', encoding="utf-8") as f:
- f.write(html_data)
- await self.upload_sftp(filename, (os.getenv('ftp_public_html') + 'ai-blog/'), "index.html")
- run_time = time.time() - start_time
- 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"
- return output
+ post_div = post_div.replace("", title)
+ post_div = post_div.replace("", date)
+ post_div = post_div.replace("", content)
+
+ html_data = html_data.replace("", post_div)
+ with open(filename, 'w', encoding="utf-8") as f:
+ f.write(html_data)
+ await self.upload_sftp(filename, (os.getenv('ftp_public_html') + 'ai-blog/'), "index.html")
+ run_time = time.time() - start_time
+ 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"
+ return output
@commands.command()
async def force_blog(self, ctx):