Skip to content
Permalink
Browse files
Inheritance for metadata handling
  • Loading branch information
Javinator9889 committed Sep 30, 2019
1 parent 245095e commit 08aaff2a2e960e3cfbb29d2eed3fb1eedf697880
Showing with 50 additions and 22 deletions.
  1. +42 −15 YouTubeMDBot/metadata/MetadataIdentifier.py
  2. +1 −0 YouTubeMDBot/metadata/__init__.py
  3. +7 −7 YouTubeMDBot/tests/identifier.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
@@ -14,3 +14,4 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ..metadata.MetadataIdentifier import MetadataIdentifier
from ..metadata.MetadataIdentifier import YouTubeMetadataIdentifier
@@ -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

0 comments on commit 08aaff2

Please sign in to comment.