Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Inheritance for metadata handling
  • Loading branch information
Javinator9889 committed Sep 30, 2019
1 parent 245095e commit 08aaff2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
39 changes: 33 additions & 6 deletions YouTubeMDBot/metadata/MetadataIdentifier.py
Expand Up @@ -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 = ""
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -80,9 +81,32 @@ 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:
# 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())
Expand All @@ -93,4 +117,7 @@ def identify_audio(self) -> json:
self.cover = urlopen(video_data.thumbnail).read()
self.youtube_id = video_data.id
self.youtube_data = True
return data

valid = True

return valid
1 change: 1 addition & 0 deletions YouTubeMDBot/metadata/__init__.py
Expand Up @@ -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
14 changes: 7 additions & 7 deletions YouTubeMDBot/tests/identifier.py
Expand Up @@ -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):
Expand All @@ -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"
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 08aaff2

Please sign in to comment.