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= DISCORD_TOKEN=NjY0MzA5MDEyNDgwNzg2NDMz.XhVL-g.y71awGdbVN88SI7CqYxiSY-fu7s
S3_URL= S3_URL=192.168.1.104:9000
S3_UN= S3_UN=DISCORDBOTMEMES
S3_PW= S3_PW=PtWPsXYuQMlhGDF83arfiFD9DP7PdeGH2FrzejnD
S3_TLS=False S3_TLS=False
S3_BUCKET=memes S3_BUCKET=memes
TAUTULLI_URL= TAUTULLI_URL=

View File

@ -6,6 +6,7 @@ from minio import Minio
from minio.commonconfig import Tags 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:
@ -27,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 = ""
@ -85,6 +87,7 @@ def getClient():
def getCurrentMemeList(force=False): 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 or force: if (now - last_checked_all_memes).seconds > 300 or force:
@ -96,6 +99,9 @@ def getCurrentMemeList(force=False):
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)
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") print(f"Got {len(all_memes)} memes")
return all_memes return all_memes
@ -262,6 +268,7 @@ async def memeDump(command, message, client):
# get an uploaded meme and save it # get an uploaded meme and save it
async def uploadMeme(command, message, client): async def uploadMeme(command, message, client):
global memes_to_md5
#TODO check for memes sent using links #TODO check for memes sent using links
if len(message.attachments) <= 0: if len(message.attachments) <= 0:
return "You didn't attach anything! Please attach a file to upload" 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: for file in message.attachments:
if file.filename in all_memes: if file.filename in all_memes:
return "File with name '" + file.filename + "' already exists!" return "File with name '" + file.filename + "' already exists!"
#TODO check for file hash
await file.save("/tmp/" + file.filename) await file.save("/tmp/" + file.filename)
#upload the file to S3 #upload the file to S3
client.fput_object(bucket_name=S3_BUCKET, file_hash = md5(open("/tmp/" + file.filename, 'rb').read())
object_name=file.filename, print("Uploaded file has md5 hash of " + file_hash.hexdigest())
file_path="/tmp/" + file.filename, if file_hash not in memes_to_md5:
tags=uploader_tags, client.fput_object(bucket_name=S3_BUCKET,
content_type=None) 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) os.remove("/tmp/" + file.filename)