From 9292869520b95ec8e3d9adee78ba8212ca9250b9 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Thu, 1 Dec 2022 18:05:58 -0500 Subject: [PATCH] Save progress --- Dockerfile | 9 +++++++++ docker-compose.yml | 10 ++++++++++ src/.env | 8 ++++---- src/memes.py | 23 +++++++++++++++++------ 4 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..71f5805 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python + +WORKDIR /usr/src/app + +COPY ./src ./ + +RUN pip install -r requirements.txt + +CMD "python" "./main.py" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..26b59ad --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +version: '3' + bot: + image: bot + environment: + - DISCORD_TOKEN=token + - S3_URL=url + - S3_UN=UN + - S3_PW=PW + - S3_TLS=TLS + - S3_BUCKET=BUCKET diff --git a/src/.env b/src/.env index 11e554d..4ac624c 100644 --- a/src/.env +++ b/src/.env @@ -1,7 +1,7 @@ -DISCORD_TOKEN= -S3_URL= -S3_UN= -S3_PW= +DISCORD_TOKEN=NjY0MzA5MDEyNDgwNzg2NDMz.XhVL-g.y71awGdbVN88SI7CqYxiSY-fu7s +S3_URL=192.168.1.104:9000 +S3_UN=DISCORDBOTMEMES +S3_PW=PtWPsXYuQMlhGDF83arfiFD9DP7PdeGH2FrzejnD S3_TLS=False S3_BUCKET=memes TAUTULLI_URL= diff --git a/src/memes.py b/src/memes.py index e8c9c23..0984055 100644 --- a/src/memes.py +++ b/src/memes.py @@ -6,6 +6,7 @@ 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: @@ -27,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 = "" @@ -85,6 +87,7 @@ def getClient(): 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 or force: @@ -96,6 +99,9 @@ def getCurrentMemeList(force=False): if not obj.is_dir: #print(f'{obj.object_name}') all_memes.append(obj.object_name) + if obj.object_name == 'combat_shotgun_my_beloved1.png': + print("Found! " + obj.etag) + memes_to_md5[obj.etag] = obj.object_name print(f"Got {len(all_memes)} memes") return all_memes @@ -262,6 +268,7 @@ async def memeDump(command, message, client): # 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" @@ -277,16 +284,20 @@ async def uploadMeme(command, message, client): for file in message.attachments: if file.filename in all_memes: return "File with name '" + file.filename + "' already exists!" - #TODO check for file hash await file.save("/tmp/" + file.filename) #upload the file to S3 - client.fput_object(bucket_name=S3_BUCKET, - object_name=file.filename, - file_path="/tmp/" + file.filename, - tags=uploader_tags, - content_type=None) + file_hash = md5(open("/tmp/" + file.filename, 'rb').read()) + print("Uploaded file has md5 hash of " + file_hash.hexdigest()) + if file_hash not in memes_to_md5: + client.fput_object(bucket_name=S3_BUCKET, + object_name=file.filename, + file_path="/tmp/" + file.filename, + tags=uploader_tags, + content_type=None) + else: + return "Found that meme already! (aka " + memes_to_md5[file_hash] + ")" os.remove("/tmp/" + file.filename)