Changed ftp to sftp, plan on removing all aioftp commands later.
This commit is contained in:
parent
8b2683b8e9
commit
6a6863406d
1 changed files with 45 additions and 34 deletions
|
|
@ -17,6 +17,7 @@ from dotenv import load_dotenv
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import aioftp
|
import aioftp
|
||||||
|
import asyncssh
|
||||||
|
|
||||||
#Stable Diffusion
|
#Stable Diffusion
|
||||||
#Set this env variable to http://host:port or "disabled"
|
#Set this env variable to http://host:port or "disabled"
|
||||||
|
|
@ -51,7 +52,7 @@ bot = commands.Bot(command_prefix='!', intents=intents)
|
||||||
brief="Moderation Tools"
|
brief="Moderation Tools"
|
||||||
)
|
)
|
||||||
async def moderate(ctx, filename):
|
async def moderate(ctx, filename):
|
||||||
await upload_ftp("blank_image.png", os.getenv('ftp_ai_images'), filename)
|
await upload_sftp("blank_image.png", os.getenv('ftp_ai_images'), filename)
|
||||||
output = "Image " + filename + " replaced"
|
output = "Image " + filename + " replaced"
|
||||||
await ctx.send(output)
|
await ctx.send(output)
|
||||||
|
|
||||||
|
|
@ -63,6 +64,12 @@ async def upload_ftp(local_filename, server_folder, server_filename):
|
||||||
await client.upload(local_filename, server_folder+server_filename, write_into=True)
|
await client.upload(local_filename, server_folder+server_filename, write_into=True)
|
||||||
await client.quit()
|
await client.quit()
|
||||||
|
|
||||||
|
async def upload_sftp(local_filename, server_folder, server_filename):
|
||||||
|
remotepath = server_folder + server_filename
|
||||||
|
async with asyncssh.connect(ftp_server, username=ftp_username, password=ftp_password) as conn:
|
||||||
|
async with conn.start_sftp_client() as sftp:
|
||||||
|
await sftp.put(local_filename, remotepath=remotepath)
|
||||||
|
|
||||||
async def handle_error(error):
|
async def handle_error(error):
|
||||||
print(error)
|
print(error)
|
||||||
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
||||||
|
|
@ -81,27 +88,17 @@ async def upload_ftp_ai_images(filename, prompt):
|
||||||
</div>'''
|
</div>'''
|
||||||
img_list = []
|
img_list = []
|
||||||
server_folder = os.getenv('ftp_ai_images')
|
server_folder = os.getenv('ftp_ai_images')
|
||||||
client = aioftp.Client()
|
new_filename = str(time.time_ns()) + ".png"
|
||||||
await client.connect(ftp_server)
|
await upload_sftp(filename, server_folder, new_filename)
|
||||||
await client.login(ftp_username, ftp_password)
|
|
||||||
await client.change_directory(server_folder)
|
|
||||||
server_files = await client.list()
|
|
||||||
try:
|
|
||||||
file_count = int(len(server_files))
|
|
||||||
except:
|
|
||||||
file_count = 0
|
|
||||||
new_file_name = str(file_count) + ".png"
|
|
||||||
await client.upload(filename, new_file_name, write_into=True)
|
|
||||||
print("Uploaded", new_file_name)
|
print("Uploaded", new_file_name)
|
||||||
with open(html_file, 'r') as f:
|
with open(html_file, 'r') as f:
|
||||||
html_data = f.read()
|
html_data = f.read()
|
||||||
html_insert = html_insert.replace("<!--filename-->", new_file_name)
|
html_insert = html_insert.replace("<!--filename-->", new_filename)
|
||||||
html_insert = html_insert.replace("<!--description-->", prompt)
|
html_insert = html_insert.replace("<!--description-->", prompt)
|
||||||
html_data = html_data.replace("<!--REPLACE THIS COMMENT-->", html_insert)
|
html_data = html_data.replace("<!--REPLACE THIS COMMENT-->", html_insert)
|
||||||
with open(html_file, "w") as f:
|
with open(html_file, "w") as f:
|
||||||
f.writelines(html_data)
|
f.writelines(html_data)
|
||||||
await client.upload(html_file, "index.html", write_into=True)
|
await upload_sftp(html_file, server_folder, "index.html")
|
||||||
await client.quit()
|
|
||||||
|
|
||||||
def create_channel_config(filepath):
|
def create_channel_config(filepath):
|
||||||
config_dict = {
|
config_dict = {
|
||||||
|
|
@ -604,17 +601,12 @@ async def currency(ctx, arg1=None, arg2=None, arg3=None, arg4=None):
|
||||||
async def meme(ctx):
|
async def meme(ctx):
|
||||||
async def update_meme_webpage(filename):
|
async def update_meme_webpage(filename):
|
||||||
server_folder = os.getenv('ftp_ai_memes')
|
server_folder = os.getenv('ftp_ai_memes')
|
||||||
client = aioftp.Client()
|
|
||||||
await client.connect(ftp_server)
|
|
||||||
await client.login(ftp_username, ftp_password)
|
|
||||||
await client.change_directory(server_folder)
|
|
||||||
server_files = await client.list()
|
|
||||||
try:
|
try:
|
||||||
file_count = len(server_files)
|
file_count = len(server_files)
|
||||||
except:
|
except:
|
||||||
file_count = 0
|
file_count = 0
|
||||||
new_file_name = str(file_count) + ".png"
|
new_file_name = str(time.time_ns()) + ".png"
|
||||||
await client.upload(filename, new_file_name, write_into=True)
|
await upload_sftp(filename, server_folder, new_file_name)
|
||||||
print("Uploaded", new_file_name)
|
print("Uploaded", new_file_name)
|
||||||
with open("phixxy.com/ai-memes/index.html", 'r') as f:
|
with open("phixxy.com/ai-memes/index.html", 'r') as f:
|
||||||
html_data = f.read()
|
html_data = f.read()
|
||||||
|
|
@ -622,8 +614,7 @@ async def meme(ctx):
|
||||||
html_data = html_data.replace('<!--ADD IMG HERE-->',html_insert)
|
html_data = html_data.replace('<!--ADD IMG HERE-->',html_insert)
|
||||||
with open("phixxy.com/ai-memes/index.html", "w") as f:
|
with open("phixxy.com/ai-memes/index.html", "w") as f:
|
||||||
f.writelines(html_data)
|
f.writelines(html_data)
|
||||||
await client.upload("phixxy.com/ai-memes/index.html", "index.html", write_into=True)
|
await upload_sftp("phixxy.com/ai-memes/index.html", server_folder, "index.html")
|
||||||
await client.quit()
|
|
||||||
|
|
||||||
|
|
||||||
async def generate_random_meme(topic):
|
async def generate_random_meme(topic):
|
||||||
|
|
@ -832,7 +823,7 @@ async def generate_blog(ctx):
|
||||||
f.write(html_data)
|
f.write(html_data)
|
||||||
|
|
||||||
|
|
||||||
await upload_ftp(filename, "/media/sdq1/bottlecap/www/phixxy.com/public_html/ai-blog/", "index.html")
|
await upload_sftp(filename, "/media/sdq1/bottlecap/www/phixxy.com/public_html/ai-blog/", "index.html")
|
||||||
run_time = time.time() - start_time
|
run_time = time.time() - start_time
|
||||||
print("It took " + str(run_time) + " seconds to generate the blog post!")
|
print("It took " + str(run_time) + " seconds to generate the blog post!")
|
||||||
output = "Blog Updated! (" + str(run_time) + " seconds) https://phixxy.com/ai-blog"
|
output = "Blog Updated! (" + str(run_time) + " seconds) https://phixxy.com/ai-blog"
|
||||||
|
|
@ -993,7 +984,14 @@ async def website(ctx):
|
||||||
if ".png" in filename:
|
if ".png" in filename:
|
||||||
os.remove(local_folder + filename)
|
os.remove(local_folder + filename)
|
||||||
|
|
||||||
async def delete_ftp_pngs(server_folder):
|
async def delete_sftp_pngs(server_folder):
|
||||||
|
async with asyncssh.connect(ftp_server, username=ftp_username, password=ftp_password) as conn:
|
||||||
|
async with conn.start_sftp_client() as sftp:
|
||||||
|
for filename in (await sftp.listdir(server_folder)):
|
||||||
|
print("Deleting", filename)
|
||||||
|
await sftp.remove(server_folder+filename)
|
||||||
|
|
||||||
|
'''async def delete_ftp_pngs(server_folder):
|
||||||
client = aioftp.Client()
|
client = aioftp.Client()
|
||||||
await client.connect(ftp_server)
|
await client.connect(ftp_server)
|
||||||
await client.login(ftp_username, ftp_password)
|
await client.login(ftp_username, ftp_password)
|
||||||
|
|
@ -1002,7 +1000,7 @@ async def website(ctx):
|
||||||
if ".png" in path.name:
|
if ".png" in path.name:
|
||||||
print("Deleting", path.name)
|
print("Deleting", path.name)
|
||||||
await client.remove(path.name)
|
await client.remove(path.name)
|
||||||
await client.quit()
|
await client.quit()'''
|
||||||
|
|
||||||
async def extract_image_tags(code):
|
async def extract_image_tags(code):
|
||||||
count = code.count("<img")
|
count = code.count("<img")
|
||||||
|
|
@ -1051,22 +1049,35 @@ async def website(ctx):
|
||||||
return code
|
return code
|
||||||
|
|
||||||
|
|
||||||
|
async def upload_sftp(local_filename, server_folder, server_filename):
|
||||||
|
remotepath = server_folder + server_filename
|
||||||
|
async with asyncssh.connect(ftp_server, username=ftp_username, password=ftp_password) as conn:
|
||||||
|
async with conn.start_sftp_client() as sftp:
|
||||||
|
await sftp.put(local_filename, remotepath=remotepath)
|
||||||
|
|
||||||
async def upload_html_and_imgs(local_folder, server_folder):
|
async def upload_html_and_imgs(local_folder, server_folder):
|
||||||
client = aioftp.Client()
|
'''client = aioftp.Client()
|
||||||
await client.connect(ftp_server)
|
await client.connect(ftp_server)
|
||||||
await client.login(ftp_username, ftp_password)
|
await client.login(ftp_username, ftp_password)
|
||||||
await client.change_directory(server_folder)
|
await client.change_directory(server_folder)'''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for filename in os.listdir(local_folder):
|
for filename in os.listdir(local_folder):
|
||||||
if ".png" in filename:
|
if ".png" in filename:
|
||||||
await client.upload(local_folder + filename, filename, write_into=True)
|
#await client.upload(local_folder + filename, filename, write_into=True)
|
||||||
|
async with asyncssh.connect(ftp_server, username=ftp_username, password=ftp_password) as conn:
|
||||||
|
async with conn.start_sftp_client() as sftp:
|
||||||
|
remotepath = server_folder + filename
|
||||||
|
await sftp.put(filename, remotepath=remotepath)
|
||||||
#explicitly upload html files last!
|
#explicitly upload html files last!
|
||||||
for filename in os.listdir(local_folder):
|
for filename in os.listdir(local_folder):
|
||||||
if ".html" in filename:
|
if ".html" in filename:
|
||||||
await client.upload(local_folder + filename, filename, write_into=True)
|
#await client.upload(local_folder + filename, filename, write_into=True)
|
||||||
await client.quit()
|
async with asyncssh.connect(ftp_server, username=ftp_username, password=ftp_password) as conn:
|
||||||
|
async with conn.start_sftp_client() as sftp:
|
||||||
|
remotepath = server_folder + filename
|
||||||
|
await sftp.put(filename, remotepath=remotepath)
|
||||||
|
|
||||||
|
|
||||||
server_folder = os.getenv('ftp_ai_webpage')
|
server_folder = os.getenv('ftp_ai_webpage')
|
||||||
|
|
@ -1081,7 +1092,7 @@ async def website(ctx):
|
||||||
await ctx.send("Please wait, this will take a long time! You will be able to view the website here: https://phixxy.com/ai-webpage/")
|
await ctx.send("Please wait, this will take a long time! You will be able to view the website here: https://phixxy.com/ai-webpage/")
|
||||||
with open(working_file, "w") as f:
|
with open(working_file, "w") as f:
|
||||||
f.write("<!DOCTYPE html><html><head><script>setTimeout(function(){location.reload();}, 10000);</script><title>Generating Website</title><style>body {font-size: 24px;text-align: center;margin-top: 100px;}</style></head><body><p>This webpage is currently being generated. The page will refresh once it is complete. Please be patient.</p></body></html>")
|
f.write("<!DOCTYPE html><html><head><script>setTimeout(function(){location.reload();}, 10000);</script><title>Generating Website</title><style>body {font-size: 24px;text-align: center;margin-top: 100px;}</style></head><body><p>This webpage is currently being generated. The page will refresh once it is complete. Please be patient.</p></body></html>")
|
||||||
await upload_ftp(working_file, server_folder, "index.html")
|
await upload_sftp(working_file, server_folder, "index.html")
|
||||||
topic = ctx.message.content.split(" ", maxsplit=1)[1]
|
topic = ctx.message.content.split(" ", maxsplit=1)[1]
|
||||||
prompt = "Generate a webpage using html and inline css. The webpage topic should be " + topic + ". Feel free to add image tags with alt text. Leave the image source blank. The images will be added later."
|
prompt = "Generate a webpage using html and inline css. The webpage topic should be " + topic + ". Feel free to add image tags with alt text. Leave the image source blank. The images will be added later."
|
||||||
code = await answer_question(prompt)
|
code = await answer_question(prompt)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue