From b6dbc5c00a33aabc18aba03e8613c09a827191c0 Mon Sep 17 00:00:00 2001 From: Ritiek Malhotra Date: Wed, 18 Apr 2018 19:31:29 +0530 Subject: [PATCH] Show complete list of tracks if metadata not found in manual mode (#266) * Show complete list of tracks if metadata not found in manual mode * Add regression test --- core/youtube_tools.py | 22 +++++++++++++--------- test/test_without_metadata.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/core/youtube_tools.py b/core/youtube_tools.py index 7eee219..3ed03b0 100644 --- a/core/youtube_tools.py +++ b/core/youtube_tools.py @@ -159,7 +159,7 @@ class GenerateYouTubeURL: return url - def scrape(self, tries_remaining=5): + def scrape(self, bestmatch=True, tries_remaining=5): """ Search and scrape YouTube to return a list of matching videos. """ # prevents an infinite loop but allows for a few retries @@ -187,18 +187,21 @@ class GenerateYouTubeURL: videotime = x.find('span', class_="video-time").get_text() except AttributeError: log.debug('Could not find video duration on YouTube, retrying..') - return generate_youtube_url(self.raw_song, self.meta_tags, tries_remaining - 1) + return self.scrape(self.raw_song, + self.meta_tags, + tries_remaining=tries_remaining-1) youtubedetails = {'link': link, 'title': title, 'videotime': videotime, 'seconds': internals.get_sec(videotime)} videos.append(youtubedetails) - if self.meta_tags is None: - break - return self._best_match(videos) + if bestmatch: + return self._best_match(videos) + + return videos - def api(self): + def api(self, bestmatch=True): """ Use YouTube API to search and return a list of matching videos. """ query = { 'part' : 'snippet', @@ -232,7 +235,8 @@ class GenerateYouTubeURL: 'videotime':internals.videotime_from_seconds(duration_s), 'seconds': duration_s} videos.append(youtubedetails) - if not self.meta_tags: - break - return self._best_match(videos) + if bestmatch: + return self._best_match(videos) + + return videos diff --git a/test/test_without_metadata.py b/test/test_without_metadata.py index dc7bf33..345f4f4 100644 --- a/test/test_without_metadata.py +++ b/test/test_without_metadata.py @@ -36,6 +36,25 @@ def test_metadata(): assert metadata == expect_metadata +class TestArgsManualResultCount: + # Regresson test for issue #264 + def test_scrape(self): + const.args.manual = True + url = youtube_tools.GenerateYouTubeURL("she is still sleeping", + meta_tags=None) + video_ids = url.scrape(bestmatch=False) + # Web scraping gives us all videos on the 1st page + assert len(video_ids) == 20 + + def test_api(self): + url = youtube_tools.GenerateYouTubeURL("she is still sleeping", + meta_tags=None) + video_ids = url.api(bestmatch=False) + const.args.manual = False + # API gives us 50 videos (or as requested) + assert len(video_ids) == 50 + + class TestYouTubeURL: def test_only_music_category(self): # YouTube keeps changing its results