From 59094568e036392384f46f10d2365cb754241e82 Mon Sep 17 00:00:00 2001 From: jesopo Date: Fri, 11 Jan 2019 11:30:29 +0000 Subject: [PATCH] Add youtube-safesearch channel setting, to allow channels to opt-in to turning off any safe search filters (youtube.py) --- modules/youtube.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/youtube.py b/modules/youtube.py index 6f7f041a..03674f87 100644 --- a/modules/youtube.py +++ b/modules/youtube.py @@ -19,6 +19,9 @@ ARROW_DOWN = "↓" @utils.export("channelset", {"setting": "auto-youtube", "help": "Disable/Enable automatically getting info from youtube URLs", "validate": utils.bool_or_none}) +@utils.export("channelset", {"setting": "youtube-safesearch", + "help": "Turn safe search off/on", + "validate": utils.bool_or_none}) class Module(ModuleManager.BaseModule): def get_video_page(self, video_id, part): return utils.http.request(URL_YOUTUBEVIDEO, get_params={"part": part, @@ -92,12 +95,16 @@ class Module(ModuleManager.BaseModule): video_id = url_match.group(1) if search or video_id: + safe_setting = event["target"].get_setting("youtube-safesearch", + True) + safe = "moderate" if safe_setting else "none" + if not video_id: search_page = utils.http.request(URL_YOUTUBESEARCH, get_params={"q": search, "part": "snippet", "maxResults": "1", "type": "video", - "key": self.bot.config["google-api-key"]}, - json=True) + "key": self.bot.config["google-api-key"], + "safeSearch": safe}, json=True) if search_page: if search_page.data["pageInfo"]["totalResults"] > 0: video_id = search_page.data["items"][0]["id"]["videoId"]