Skip to content

Commit

Permalink
Changed some typos and info var name
Browse files Browse the repository at this point in the history
  • Loading branch information
henrique-coder committed Jan 3, 2025
1 parent 9c83bed commit 31e60e2
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions streamsnapper/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@


class YouTube:
"""A class for extracting and formatting data from YouTube videos, facilitating access to general video information, video streams, audio streams and subtitles."""
"""
A class for extracting and formatting data from YouTube videos, facilitating access to general video information, video streams, audio streams and subtitles.
"""

def __init__(self, logging: bool = False) -> None:
"""
Expand Down Expand Up @@ -66,7 +68,7 @@ def __init__(self, logging: bool = False) -> None:
self.base_system_language: str = "en"
self.system_language_suffix: str = "US"

self.general_info: Dict[str, Any] = {}
self.information: Dict[str, Any] = {}

self.best_video_streams: List[Dict[str, Any]] = []
self.best_video_stream: Dict[str, Any] = {}
Expand Down Expand Up @@ -169,7 +171,7 @@ def analyze_info(self, check_thumbnails: bool = False, retrieve_dislike_count: b
except JSONDecodeError:
pass

general_info = {
information = {
"sourceUrl": self._source_url,
"shortUrl": f"https://youtu.be/{id_}",
"embedUrl": f"https://www.youtube.com/embed/{id_}",
Expand Down Expand Up @@ -208,19 +210,19 @@ def analyze_info(self, check_thumbnails: bool = False, retrieve_dislike_count: b
}

if check_thumbnails:
while general_info["thumbnails"]:
while information["thumbnails"]:
if head(
general_info["thumbnails"][0],
information["thumbnails"][0],
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
},
follow_redirects=False,
).is_success:
break
else:
general_info["thumbnails"].pop(0)
information["thumbnails"].pop(0)

self.general_info = dict(sorted(general_info.items()))
self.information = dict(sorted(information.items()))

def analyze_video_streams(
self,
Expand Down Expand Up @@ -516,7 +518,9 @@ def extract_stream_info(stream: Dict[Any, Any]) -> Dict[str, Optional[Union[str,
self.best_audio_download_url = self.best_audio_stream["url"] if self.best_audio_stream else None

def analyze_subtitle_streams(self) -> None:
"""Analyze the subtitle streams of the YouTube video."""
"""
Analyze the subtitle streams of the YouTube video.
"""

data = self._raw_youtube_subtitles

Expand Down Expand Up @@ -581,7 +585,7 @@ def download(
if not self._raw_youtube_data:
raise EmptyDataError("No YouTube data available. Please call .extract() first.")

if not self.general_info:
if not self.information:
self.analyze_info()

if not video_stream and not audio_stream:
Expand All @@ -596,13 +600,13 @@ def download(
if video_stream and audio_stream:
if output_path.is_dir():
output_path = Path(
output_path, f'{self.general_info["cleanTitle"]} [{self.general_info["id"]}].{video_stream["extension"]}'
output_path, f'{self.information["cleanTitle"]} [{self.information["id"]}].{video_stream["extension"]}'
)

tmp_path = Path(gettempdir(), ".tmp-streamsnapper-downloader")
tmp_path.mkdir(exist_ok=True)

output_video_path = Path(tmp_path, f'.tmp-video-{self.general_info["id"]}.{video_stream["extension"]}')
output_video_path = Path(tmp_path, f'.tmp-video-{self.information["id"]}.{video_stream["extension"]}')
video_downloader = TurboDL(
max_connections=max_connections,
connection_speed=connection_speed,
Expand All @@ -614,7 +618,7 @@ def download(
video_stream["url"], output_video_path, pre_allocate_space=pre_allocate_space, use_ram_buffer=use_ram_buffer
)

output_audio_path = Path(tmp_path, f'.tmp-audio-{self.general_info["id"]}.{audio_stream["extension"]}')
output_audio_path = Path(tmp_path, f'.tmp-audio-{self.information["id"]}.{audio_stream["extension"]}')
audio_downloader = TurboDL(
max_connections=max_connections,
connection_speed=connection_speed,
Expand All @@ -637,7 +641,7 @@ def download(
elif video_stream:
if output_path.is_dir():
output_path = Path(
output_path, f'{self.general_info["cleanTitle"]} [{self.general_info["id"]}].{video_stream["extension"]}'
output_path, f'{self.information["cleanTitle"]} [{self.information["id"]}].{video_stream["extension"]}'
)

downloader = TurboDL(
Expand All @@ -655,7 +659,7 @@ def download(
elif audio_stream:
if output_path.is_dir():
output_path = Path(
output_path, f'{self.general_info["cleanTitle"]} [{self.general_info["id"]}].{audio_stream["extension"]}'
output_path, f'{self.information["cleanTitle"]} [{self.information["id"]}].{audio_stream["extension"]}'
)

downloader = TurboDL(
Expand All @@ -673,10 +677,14 @@ def download(


class YouTubeExtractor:
"""A class for extracting data from YouTube URLs and searching for YouTube videos."""
"""
A class for extracting data from YouTube URLs and searching for YouTube videos.
"""

def __init__(self) -> None:
"""Initialize the Extractor class with some regular expressions for analyzing YouTube URLs."""
"""
Initialize the Extractor class with some regular expressions for analyzing YouTube URLs.
"""

self._platform_regex = re_compile(r"(?:https?://)?(?:www\.)?(music\.)?youtube\.com|youtu\.be|youtube\.com/shorts")
self._video_id_regex = re_compile(
Expand Down

0 comments on commit 31e60e2

Please sign in to comment.