]> Skullheadx's Git Forge - youtube-downloader.git/commitdiff
make things more efficient
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 14 Oct 2024 03:05:01 +0000 (23:05 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 14 Oct 2024 03:05:01 +0000 (23:05 -0400)
ytdl/__main__.py
ytdl/funcmodule.py

index 698d70ff64459065c2aa72a6a99db6ffdb956ec6..6a653b1f4f15f03b539961390c8b9171be4b39b1 100644 (file)
@@ -1,6 +1,5 @@
 import sys
-from .funcmodule import check_playlist, links_work, get_audio_streams, download_audio_streams, \
-    get_metadata
+from .funcmodule import check_playlist, get_audio_metadata_streams, download_audio_streams
 
 
 
@@ -28,15 +27,9 @@ def main():
     links = check_playlist(links)
     assert len(links) > 0, "Should be at least one song in playlist"
 
-    print("Checking if links exist")
-    assert links_work(links), "Links don't work :("
-
-    print("Getting audio streams")
-    streams = get_audio_streams(links)
-    assert len(streams) > 0, "was not able to get audio streams"
-
-    print("Getting video metadata")
-    metadata = get_metadata(links)
+    print("Getting audio streams and metadata")
+    streams, metadata = get_audio_metadata_streams(links)
+    assert len(streams) > 0, "was not able to get audio streams / metadata"
     assert len(metadata) == len(streams), "make sure metadata for every stream"
 
     if arg == "-d":
index d398f7c5e0b0318b306d93b1774cd984d2e53bb7..f3a04b7652d735d60610c417590e6d811715f330 100644 (file)
@@ -14,25 +14,16 @@ def check_playlist(links):
     return links
 
 
-def links_work(links):
-    for link in links:
-        YouTube(link).check_availability()
-    return True
-
-
-def get_audio_streams(links):
+def get_audio_metadata_streams(links):
     audio_streams = []
+    metadata = []
+
     for link in links:
         yt = YouTube(link)
+        yt.check_availability()
+        print(f"Fetching stream for {yt.title}")
         assert len(yt.streams.filter(only_audio=True)) > 0, "No available audio streams"
         audio_streams.append(yt.streams.filter(only_audio=True).order_by("abr").last())
-    return audio_streams
-
-
-def get_metadata(links):
-    metadata = []
-    for link in links:
-        yt = YouTube(link)
         metadata.append(
             {
                 "title": yt.title,
@@ -42,8 +33,7 @@ def get_metadata(links):
                 "views": yt.views
             }
         )
-    return metadata
-
+    return audio_streams, metadata
 
 def big_num_format(num):  # https://stackoverflow.com/a/579376
     magnitude = 0
@@ -55,6 +45,7 @@ def big_num_format(num):  # https://stackoverflow.com/a/579376
 
 def download_audio_streams(audio_streams, metadata):
     for audio_stream, md in zip(audio_streams, metadata):
+        print(f"Downloading audio stream for {audio_stream.title}")
         audio_stream.download()
         data = requests.get(md["thumbnail_url"]).content
         f = open('thumbnail.jpg', 'wb')