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

Solved: Read media info from tags #30

merged 8 commits into from Jan 16, 2022

Conversation

DipayanP007
Copy link
Contributor

Modified Code so that:

  • When the command is !list, the output will have audio title, artist, album in the format title | artist | album
  • If the media does not have the specified tags, the filename will be used instead

Screenshots:

  • !list command

list

  • !play command

play

Solves the issue #25

@anoadragon453
Copy link
Owner

Thanks @DipayanP007! If you find yourself battling with the CI, note that there is a script (scripts-dev/lint.sh) that will fix/point out style errors locally. Thus, you can fix them before pushing a commit to GitHub.

The CI is just running the same commands as that script. So if the script passes, then the CI should to!

@DipayanP007
Copy link
Contributor Author

Hi @anoadragon453 thanks for the info. I did the same and the script ran on local. I guess the ci will pass now.

@DipayanP007
Copy link
Contributor Author

And review the changes if you can 😄

Copy link
Collaborator

@Cephian Cephian left a comment

Choose a reason for hiding this comment

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

Hey Dipayan, awesome work! Thanks for the PR.

I have a few formatting suggestions/requests:

  1. The "Album" field unnecessarily clutters the output, So I think it's best to just not display that one
  2. Instead of the format @user - <Artist Tag> | <Title Tag> I think @user: <Artist Tag> - <Title Tag> is closer to what's more commonly used for titles, so we should prefer it. Files without tags should display @user: <Filename> for consistency, where the filename is processed as it is now.
  3. If a user submits a song with a Title tag but no Artist tag, we should just display @user: <Title Tag>

With these changes (and @anoadragon453 and I's suggestions below) I think this should be ready to commit!

main.py Outdated
if len(audio.title.strip()) == 0:
filename = path.splitext(filename)[0]

filename = filename.replace("_", " ")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe we can just remove this line, since filename.replace("-", " ") is called on the line afterwards as well

Suggested change
filename = filename.replace("_", " ")

Copy link
Owner

@anoadragon453 anoadragon453 left a comment

Choose a reason for hiding this comment

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

Some things I noticed. Thanks again for picking this up!

.gitignore Outdated
Comment on lines 4 to 5
venv
__pycache__
Copy link
Owner

Choose a reason for hiding this comment

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

A small nitpick, but for consistency with the other entries in this file.

Suggested change
venv
__pycache__
venv/
__pycache__/

main.py Outdated
@@ -2,6 +2,7 @@
import os
Copy link
Owner

Choose a reason for hiding this comment

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

Could you remove the TODO: Parse mp3 tags and things at line ~350? This PR should resolve the TODO!

main.py Outdated
# Replace all underscores with spaces
filename = filename.replace("_", " ")
# Get all the tags for a track
audio = TinyTag.get(f"{attachment_directory_filepath}/{filename}")
Copy link
Owner

Choose a reason for hiding this comment

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

Note that {attachment_directory_filepath}/{filename} will be invalid on Windows, as Windows uses \ as a directory separator.

Instead, one should use os.path.join, which will create a path suitable for the detected operating system.

main.py Outdated

return discord.utils.escape_markdown(filename)
# If the tag doesnot exist display the file name only
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
# If the tag doesnot exist display the file name only
# If the tag does not exist or only contains whitespace, display the file name only

main.py Outdated

return discord.utils.escape_markdown(filename)
# If the tag doesnot exist display the file name only
if len(audio.title.strip()) == 0:
Copy link
Owner

Choose a reason for hiding this comment

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

Note that the attributes of audio here, such as audio.title, will be None if the corresponding tag isn't found in the file. In that case, audio.title.strip() will raise an Exception as strip() does not exist on type None.

We should first check if audio.title is None before using it:

Suggested change
if len(audio.title.strip()) == 0:
if audio.title is None or len(audio.title.strip()) == 0:

@DipayanP007
Copy link
Contributor Author

Hey @anoadragon453 @Cephian I have made the changes you requested. Review it
Here is a screenshot of the current embed:
ss

Copy link
Collaborator

@Cephian Cephian left a comment

Choose a reason for hiding this comment

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

The code looks and runs good! I made three suggestions, each just for code style. After that, I think we should be ready to merge. Thanks!

main.py Outdated
str(os.path.join(f"{attachment_directory_filepath}", f"{filename}"))
)
content = ""
# If the tag doesnot exist or is whitespace display the file name only
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 the tag doesnot exist or is whitespace display the file name only
# If the tag does not exist or is whitespace display the file name only

content = ""
# If the tag doesnot 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 len(audio.artist.strip()) != 0:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I missed this in my first review, but using string.isspace() seems better than checking if len(string.split()) == 0

Suggested change
if audio.artist is not None and len(audio.artist.strip()) != 0:
if audio.artist is not None and not audio.artist.isspace():

main.py Outdated
Comment on lines 114 to 123
if audio.title is not None:
if len(audio.title.strip()) == 0:
filename = path.splitext(filename)[0]
content = content + filename.replace("_", " ")
else:
content = content + f"{str(audio.title)}"
# If the title tag doesnot exist but the artist tag exists, display the file name along with artist tag
else:
filename = path.splitext(filename)[0]
content = content + filename.replace("_", " ")
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can remove some duplicated code if we write the if statements like this

Suggested change
if audio.title is not None:
if len(audio.title.strip()) == 0:
filename = path.splitext(filename)[0]
content = content + filename.replace("_", " ")
else:
content = content + f"{str(audio.title)}"
# If the title tag doesnot exist but the artist tag exists, display the file name along with artist tag
else:
filename = path.splitext(filename)[0]
content = content + filename.replace("_", " ")
if audio.title is not None and not audio.title.isspace():
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("_", " ")

@DipayanP007
Copy link
Contributor Author

Hey @Cephian I made the changes requested. 😄

Copy link
Collaborator

@Cephian Cephian left a comment

Choose a reason for hiding this comment

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

Oops! This time it was my mistake, I recommended code without testing it. It turns out text.isspace() returns False on empty strings, so what you had was better! I fixed the two isspace instances as recommendations (and actually tested the code this time!) so with this, the PR should finally be good.

main.py Outdated
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():

main.py Outdated
if audio.artist is not None and not audio.artist.isspace():
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():

@DipayanP007
Copy link
Contributor Author

Hey @Cephian did the changes you requested

Copy link
Collaborator

@Cephian Cephian left a comment

Choose a reason for hiding this comment

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

Everything looks good! Thanks so much for your PR!

@Cephian Cephian merged commit c508924 into anoadragon453:main Jan 16, 2022
@Cephian Cephian mentioned this pull request Jan 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants