Add ability to upload #2

Merged
tyler merged 10 commits from uploading into main 2022-12-18 02:55:41 +00:00
4 changed files with 40 additions and 10 deletions
Showing only changes of commit 9292869520 - Show all commits

9
Dockerfile Normal file
View File

@ -0,0 +1,9 @@
FROM python
WORKDIR /usr/src/app
COPY ./src ./
RUN pip install -r requirements.txt
CMD "python" "./main.py"

10
docker-compose.yml Normal file
View File

@ -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

View File

@ -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=

View File

@ -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
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)