Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved: Read media info from tags #30

Merged
merged 8 commits into from Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
env/
attachments/
.idea/
venv/
__pycache__/
28 changes: 20 additions & 8 deletions main.py
Expand Up @@ -16,6 +16,7 @@
TextChannel,
VoiceClient,
)
from tinytag import TinyTag

# SETTINGS
# How many seconds to wait in-between songs
Expand Down Expand Up @@ -99,13 +100,25 @@ async def on_message(message: Message):

# Take a filename as string and return it formatted nicely
def format_filename(filename: str):
# Remove file extension
filename = path.splitext(filename)[0]

# Replace all underscores with spaces
filename = filename.replace("_", " ")
# Get all the tags for a track
audio = TinyTag.get(
str(os.path.join(f"{attachment_directory_filepath}", f"{filename}"))
)
content = ""
# If the tag does not exist or is whitespace display the file name only
# Otherwise display in the format @user: <Artist-tag> - <Title-tag>
if audio.artist is not None and not audio.artist.isspace():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if audio.artist is not None and not audio.artist.isspace():
if audio.artist is not None and audio.artist.strip():

content = content + f"{str(audio.artist)} - "

if audio.title is not None and not audio.title.isspace():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if audio.title is not None and not audio.title.isspace():
if audio.title is not None and audio.title.strip():

content = content + f"{str(audio.title)}"
# If the title tag does not exist but the artist tag exists, display the file name along with artist tag
else:
filename = path.splitext(filename)[0]
content = content + filename.replace("_", " ")

return discord.utils.escape_markdown(filename)
return discord.utils.escape_markdown(content)


async def command_stop():
Expand Down Expand Up @@ -230,7 +243,7 @@ async def inner_f():

# Build and send "Now Playing" embed
embed_title = f"{random_emoji} Now Playing {random_emoji}"
list_format = "{0} - [{1}]({2}) [`↲jump`]({3})"
list_format = "{0}: [{1}]({2}) [`↲jump`]({3})"
embed_content = list_format.format(
submit_message.author.mention,
format_filename(attachment.filename),
Expand Down Expand Up @@ -278,7 +291,7 @@ async def command_list(message: Message):
attachment,
local_filepath,
) in enumerate(channel_media_attachments):
list_format = "**{0}.** {1} - [{2}]({3}) [`↲jump`]({4})\n"
list_format = "**{0}.** {1}: [{2}]({3}) [`↲jump`]({4})\n"
embed_content += list_format.format(
index + 1,
submit_message.author.mention,
Expand Down Expand Up @@ -333,7 +346,6 @@ async def scrape_channel_media(
continue

# Save attachment content
# TODO: Parse mp3 tags and things
attachment_filepath = path.join(
attachment_directory_filepath, attachment.filename
)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
@@ -1,2 +1,3 @@
pynacl>=1.4.0
discord.py>=1.7.3
discord.py>=1.7.3
tinytag==1.7.0