From 7e6e40965b6ee0375c600593db6c4cbeb7fd89cc Mon Sep 17 00:00:00 2001 From: jesopo Date: Fri, 25 Oct 2019 15:07:30 +0100 Subject: [PATCH] comma-separate likes/dislikes for youtube videos --- modules/youtube.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/modules/youtube.py b/modules/youtube.py index 685f126b..e9a84f19 100644 --- a/modules/youtube.py +++ b/modules/youtube.py @@ -31,6 +31,11 @@ class Module(ModuleManager.BaseModule): return utils.http.request(URL_YOUTUBEVIDEO, get_params={"part": part, "id": video_id, "key": self.bot.config["google-api-key"]}, json=True) + + def _number(self, n): + if n: + return "{:,}".format(int(n)) + def video_details(self, video_id): snippet = self.get_video_page(video_id, "snippet") if snippet.data["items"]: @@ -41,9 +46,9 @@ class Module(ModuleManager.BaseModule): "items"][0]["contentDetails"] video_uploader = snippet["channelTitle"] video_title = snippet["title"] - video_views = statistics["viewCount"] - video_likes = statistics.get("likeCount") - video_dislikes = statistics.get("dislikeCount") + video_views = self._number(statistics["viewCount"]) + video_likes = self._number(statistics.get("likeCount")) + video_dislikes = self._number(statistics.get("dislikeCount")) video_duration = content["duration"] video_opinions = "" if video_likes and video_dislikes: @@ -63,8 +68,8 @@ class Module(ModuleManager.BaseModule): ) if match.group(3) else "00" url = URL_YOUTUBESHORT % video_id return "%s (%s) uploaded by %s, %s views%s" % ( - video_title, video_duration, video_uploader, "{:,}".format( - int(video_views)), video_opinions), url + video_title, video_duration, video_uploader, video_views, + video_opinions), url return None def get_playlist_page(self, playlist_id, part): @@ -79,7 +84,7 @@ class Module(ModuleManager.BaseModule): content = self.get_playlist_page(playlist_id, "contentDetails") count = content.data["items"][0]["contentDetails"]["itemCount"] - return "%s - %s (%d %s)" % (snippet["channelTitle"], + return "%s - %s (%s %s)" % (snippet["channelTitle"], snippet["title"], count, "video" if count == 1 else "videos" ), URL_PLAYLIST % playlist_id