From d8ea5e9ce838a9b6e8e35e7458108255f0514c96 Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 18 Nov 2019 10:52:01 +0000 Subject: combine all 3 youtube API queries in to one (for speed!) --- modules/youtube.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'modules') diff --git a/modules/youtube.py b/modules/youtube.py index 6d25c3e8..ac15f11e 100644 --- a/modules/youtube.py +++ b/modules/youtube.py @@ -27,8 +27,9 @@ class Module(ModuleManager.BaseModule): def on_load(self): self.exports.add("search-youtube", self._search_youtube) - def get_video_page(self, video_id, part): - return utils.http.request(URL_YOUTUBEVIDEO, get_params={"part": part, + def get_video_page(self, video_id): + return utils.http.request(URL_YOUTUBEVIDEO, get_params={ + "part": "contentDetails,snippet,statistics", "id": video_id, "key": self.bot.config["google-api-key"]}, json=True) @@ -37,13 +38,12 @@ class Module(ModuleManager.BaseModule): return "{:,}".format(int(n)) def video_details(self, video_id): - snippet = self.get_video_page(video_id, "snippet") - if snippet.data["items"]: - snippet = snippet.data["items"][0]["snippet"] - statistics = self.get_video_page(video_id, "statistics").data[ - "items"][0]["statistics"] - content = self.get_video_page(video_id, "contentDetails").data[ - "items"][0]["contentDetails"] + page = self.get_video_page(video_id) + if page.data["items"]: + item = page.data["items"][0] + snippet = item["snippet"] + statistics = item["statistics"] + content = item["contentDetails"] video_uploaded_at = utils.datetime.iso8601_parse( snippet["publishedAt"], microseconds=True) -- cgit v1.3.1-10-gc9f91