Skip to content

Commit

Permalink
fix: release date empty
Browse files Browse the repository at this point in the history
  • Loading branch information
tristiisch committed Sep 22, 2024
1 parent dfcf2f8 commit b85c4e8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/pyramid/connector/discord/music_player_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def __embed_track(self, track: Track, locale: Locale) -> Embed:
color=discord.Color.blue(),
)
embed.add_field(name="Album", value=track.album_title)
embed.add_field(name="Release", value=track.get_date(locale.value))
release_date = track.get_date(locale.value)
if release_date is not None:
embed.add_field(name="Release", value=release_date)

embed.set_author(name=", ".join(track.authors), icon_url=track.author_picture)
embed.set_thumbnail(url=track.album_picture)
Expand Down
23 changes: 19 additions & 4 deletions src/pyramid/data/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime

import deezer
import pyramid.tools.utils as tools
from pyramid.tools import utils


class TrackMinimal(ABC):
Expand Down Expand Up @@ -95,9 +95,24 @@ def __init__(self, data, file_path):
self.duration_seconds: int = int(data["DURATION"])
self.duration: str = self.format_duration(int(data["DURATION"]))
self.file_size: int = int(data["FILESIZE"])
self.date: datetime = datetime.strptime(data["PHYSICAL_RELEASE_DATE"], "%Y-%m-%d")
if self.__is_valid_date(data["PHYSICAL_RELEASE_DATE"]):
self.date = datetime.strptime(data["PHYSICAL_RELEASE_DATE"], "%Y-%m-%d")
else:
self.date = None
self.file_local: str = file_path

def get_date(self, locale: str = "en-US") -> str:
date_formatted = tools.format_date_by_country(self.date, locale)
def get_date(self, locale: str = "en-US") -> str | None:
if self.date is None:
return None
date_formatted = utils.format_date_by_country(self.date, locale)
return date_formatted

def __is_valid_date(self, date: str):
# Check if the format is exactly "YYYY-MM-DD"
parts = date.split("-")
if len(parts) == 3 and len(parts[0]) == 4 and len(parts[1]) == 2 and len(parts[2]) == 2:
year, month, day = parts
if year.isdigit() and month.isdigit() and day.isdigit():
if 1 <= int(month) <= 12 and 1 <= int(day) <= 31:
return True
return False

0 comments on commit b85c4e8

Please sign in to comment.