Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Optimized imports
When the Google API is not being used for just doing a search, we do not do an import of it
  • Loading branch information
Javinator9889 committed Sep 26, 2019
1 parent b61bd81 commit 15c0a51
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
36 changes: 18 additions & 18 deletions YouTubeMDBot/api/youtube_api.py
Expand Up @@ -13,13 +13,6 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
Expand All @@ -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")
Expand All @@ -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:
Expand All @@ -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"])
Expand All @@ -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()))
2 changes: 2 additions & 0 deletions YouTubeMDBot/metadata/MetadataIdentifier.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions YouTubeMDBot/tests/identifier.py
Expand Up @@ -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()

Expand Down
Binary file added cover.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hello.m4a
Binary file not shown.

0 comments on commit 15c0a51

Please sign in to comment.