Merge pull request 'Add ability to upload' (#2) from uploading into main
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #2
This commit is contained in:
Tyler Perkins 2022-12-18 02:55:41 +00:00
commit 7f03b290b3
5 changed files with 62 additions and 14 deletions

1
.gitignore vendored
View File

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

View File

@ -15,7 +15,7 @@ Features
- [X] Fuzzy search for memes - [X] Fuzzy search for memes
- [X] Get a random meme - [X] Get a random meme
- [X] Get a set of memes - [X] Get a set of memes
- [ ] Upload memes to bucket - [X] Upload memes to bucket
- [ ] Check status of Plex instance - [ ] Check status of Plex instance
Enviroment vars 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 datetime import datetime
from fuzzywuzzy import fuzz, process from fuzzywuzzy import fuzz, process
from minio import Minio from minio import Minio
from minio.commonconfig import Tags
from minio.error import S3Error from minio.error import S3Error
from catbox import Uploader from catbox import Uploader
from hashlib import md5
# memes we have most recently gotten # memes we have most recently gotten
class RecentMemeQueue: class RecentMemeQueue:
@ -26,6 +28,7 @@ recentMemes = RecentMemeQueue()
# list of all current memes' names # list of all current memes' names
all_memes = [] all_memes = []
memes_to_md5 = dict()
last_checked_all_memes = datetime.strptime("2000-01-01 01:01:01", "%Y-%m-%d %H:%M:%S") last_checked_all_memes = datetime.strptime("2000-01-01 01:01:01", "%Y-%m-%d %H:%M:%S")
S3_URL = "" S3_URL = ""
@ -81,20 +84,25 @@ def getClient():
# Helper methods # Helper methods
# Methods to break up the calldict methods # Methods to break up the calldict methods
def getCurrentMemeList(): def getCurrentMemeList(force=False):
global last_checked_all_memes global last_checked_all_memes
global all_memes global all_memes
global memes_to_md5
now = datetime.now() now = datetime.now()
# if no update in the past 5 mins # 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...") print("Enough time has elapsed, refreshing meme cache...")
last_checked_all_memes = now last_checked_all_memes = now
all_memes.clear() all_memes.clear()
memes_to_md5.clear()
myClient = getClient() myClient = getClient()
for obj in myClient.list_objects(S3_BUCKET): for obj in myClient.list_objects(S3_BUCKET):
if not obj.is_dir: if not obj.is_dir:
#print(f'{obj.object_name}') #print(f'{obj.object_name}')
all_memes.append(obj.object_name) all_memes.append(obj.object_name)
memes_to_md5[obj.etag] = obj.object_name
print(f"Got {len(all_memes)} memes") print(f"Got {len(all_memes)} memes")
return all_memes return all_memes
@ -258,3 +266,45 @@ async def memeDump(command, message, client):
else: else:
await message.channel.send(file=ready_meme) await message.channel.send(file=ready_meme)
return "Enjoy your memes :)" 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 #!/usr/bin/python3
import requests import requests
import os, random, sys import os, random, sys
from memes import parseMeme, memeCount, memeDump, allMemes from memes import parseMeme, memeCount, memeDump, allMemes, uploadMeme
import discord import discord
async def showHelp(very, useless, arguments): async def showHelp(very, useless, arguments):
@ -28,6 +28,10 @@ calldict = {
"!memedump" : memeDump, "!memedump" : memeDump,
"!allmemes" : allMemes, "!allmemes" : allMemes,
"!help" : showHelp, "!help" : showHelp,
"!up" : uploadMeme,
"!upload" : uploadMeme,
"!uploadmeme": uploadMeme,
#"!memeblame" : memeBlame
# plex # plex
#"!plexleaderboard" : getTopUsers, #"!plexleaderboard" : getTopUsers,
} }
@ -36,11 +40,12 @@ calldict = {
# parse the message # parse the message
async def parse_message(client, message): async def parse_message(client, message):
command = message.content.split() command = message.content.split()
cmd_input = command[0].lower()
# if i know this command # 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})') 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): if isinstance(result, discord.File):
print(f'{client.user} is replying with a file ({result.filename})') print(f'{client.user} is replying with a file ({result.filename})')
return await message.channel.send(file=result) return await message.channel.send(file=result)