Commit 15c0a51d authored by Javinator9889's avatar Javinator9889 🎼

Optimized imports

When the Google API is not being used for just doing a search, we do not do an import of it
parent b61bd81d
Pipeline #72 passed with stage
in 1 minute and 53 seconds
......@@ -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
......@@ -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 @@ class YouTubeVideoData(object):
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 @@ class YouTubeVideoData(object):
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 @@ class YouTubeAPI(object):
@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()))
......@@ -40,6 +40,7 @@ class MetadataIdentifier(object):
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 @@ class MetadataIdentifier(object):
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
......@@ -85,6 +85,7 @@ class IdentifierTest(unittest.TestCase):
"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()
......
This diff is collapsed.
File added
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