]> Skullheadx's Git Forge - youtube-downloader.git/commitdiff
add views
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 14 Oct 2024 02:51:16 +0000 (22:51 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 14 Oct 2024 02:51:16 +0000 (22:51 -0400)
.gitignore
ytdl/__main__.py
ytdl/funcmodule.py

index 18a6ba811c1d5a65e6a8232b212ac4a8f757f0db..7e413f7a1fb231f4bd364a36d66eed5682c61496 100644 (file)
@@ -26,3 +26,5 @@ ytdl/__pycache__/__init__.cpython-312.pyc
 ytdl/__pycache__/__main__.cpython-312.pyc
 ytdl/__pycache__/classmodule.cpython-312.pyc
 ytdl/__pycache__/funcmodule.cpython-312.pyc
+/ytdl.egg-info
+/ytdl/__pycache__
index 85b0add244a95472d0837eba8e58a34fc121b0f2..7108f53e8da82bac56653bf840d90c5bfb25e813 100644 (file)
@@ -1,22 +1,22 @@
-from .__init__ import *
 import sys
-from .classmodule import MyClass
-from .funcmodule import check_playlist, links_work, download_audio
+from .funcmodule import check_playlist, links_work, get_audio_streams, download_audio_streams, \
+    get_metadata
+
+
 
 def main():
     args = sys.argv[1:]
+    modes = ["-d", "-a", "-v", "-av"]
 
     links = []
     mode = "-d"
     for arg in args:
-        print('passed argument :: {}'.format(arg))
         if arg in modes:
             mode = arg
 
         if "youtube.com" in arg:
             links.extend(arg.split(" "))
-    
-    
+
     assert len(links) > 0, "Should pass at least one link as arg"
     assert mode in modes, f"Mode should be one of {modes}"
 
@@ -26,13 +26,19 @@ def main():
 
     links = check_playlist(links)
     assert len(links) > 0, "Should be at least one song in playlist"
-  
-    #assert links_work(links), "Links don't work :("
-    
+
+    assert links_work(links), "Links don't work :("
+
+    streams = get_audio_streams(links)
+    assert len(streams) > 0, "was not able to get audio streams"
+
+    metadata = get_metadata(links)
+    assert len(metadata) == len(streams), "make sure metadata for every stream"
+
     if arg == "-d":
         pass
     elif arg == "-a":
-        download_audio(links)
+        download_audio_streams(streams, metadata)
     elif arg == "-v":
         pass
     elif arg == "-av":
@@ -41,5 +47,3 @@ def main():
 
 if __name__ == '__main__':
     main()
-
-
index 0720edd58e01c3e6ac76970fd19da1d8f34ffc34..ed565001e708d1444ba142b47d615158b3e48bbf 100644 (file)
@@ -4,6 +4,7 @@ import requests
 import subprocess
 import os
 
+
 def check_playlist(links):
     for link in links:
         if "playlist" in link:
@@ -39,13 +40,20 @@ def get_metadata(links):
                 "artist": yt.author,
                 "thumbnail_url": yt.thumbnail_url,
                 "publish_date": yt.publish_date,
-                "rating": yt.rating,
                 "views": yt.views
             }
         )
     return metadata
 
 
+def big_num_format(num):  # https://stackoverflow.com/a/579376
+    magnitude = 0
+    while abs(num) >= 1000:
+        magnitude += 1
+        num /= 1000.0
+    return '%.1f%s' % (num, ['', 'K', 'M', 'B'][magnitude])
+
+
 def download_audio_streams(audio_streams, metadata):
     for audio_stream, md in zip(audio_streams, metadata):
         audio_stream.download()
@@ -63,6 +71,7 @@ def download_audio_streams(audio_streams, metadata):
             '-metadata', f'title={audio_stream.title}',
             '-metadata', f'artist={md["artist"]}',
             '-metadata', f'date={md["publish_date"]}',
+            '-metadata', f'comment={big_num_format(md["views"]) + " views"}',
             "downloads/" + audio_stream.title + ".mp4",
             '-y'
         ]
@@ -71,5 +80,3 @@ def download_audio_streams(audio_streams, metadata):
         subprocess.run(command)
         os.remove("thumbnail.jpg")
         os.remove(audio_stream.default_filename)
-
-