Commit 08aaff2a authored by Javinator9889's avatar Javinator9889 🎼

Inheritance for metadata handling

parent 245095e7
Pipeline #77 passed with stage
in 17 minutes and 37 seconds
......@@ -29,7 +29,7 @@ from ..downloader import YouTubeDownloader
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 @@ class MetadataIdentifier(object):
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 @@ class MetadataIdentifier(object):
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 @@ class MetadataIdentifier(object):
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 sleep
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 @@ class IdentifierTest(unittest.TestCase):
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 @@ class IdentifierTest(unittest.TestCase):
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 @@ class IdentifierTest(unittest.TestCase):
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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment