From 6b00d3410a88d3d97781e0b4c775d50e5e8966bf Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 27 May 2019 23:48:47 +0200 Subject: [PATCH] Fix things in the discord embed --- royalnet/audio/youtubedl.py | 8 +++++--- royalnet/utils/__init__.py | 4 ++-- royalnet/utils/formatters.py | 6 ++++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/royalnet/audio/youtubedl.py b/royalnet/audio/youtubedl.py index b0c4f47d..cc9a3289 100644 --- a/royalnet/audio/youtubedl.py +++ b/royalnet/audio/youtubedl.py @@ -2,8 +2,10 @@ import typing import logging as _logging import discord import os +import dateparser import datetime from youtube_dl import YoutubeDL +from ..utils import ytdldateformat log = _logging.getLogger(__name__) @@ -71,7 +73,7 @@ class YtdlInfo: self.uploader_url: typing.Optional[str] = info.get("uploader_url") self.channel_id: typing.Optional[str] = info.get("channel_id") self.channel_url: typing.Optional[str] = info.get("channel_url") - self.upload_date: typing.Optional[str] = info.get("upload_date") + self.upload_date: typing.Optional[datetime.datetime] = dateparser.parse(ytdldateformat(info.get("upload_date"))) self.license: typing.Optional[str] = info.get("license") self.creator: typing.Optional[...] = info.get("creator") self.title: typing.Optional[str] = info.get("title") @@ -82,7 +84,7 @@ class YtdlInfo: self.tags: typing.Optional[typing.List[str]] = info.get("tags") self.subtitles: typing.Optional[typing.Dict[str, typing.List[typing.Dict[str, str]]]] = info.get("subtitles") self.automatic_captions: typing.Optional[dict] = info.get("automatic_captions") - self.duration: typing.Optional[int] = info.get("duration") + self.duration: typing.Optional[datetime.timedelta] = datetime.timedelta(seconds=info.get("duration", 0)) self.age_limit: typing.Optional[int] = info.get("age_limit") self.annotations: typing.Optional[...] = info.get("annotations") self.chapters: typing.Optional[...] = info.get("chapters") @@ -150,7 +152,7 @@ class YtdlInfo: embed.set_author(name=self.uploader, url=self.uploader_url) embed.set_footer(text="Fonte: youtube-dl", icon_url="https://i.imgur.com/TSvSRYn.png") embed.add_field(name="Duration", value=str(self.duration), inline=True) - embed.add_field(name="Published on", value=self.upload_date, inline=True) + embed.add_field(name="Published on", value=str(self.upload_date), inline=True) return embed def __repr__(self): diff --git a/royalnet/utils/__init__.py b/royalnet/utils/__init__.py index 2d42bbe3..cd1860b0 100644 --- a/royalnet/utils/__init__.py +++ b/royalnet/utils/__init__.py @@ -9,7 +9,7 @@ from .safeformat import safeformat from .classdictjanitor import cdj from .sleepuntil import sleep_until from .networkhandler import NetworkHandler -from .formatters import andformat, plusformat, fileformat +from .formatters import andformat, plusformat, fileformat, ytdldateformat __all__ = ["asyncify", "Call", "Command", "safeformat", "cdj", "sleep_until", "plusformat", "CommandArgs", - "NetworkHandler", "andformat", "plusformat", "fileformat"] + "NetworkHandler", "andformat", "plusformat", "fileformat", "ytdldateformat"] diff --git a/royalnet/utils/formatters.py b/royalnet/utils/formatters.py index 644c92d3..9e519fb4 100644 --- a/royalnet/utils/formatters.py +++ b/royalnet/utils/formatters.py @@ -38,3 +38,9 @@ def plusformat(i: int) -> str: def fileformat(string: str) -> str: """Ensure a string can be used as a filename by replacing all non-word characters with underscores.""" return re.sub(r"\W", "_", string) + + +def ytdldateformat(string: typing.Optional[str], separator: str = "-") -> str: + if string is None: + return "" + return f"{string[0:4]}-{string[4:6]}-{string[6:8]}"