diff --git a/YouTubeMDBot/api/youtube_api.py b/YouTubeMDBot/api/youtube_api.py index 23b26e9..1260023 100644 --- a/YouTubeMDBot/api/youtube_api.py +++ b/YouTubeMDBot/api/youtube_api.py @@ -13,13 +13,6 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -try: - import ujson as json -except ImportError: - import json -from urllib.request import urlopen - -from googleapiclient.discovery import build from isodate import parse_duration from ..constants import YOUTUBE @@ -30,14 +23,14 @@ class YouTubeVideoData(object): def __init__(self, data: dict, ignore_errors: bool = False): if not data.get("items"): raise EmptyBodyError("The data object has no items") - self.id = None - self.title = None - self.thumbnail = None - self.artist = None - self.duration = None - self.views = None - self.likes = None - self.dislikes = None + self.id: str = "" + self.title: str = "" + self.thumbnail: str = "" + self.artist: str = "" + self.duration: float = 0.0 + self.views: int = 0 + self.likes: int = 0 + self.dislikes: int = 0 if len(data.get("items")) >= 1: content = data.get("items")[0] snippet = content.get("snippet") @@ -61,9 +54,8 @@ def __init__(self, data: dict, ignore_errors: bool = False): statistics_available = False else: statistics_available = True - self.id = content["id"] - if isinstance(self.id, dict): - self.id = self.id.get("videoId") + c_id = content.get("id", "") + self.id = c_id.get("videoId", "") if isinstance(c_id, dict) else c_id if snippet_available: self.title = snippet["title"] try: @@ -87,6 +79,8 @@ def __init__(self, data: dict, ignore_errors: bool = False): class YouTubeAPI(object): def __init__(self): + from googleapiclient.discovery import build + self.__youtube = build(serviceName=YOUTUBE["api"]["name"], version=YOUTUBE["api"]["version"], developerKey=YOUTUBE["key"]) @@ -101,6 +95,12 @@ def search(self, term: str): @staticmethod def video_details(video_id: str) -> YouTubeVideoData: + try: + import ujson as json + except ImportError: + import json + from urllib.request import urlopen + api_url = YOUTUBE["endpoint"].format(video_id, YOUTUBE["key"]) data = urlopen(url=api_url) return YouTubeVideoData(data=json.loads(data.read())) diff --git a/YouTubeMDBot/metadata/MetadataIdentifier.py b/YouTubeMDBot/metadata/MetadataIdentifier.py index 5adde34..69039cb 100644 --- a/YouTubeMDBot/metadata/MetadataIdentifier.py +++ b/YouTubeMDBot/metadata/MetadataIdentifier.py @@ -40,6 +40,7 @@ def __init__(self, audio: bytes, downloader: YouTubeDownloader = None): self.cover: bytes = bytes(0) self.duration: int = 0 self.youtube_data: bool = False + self.youtube_id: str = "" self._downloader = downloader @staticmethod @@ -90,5 +91,6 @@ def identify_audio(self) -> json: 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 diff --git a/YouTubeMDBot/tests/identifier.py b/YouTubeMDBot/tests/identifier.py index 3937e0e..dec5b80 100644 --- a/YouTubeMDBot/tests/identifier.py +++ b/YouTubeMDBot/tests/identifier.py @@ -85,6 +85,7 @@ def find_metadata(self, downloader: YouTubeDownloader): "https://musicbrainz.org/release/{0}".format(identifier.release_id) else: self.song_info[downloader.get_url()]["duration"] = identifier.duration + self.song_info[downloader.get_url()]["id"] = identifier.youtube_id self.song_info[downloader.get_url()]["youtube_data"] = True self.barrier() diff --git a/cover.jpg b/cover.jpg new file mode 100644 index 0000000..169117d Binary files /dev/null and b/cover.jpg differ diff --git a/hello.m4a b/hello.m4a new file mode 100644 index 0000000..1126d7e Binary files /dev/null and b/hello.m4a differ