-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.py
64 lines (52 loc) · 2.42 KB
/
utility.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from dotenv import load_dotenv
from mutagen.easyid3 import EasyID3
load_dotenv()
#Counts how many MP3 files have been uploaded as attachments, returns the value
def count_mp3(attachments):
count = 0
for attachment in attachments:
if attachment.content_type == "audio/mpeg":
count += 1
return count
#Downloads any attachments associated with a message
async def retrieve_attachments(message, args):
mp3_count = count_mp3(message.attachments)
#Can't download MP3s if there aren't any given!
if mp3_count == 0:
await message.channel.send("No MP3 files have been provided, so not going to download")
return
#If the amount of alt filenames and MP3s provided do not match, downloads will go ahead but retain original filenames
if len(args) > 0 and len(args) != mp3_count:
await message.channel.send("Amount of alternative filenames does not match count of MP3s uploaded. For safety, the filenames and metadata of \
any attached audio files will not be changed")
for attachment in message.attachments:
if attachment.content_type == "audio/mpeg":
file = os.path.join(os.getenv("SAVE_PATH")+attachment.filename)
await attachment.save(file)
#If no alt filenames are provided, download but retain original filenames
elif len(args) == 0:
for attachment in message.attachments:
if attachment.content_type == "audio/mpeg":
file = os.path.join(os.getenv("SAVE_PATH")+attachment.filename)
await attachment.save(file)
#If alt filenames are provided and match the amount of MP3s attached, they will be downloaded and renamed in order
else:
x = 0
y = 0
while x < len(message.attachments):
if message.attachments[x].content_type == "audio/mpeg":
#Checks if a .mp3 extension needs to be added to the new filename
if args[y][-4:] == ".mp3":
fname = args[y]
else:
fname = args[y]+".mp3"
#Save 'n' rename
file = os.path.join(os.getenv("SAVE_PATH")+fname)
await message.attachments[x].save(file)
audio = EasyID3(file)
audio["title"] = args[y]
audio.save()
y += 1
x += 1
await message.channel.send("All files succesfully downloaded")