Better implementation of YoutubeSearchURLIE

This commit is contained in:
pukkandan 2020-11-22 18:39:09 +05:30
parent 7bd4a9b611
commit 386e1dd908
1 changed files with 11 additions and 10 deletions

View File

@ -3457,10 +3457,10 @@ class YoutubeSearchDateIE(YoutubeSearchIE):
_SEARCH_PARAMS = 'CAI%3D' _SEARCH_PARAMS = 'CAI%3D'
class YoutubeSearchURLIE(InfoExtractor): class YoutubeSearchURLIE(YoutubeSearchIE):
IE_DESC = 'YouTube.com search URLs' IE_DESC = 'YouTube.com search URLs'
IE_NAME = 'youtube:search_url' IE_NAME = YoutubeSearchIE.IE_NAME + '_url'
_VALID_URL = r'https?://(?:www\.)?youtube\.com/results/?(?:\?|\?[^#]*?&)(?:sp=(?P<param1>[^&#]+)&(?:[^#]*&)?)?(?:q|search_query)=(?P<query>[^#&]+)(?:[^#]*?&sp=(?P<param2>[^#&]+))?' _VALID_URL = r'https?://(?:www\.)?youtube\.com/results\?(.*?&)?(?:search_query|q)=(?:[^&]+)(?:[&]|$)'
# _MAX_RESULTS = 100 # _MAX_RESULTS = 100
_TESTS = [{ _TESTS = [{
'url': 'https://www.youtube.com/results?baz=bar&search_query=youtube-dl+test+video&filters=video&lclk=video', 'url': 'https://www.youtube.com/results?baz=bar&search_query=youtube-dl+test+video&filters=video&lclk=video',
@ -3473,14 +3473,15 @@ class YoutubeSearchURLIE(InfoExtractor):
'only_matching': True, 'only_matching': True,
}] }]
@classmethod
def _make_valid_url(cls):
return cls._VALID_URL
def _real_extract(self, url): def _real_extract(self, url):
mobj = re.match(self._VALID_URL, url) qs = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
query = compat_urllib_parse_unquote_plus(mobj.group('query')) query = (qs.get('search_query') or qs.get('q'))[0]
IE = YoutubeSearchIE(self._downloader) self._SEARCH_PARAMS = qs.get('sp', ('',))[0]
IE._SEARCH_PARAMS = mobj.group('param1') or mobj.group('param2') return self._get_n_results(query, self._MAX_RESULTS)
if hasattr(self, '_MAX_RESULTS'):
IE._MAX_RESULTS = self._MAX_RESULTS
return IE._get_n_results(query, IE._MAX_RESULTS)
class YoutubeFeedsInfoExtractor(YoutubeTabIE): class YoutubeFeedsInfoExtractor(YoutubeTabIE):