From 090d9fb020d6e375050a74e8cfbe232af3bb4881 Mon Sep 17 00:00:00 2001 From: Fabian Wunsch Date: Sat, 30 Apr 2022 00:29:28 +0200 Subject: [PATCH] Made ChannelSearch more resilliant, Fixes #175 --- tests/async/search.py | 4 ++++ tests/sync/search.py | 3 +++ youtubesearchpython/core/channelsearch.py | 10 +++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/async/search.py b/tests/async/search.py index 56a5034..074ed07 100644 --- a/tests/async/search.py +++ b/tests/async/search.py @@ -31,6 +31,10 @@ async def main(): result = await search.next() print(result) + channel = ChannelSearch('The Beatles - Topic', 'UC2XdaAVUannpujzv32jcouQ') + result = await search.next() + print(result) + search = VideosSearch('NoCopyrightSounds') index = 0 diff --git a/tests/sync/search.py b/tests/sync/search.py index 00cf74d..007cf73 100644 --- a/tests/sync/search.py +++ b/tests/sync/search.py @@ -39,3 +39,6 @@ channel = ChannelSearch("Watermelon Sugar", "UCZFWPqqPkFlNwIxcpsLOwew") print(channel.result(mode=ResultMode.json)) + +channel = ChannelSearch('The Beatles - Topic', 'UC2XdaAVUannpujzv32jcouQ') +print(channel.result(mode=ResultMode.json)) diff --git a/youtubesearchpython/core/channelsearch.py b/youtubesearchpython/core/channelsearch.py index 74dd16a..9550ec4 100644 --- a/youtubesearchpython/core/channelsearch.py +++ b/youtubesearchpython/core/channelsearch.py @@ -35,7 +35,15 @@ async def next(self): def _parseChannelSearchSource(self) -> None: try: - self.response = self.response["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][-1]["expandableTabRenderer"]["content"]["sectionListRenderer"]["contents"] + last_tab = self.response["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][-1] + if 'expandableTabRenderer' in last_tab: + self.response = last_tab["expandableTabRenderer"]["content"]["sectionListRenderer"]["contents"] + else: + tab_renderer = last_tab["tabRenderer"] + if 'content' in tab_renderer: + self.response = tab_renderer["content"]["sectionListRenderer"]["contents"] + else: + self.response = [] except: raise Exception('ERROR: Could not parse YouTube response.')