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
Conversation
Thanks @DipayanP007! If you find yourself battling with the CI, note that there is a script ( The CI is just running the same commands as that script. So if the script passes, then the CI should to! |
Hi @anoadragon453 thanks for the info. I did the same and the script ran on local. I guess the ci will pass now. |
And review the changes if you can 😄 |
There was a problem hiding this 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:
- The "Album" field unnecessarily clutters the output, So I think it's best to just not display that one
- 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. - 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("_", " ") |
There was a problem hiding this comment.
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
filename = filename.replace("_", " ") |
There was a problem hiding this 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
venv | ||
__pycache__ |
There was a problem hiding this comment.
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.
venv | |
__pycache__ | |
venv/ | |
__pycache__/ |
main.py
Outdated
@@ -2,6 +2,7 @@ | |||
import os |
There was a problem hiding this comment.
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}") |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# 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: |
There was a problem hiding this comment.
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:
if len(audio.title.strip()) == 0: | |
if audio.title is None or len(audio.title.strip()) == 0: |
Hey @anoadragon453 @Cephian I have made the changes you requested. Review it |
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# 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: |
There was a problem hiding this comment.
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
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
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("_", " ") |
There was a problem hiding this comment.
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
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("_", " ") |
Hey @Cephian I made the changes requested. 😄 |
There was a problem hiding this 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(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if audio.title is not None and not audio.title.isspace(): | |
if audio.title is not None and audio.title.strip(): |
Hey @Cephian did the changes you requested |
There was a problem hiding this 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!
Modified Code so that:
!list
, the output will have audio title, artist, album in the formattitle | artist | album
Screenshots:
!list command
!play command
Solves the issue #25