From 08aaff2a2e960e3cfbb29d2eed3fb1eedf697880 Mon Sep 17 00:00:00 2001 From: Javinator9889 Date: Mon, 30 Sep 2019 12:07:07 +0200 Subject: [PATCH] Inheritance for metadata handling --- YouTubeMDBot/metadata/MetadataIdentifier.py | 57 +++++++++++++++------ YouTubeMDBot/metadata/__init__.py | 1 + YouTubeMDBot/tests/identifier.py | 14 ++--- 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/YouTubeMDBot/metadata/MetadataIdentifier.py b/YouTubeMDBot/metadata/MetadataIdentifier.py index 69039cb..7235535 100644 --- a/YouTubeMDBot/metadata/MetadataIdentifier.py +++ b/YouTubeMDBot/metadata/MetadataIdentifier.py @@ -29,7 +29,7 @@ class MetadataIdentifier(object): - def __init__(self, audio: bytes, downloader: YouTubeDownloader = None): + def __init__(self, audio: bytes): self.audio = audio self.result: json = None self.artist: str = "" @@ -41,7 +41,7 @@ def __init__(self, audio: bytes, downloader: YouTubeDownloader = None): self.duration: int = 0 self.youtube_data: bool = False self.youtube_id: str = "" - self._downloader = downloader + # self._downloader = downloader @staticmethod def _is_valid_result(data: json) -> bool: @@ -57,14 +57,15 @@ def _is_valid_result(data: json) -> bool: else: return True - def identify_audio(self) -> json: + def identify_audio(self) -> bool: fingerprint = FPCalc(self.audio) data: json = acoustid.lookup(apikey=ACOUSTID_KEY, fingerprint=fingerprint.fingerprint(), duration=fingerprint.duration(), meta="recordings releaseids") self.result = data - if self._is_valid_result(data): + is_valid = self._is_valid_result(data) + if is_valid: for result in data["results"]: if "recordings" not in result: break @@ -80,17 +81,43 @@ def identify_audio(self) -> json: self.recording_id = recording["id"] self.duration = recording["duration"] self.cover = musicbrainzngs.get_image_front(self.release_id) + is_valid = True break break - elif self._downloader: - from urllib.request import urlopen + # elif self._downloader: + # from urllib.request import urlopen + # + # video_id = youtube_utils.get_yt_video_id(self._downloader.get_url()) + # video_data = YouTubeAPI.video_details(video_id) + # self.title = video_data.title + # self.artist = video_data.artist + # self.duration = video_data.duration + # self.cover = urlopen(video_data.thumbnail).read() + # self.youtube_id = video_data.id + # self.youtube_data = True + return is_valid + + +class YouTubeMetadataIdentifier(MetadataIdentifier): + def __init__(self, audio: bytes, downloader: YouTubeDownloader = None): + super().__init__(audio) + self._downloader = downloader + + def identify_audio(self) -> bool: + valid = super().identify_audio() + if not valid: + if self._downloader: + from urllib.request import urlopen + + video_id = youtube_utils.get_yt_video_id(self._downloader.get_url()) + video_data = YouTubeAPI.video_details(video_id) + self.title = video_data.title + self.artist = video_data.artist + self.duration = video_data.duration + self.cover = urlopen(video_data.thumbnail).read() + self.youtube_id = video_data.id + self.youtube_data = True + + valid = True - video_id = youtube_utils.get_yt_video_id(self._downloader.get_url()) - video_data = YouTubeAPI.video_details(video_id) - self.title = video_data.title - self.artist = video_data.artist - self.duration = video_data.duration - self.cover = urlopen(video_data.thumbnail).read() - self.youtube_id = video_data.id - self.youtube_data = True - return data + return valid diff --git a/YouTubeMDBot/metadata/__init__.py b/YouTubeMDBot/metadata/__init__.py index 3799511..0d91c30 100644 --- a/YouTubeMDBot/metadata/__init__.py +++ b/YouTubeMDBot/metadata/__init__.py @@ -14,3 +14,4 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . from ..metadata.MetadataIdentifier import MetadataIdentifier +from ..metadata.MetadataIdentifier import YouTubeMetadataIdentifier diff --git a/YouTubeMDBot/tests/identifier.py b/YouTubeMDBot/tests/identifier.py index dec5b80..a6e659e 100644 --- a/YouTubeMDBot/tests/identifier.py +++ b/YouTubeMDBot/tests/identifier.py @@ -5,7 +5,7 @@ from time import time from YouTubeMDBot.downloader import YouTubeDownloader -from YouTubeMDBot.metadata import MetadataIdentifier +from YouTubeMDBot.metadata import YouTubeMetadataIdentifier class IdentifierTest(unittest.TestCase): @@ -20,9 +20,10 @@ def test_identification(self): audio, data = downloader.download() with open("hello.m4a", "wb") as song: song.write(data) - identifier = MetadataIdentifier(audio=data) + identifier = YouTubeMetadataIdentifier(audio=data) - results = identifier.identify_audio() + valid = identifier.identify_audio() + assert valid print("{0} by {1} - score: {2} / 1\n" "\thttps://musicbrainz.org/recording/{3}\n" "\thttps://musicbrainz.org/release/{4}\n\n" @@ -32,8 +33,6 @@ def test_identification(self): with open("cover.jpg", "wb") as cover: cover.write(identifier.cover) - pprint(results) - def test_multiple_download_identification(self): yt1 = YouTubeDownloader(url="https://www.youtube.com/watch?v=Inm-N5rLUSI") yt2 = YouTubeDownloader(url="https://www.youtube.com/watch?v=-_ZwpOdXXcA") @@ -71,8 +70,9 @@ def find_metadata(self, downloader: YouTubeDownloader): f_dl_t = time() print("Downloaded {} - elapsed time: {:.1f}s".format(downloader.get_url(), f_dl_t - st_dl_t)) - identifier = MetadataIdentifier(audio=data, downloader=downloader) - identifier.identify_audio() + identifier = YouTubeMetadataIdentifier(audio=data, downloader=downloader) + valid = identifier.identify_audio() + assert valid self.song_info[downloader.get_url()] = { "title": identifier.title, "artist": identifier.artist