more meme cleanup and added a function to upload memes to website

This commit is contained in:
phixxy 2024-01-17 19:46:10 -08:00
parent b2fb09e681
commit 60d12e388c
2 changed files with 31 additions and 17 deletions

View file

@ -43,20 +43,6 @@ async def handle_error(error):
brief="Generate a meme" brief="Generate a meme"
) )
async def meme(ctx): async def meme(ctx):
'''async def update_meme_webpage(filename):
server_folder = (os.getenv('ftp_public_html') + 'ai-memes/')
new_file_name = str(time.time_ns()) + ".png"
await upload_sftp(filename, server_folder, new_file_name)
print("Uploaded", new_file_name)
with open("phixxy.com/ai-memes/index.html", 'r') as f:
html_data = f.read()
html_insert = '<!--ADD IMG HERE-->\n <img src="' + new_file_name + '" loading="lazy">'
html_data = html_data.replace('<!--ADD IMG HERE-->',html_insert)
with open("phixxy.com/ai-memes/index.html", "w") as f:
f.writelines(html_data)
await upload_sftp("phixxy.com/ai-memes/index.html", server_folder, "index.html")'''
async def generate_random_meme(topic): async def generate_random_meme(topic):
http_session = aiohttp.ClientSession() http_session = aiohttp.ClientSession()
async with http_session.get('https://api.imgflip.com/get_memes') as resp: async with http_session.get('https://api.imgflip.com/get_memes') as resp:

View file

@ -237,7 +237,7 @@ async def chat_response(ctx, channel_vars, chat_history_string):
await handle_error(error) await handle_error(error)
async def folder_setup(): async def folder_setup():
folder_names = ["tmp", "tmp/sfw", "tmp/nsfw", "tmp/meme/", "channels", "users", "channels/config", "channels/logs", "databases", "databases/currency", "databases/currency/players"] folder_names = ["tmp", "tmp/sfw", "tmp/nsfw", "tmp/meme", "channels", "users", "channels/config", "channels/logs", "databases", "databases/currency", "databases/currency/players"]
for folder_name in folder_names: for folder_name in folder_names:
if not os.path.exists(folder_name): if not os.path.exists(folder_name):
os.mkdir(folder_name) os.mkdir(folder_name)
@ -261,11 +261,25 @@ async def delete_derp_files(server_folder):
await sftp.remove(server_folder+filename) await sftp.remove(server_folder+filename)
except: except:
print("Couldn't delete", filename) print("Couldn't delete", filename)
async def meme_handler(folder):
if folder[:-1] != '/':
folder += '/'
for file in os.listdir(folder):
filepath = folder + file
await update_meme_webpage(filepath)
@tasks.loop(seconds=1) # Run the task every second @tasks.loop(seconds=1) # Run the task every second
async def task_loop(): async def task_loop():
current_time = time.localtime() current_time = time.localtime()
#Run every minute
if current_time.tm_sec == 0:
await meme_handler('tmp/meme/')
#Run daily tasks #Run daily tasks
if current_time.tm_hour == 17 and current_time.tm_min == 0 and current_time.tm_sec == 0: if current_time.tm_hour == 17 and current_time.tm_min == 0 and current_time.tm_sec == 0:
bot_stuff = bot.get_channel(544408659174883328) bot_stuff = bot.get_channel(544408659174883328)
@ -481,7 +495,21 @@ async def generate_blog(ctx):
#for subscriber in blog_subscribers: #for subscriber in blog_subscribers:
# output += '<@' + subscriber + '> ' # output += '<@' + subscriber + '> '
await ctx.send(output) await ctx.send(output)
async def update_meme_webpage(filename):
server_folder = (os.getenv('ftp_public_html') + 'ai-memes/')
new_file_name = str(time.time_ns()) + ".png"
await upload_sftp(filename, server_folder, new_file_name)
print("Uploaded", new_file_name)
with open("phixxy.com/ai-memes/index.html", 'r') as f:
html_data = f.read()
html_insert = '<!--ADD IMG HERE-->\n <img src="' + new_file_name + '" loading="lazy">'
html_data = html_data.replace('<!--ADD IMG HERE-->',html_insert)
with open("phixxy.com/ai-memes/index.html", "w") as f:
f.writelines(html_data)
await upload_sftp("phixxy.com/ai-memes/index.html", server_folder, "index.html")
os.rename(filename, f'tmp/{filename}')
@bot.command( @bot.command(
description="Question", description="Question",