Skip to content

Commit

Permalink
Add more changes to support extracting metadata from yt
Browse files Browse the repository at this point in the history
  • Loading branch information
deepjyoti30 committed Nov 20, 2023
1 parent 94d3a47 commit 66fc577
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
26 changes: 18 additions & 8 deletions ytmdl/manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,24 @@ class Meta:
track_number : Number of the track in the album
artwork_url_100 : URL of the album cover
"""
def __init__(self):
self.release_date = "{}T00:00:00Z".format(datetime.now().date())
self.track_name = "N/A"
self.artist_name = "N/A"
self.collection_name = "N/A"
self.primary_genre_name = "N/A"
self.track_number = "1"
self.artwork_url_100 = ""
def __init__(
self,
release_date: str = None,
track_name: str = "N/A",
artist_name: str = "N/A",
collection_name: str = "N/A",
primary_genre_name: str = "N/A",
track_number: str = "1",
artwork_url_100: str = ""
):
self.release_date = "{}T00:00:00Z".format(datetime.now().date()) if \
release_date is None else release_date
self.track_name = track_name
self.artist_name = artist_name
self.collection_name = collection_name
self.primary_genre_name = primary_genre_name
self.track_number = track_number
self.artwork_url_100 = artwork_url_100

def _read_individual(self, default_value):
"""
Expand Down
40 changes: 40 additions & 0 deletions ytmdl/meta/yt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Handle metadata extraction from youtube
"""

from typing import Dict

from yt_dlp import YoutubeDL
from simber import Logger

from ytmdl.manual import Meta
from ytmdl.utils.ytdl import get_ytdl_opts
from ytmdl.exceptions import ExtractError

# Create logger
logger = Logger("meta:yt")


def __parse_meta_from_details(details: Dict) -> Meta:
"""
Parse the meta object from the passed details
"""
return Meta(

)

def extract_meta_from_yt(video_id: str) -> Meta:
"""
Extract the metadata from the passed video ID and return
it accordingly.
"""
ytdl_obj = YoutubeDL(get_ytdl_opts())

video_url = f"https://www.youtube.com/watch?v={video_id}"

try:
details = ytdl_obj.extract_info(video_url, download=False)
except Exception as e:
logger.debug("Got exception while extracting details for video with ID: ", video_id)
logger.warning("Failed to extract metadata from yt with exception: ", str(e))
raise ExtractError(f"error extracting data from yt: {str(e)}")
16 changes: 10 additions & 6 deletions ytmdl/utils/ytdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
logger = Logger("yt")


def get_ytdl_opts() -> Dict:
return {
"quiet": is_quiet,
'no_warnings': no_warnings,
'nocheckcertificate': True,
'source_address': '0.0.0.0'
}


def is_ytdl_config_present(path_passed: str) -> bool:
"""
Check if the passed file is present or not.
Expand Down Expand Up @@ -45,12 +54,7 @@ def ydl_opts_with_config(ytdl_config: str = None) -> Dict:
no_warnings: bool = utility.determine_logger_level(
) > logger.level_map["WARNING"]

ydl_opts = {
"quiet": is_quiet,
'no_warnings': no_warnings,
'nocheckcertificate': True,
'source_address': '0.0.0.0'
}
ydl_opts = get_ytdl_opts()

# If config is passed, generated opts with config
if ytdl_config is not None:
Expand Down

0 comments on commit 66fc577

Please sign in to comment.