finished ftp async swap

This commit is contained in:
phixxy 2023-07-09 18:02:15 -07:00
parent 3c1dd474dd
commit a5d199d88a

View file

@ -17,7 +17,6 @@ import subprocess
import math import math
from PIL import Image, PngImagePlugin from PIL import Image, PngImagePlugin
from dotenv import load_dotenv from dotenv import load_dotenv
from ftplib import FTP
import threading import threading
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import aiohttp import aiohttp
@ -50,13 +49,15 @@ intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents) bot = commands.Bot(command_prefix='!', intents=intents)
#discord stuff END #discord stuff END
async def upload_ftp(local_filename, server_folder, server_filename): async def upload_ftp(local_filename, server_folder, server_filename):
with FTP(ftp_server) as ftp: client = aioftp.Client()
ftp.login(ftp_username, ftp_password) await client.connect(ftp_server)
ftp.cwd(ftp_public_html) await client.login(ftp_username, ftp_password)
ftp.cwd(server_folder) await client.change_directory(server_folder)
server_filename = server_filename await client.upload(local_filename, server_folder+server_filename, write_into=True)
ftp.storbinary("STOR " + server_filename, open(local_filename, "rb")) await client.quit()
async def upload_ftp_ai_images(filename, prompt): async def upload_ftp_ai_images(filename, prompt):
html_file = "phixxy.com/ai-images/index.html" html_file = "phixxy.com/ai-images/index.html"
@ -66,26 +67,28 @@ async def upload_ftp_ai_images(filename, prompt):
<p class="image-description"><!--description--></p> <p class="image-description"><!--description--></p>
</div>''' </div>'''
img_list = [] img_list = []
with FTP(ftp_server) as ftp: server_folder = os.getenv('ftp_ai_images')
ftp.login(ftp_username, ftp_password) client = aioftp.Client()
ftp.cwd(ftp_ai_images) await client.connect(ftp_server)
server_files = ftp.nlst(ftp_ai_images) await client.login(ftp_username, ftp_password)
server_files.sort() await client.change_directory(server_folder)
try: server_files = await client.list()
file_count = int(len(server_files)) try:
except: file_count = int(len(server_files))
file_count = 0 except:
new_file_name = str(file_count) + ".png" file_count = 0
ftp.storbinary("STOR " + new_file_name, open(filename, "rb")) new_file_name = str(file_count) + ".png"
print("Uploaded", new_file_name) await client.upload(filename, new_file_name, write_into=True)
with open(html_file, 'r') as f: print("Uploaded", new_file_name)
html_data = f.read() with open(html_file, 'r') as f:
html_insert = html_insert.replace("<!--filename-->", new_file_name) html_data = f.read()
html_insert = html_insert.replace("<!--description-->", prompt) html_insert = html_insert.replace("<!--filename-->", new_file_name)
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:
ftp.storbinary("STOR " + "index.html", open(html_file, "rb")) f.writelines(html_data)
await client.upload(html_file, "index.html", write_into=True)
await client.quit()
def create_channel_config(filepath): def create_channel_config(filepath):
config_dict = { config_dict = {
@ -510,70 +513,28 @@ async def currency(ctx, arg1=None, arg2=None, arg3=None, arg4=None):
@bot.command() @bot.command()
async def meme(ctx): async def meme(ctx):
async def update_meme_webpage(filename): async def update_meme_webpage(filename):
output_file = "index.html" server_folder = os.getenv('ftp_ai_memes')
html = '''<!DOCTYPE html> client = aioftp.Client()
<html> await client.connect(ftp_server)
<head> await client.login(ftp_username, ftp_password)
<title>ai-memes</title> await client.change_directory(server_folder)
<style type="text/css"> server_files = await client.list()
body { try:
font-family: Arial, sans-serif; file_count = len(server_files)
background-color: #242424; /* dark gray */ except:
color: #fff; /* white */ file_count = 0
margin: 0; new_file_name = str(file_count) + ".png"
padding: 0; await client.upload(filename, new_file_name, write_into=True)
} print("Uploaded", new_file_name)
h1 { with open("phixxy.com/ai-memes/index.html", 'r') as f:
text-align: center; html_data = f.read()
margin-top: 50px; html_insert = '<!--ADD IMG HERE-->\n <img src="' + new_file_name + '" loading="lazy">'
} html_data = html_data.replace('<!--ADD IMG HERE-->',html_insert)
p { with open("phixxy.com/ai-memes/index.html", "w") as f:
text-align: center; f.writelines(html_data)
} await client.upload("phixxy.com/ai-memes/index.html", "index.html", write_into=True)
.container { #ftp.storbinary("STOR " + "index.html", open("phixxy.com/ai-memes/index.html", "rb"))
max-width: 1200px; await client.quit()
margin: 0 auto;
padding: 50px;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
grid-gap: 20px;
}
img {
width: 100%;
height: auto;
border-radius: 10px;
}
</style>
</head>
<body>
<h1>ai-memes</h1>
<p>AI generated memes using chatgpt and imgflip</p>
<div class="container">
'''
html_end = ''' </div>
</body>
</html>'''
img_list = []
with FTP(ftp_server) as ftp:
ftp.login(ftp_username, ftp_password)
ftp.cwd(ftp_ai_memes)
server_files = ftp.nlst(ftp_ai_memes)
server_files.sort()
try:
file_count = int(server_files[-2][-11:-4])+1
except:
file_count = 0
new_file_name = str(file_count).zfill(7) + ".png"
ftp.storbinary("STOR " + new_file_name, open(filename, "rb"))
print("Uploaded", new_file_name)
for x in range(0,file_count+1):
img_list.append("<img src=\"" + str(x).zfill(7) + ".png\">\n")
for line in reversed(img_list):
html += line
html += html_end
with open(output_file, "w") as f:
f.writelines(html)
ftp.storbinary("STOR " + output_file, open(output_file, "rb"))
async def generate_random_meme(topic): async def generate_random_meme(topic):
userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) \ userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
@ -908,6 +869,7 @@ async def website(ctx):
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()
server_folder = os.getenv('ftp_ai_webpage') server_folder = os.getenv('ftp_ai_webpage')
@ -1236,12 +1198,14 @@ async def describe(ctx):
shutil.copyfileobj(r.raw, out_file) shutil.copyfileobj(r.raw, out_file)
img_link = my_open_img_file(imageName) img_link = my_open_img_file(imageName)
try:
url = STABLE_DIFFUSION_URL url = STABLE_DIFFUSION_URL
payload = {"image": img_link} payload = {"image": img_link}
response = requests.post(url=f"{url}/sdapi/v1/interrogate", json=payload) response = requests.post(url=f"{url}/sdapi/v1/interrogate", json=payload)
r = response.json() r = response.json()
await ctx.send(r.get("caption")) await ctx.send(r.get("caption"))
except:
await ctx.send("My image generation service may not be running.")
@bot.command() @bot.command()
async def reimagine(ctx): async def reimagine(ctx):