Add ability to upload #2

Merged
tyler merged 10 commits from uploading into main 2022-12-18 02:55:41 +00:00
5 changed files with 62 additions and 14 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
src/.env
venv
apply_environment.sh

View File

@ -15,7 +15,7 @@ Features
- [X] Fuzzy search for memes
- [X] Get a random meme
- [X] Get a set of memes
- [ ] Upload memes to bucket
- [X] Upload memes to bucket
- [ ] Check status of Plex instance
Enviroment vars

View File

@ -1,8 +0,0 @@
DISCORD_TOKEN=
S3_URL=
S3_UN=
S3_PW=
S3_TLS=False
S3_BUCKET=memes
TAUTULLI_URL=
TAUTULLI_APIKEY=

View File

@ -3,8 +3,10 @@ import os, random, io
from datetime import datetime
from fuzzywuzzy import fuzz, process
from minio import Minio
from minio.commonconfig import Tags
from minio.error import S3Error
from catbox import Uploader
from hashlib import md5
# memes we have most recently gotten
class RecentMemeQueue:
@ -26,6 +28,7 @@ recentMemes = RecentMemeQueue()
# list of all current memes' names
all_memes = []
memes_to_md5 = dict()
last_checked_all_memes = datetime.strptime("2000-01-01 01:01:01", "%Y-%m-%d %H:%M:%S")
S3_URL = ""
@ -81,20 +84,25 @@ def getClient():
# Helper methods
# Methods to break up the calldict methods
def getCurrentMemeList():
def getCurrentMemeList(force=False):
global last_checked_all_memes
global all_memes
global memes_to_md5
now = datetime.now()
# if no update in the past 5 mins
if (now - last_checked_all_memes).seconds > 300:
if (now - last_checked_all_memes).seconds > 300 or force:
print("Enough time has elapsed, refreshing meme cache...")
last_checked_all_memes = now
all_memes.clear()
memes_to_md5.clear()
myClient = getClient()
for obj in myClient.list_objects(S3_BUCKET):
if not obj.is_dir:
#print(f'{obj.object_name}')
all_memes.append(obj.object_name)
memes_to_md5[obj.etag] = obj.object_name
print(f"Got {len(all_memes)} memes")
return all_memes
@ -258,3 +266,45 @@ async def memeDump(command, message, client):
else:
await message.channel.send(file=ready_meme)
return "Enjoy your memes :)"
# get an uploaded meme and save it
async def uploadMeme(command, message, client):
global memes_to_md5
#TODO check for memes sent using links
if len(message.attachments) <= 0:
return "You didn't attach anything! Please attach a file to upload"
uploader_tags = Tags.new_object_tags()
uploader_tags["uploader"] = message.author.name
all_memes = getCurrentMemeList()
client = getClient()
file_names = []
for file in message.attachments:
if file.filename in all_memes:
return "File with name '" + file.filename + "' already exists!"
await file.save("/tmp/" + file.filename)
result = client.fput_object(bucket_name=S3_BUCKET,
object_name=file.filename,
file_path="/tmp/" + file.filename,
tags=uploader_tags,
content_type=None)
if result.etag in memes_to_md5:
print("Already have that meme, etag is (" + result.etag + ")")
client.remove_object(bucket_name=S3_BUCKET,
object_name=file.filename)
return "That meme is already in the cache (it's called " + memes_to_md5[result.etag] + ")"
os.remove("/tmp/" + file.filename)
getCurrentMemeList(force=True)
return "Thanks got your memes!"

View File

@ -1,7 +1,7 @@
#!/usr/bin/python3
import requests
import os, random, sys
from memes import parseMeme, memeCount, memeDump, allMemes
from memes import parseMeme, memeCount, memeDump, allMemes, uploadMeme
import discord
async def showHelp(very, useless, arguments):
@ -28,6 +28,10 @@ calldict = {
"!memedump" : memeDump,
"!allmemes" : allMemes,
"!help" : showHelp,
"!up" : uploadMeme,
"!upload" : uploadMeme,
"!uploadmeme": uploadMeme,
#"!memeblame" : memeBlame
# plex
#"!plexleaderboard" : getTopUsers,
}
@ -36,11 +40,12 @@ calldict = {
# parse the message
async def parse_message(client, message):
command = message.content.split()
cmd_input = command[0].lower()
# if i know this command
if command[0] in calldict.keys():
if cmd_input in calldict.keys():
print(f'{client.user} was passed a command I know! ({message.content})')
result = await calldict[command[0]](command, message, client)
result = await calldict[cmd_input](command, message, client)
if isinstance(result, discord.File):
print(f'{client.user} is replying with a file ({result.filename})')
return await message.channel.send(file=result)