changeset 68:a43ed076b28f

[channeldownloader.py] Implement HTTPError to circumvent Python 2 weirdness kind of hacky because we're just giving it none for everything LOL committer: GitHub <noreply@github.com>
author Paper <37962225+mrpapersonic@users.noreply.github.com>
date Wed, 18 May 2022 20:05:47 -0400
parents 9636d5dee08c
children 63e6bc911606
files channeldownloader.py
diffstat 1 files changed, 21 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/channeldownloader.py	Wed May 18 18:57:58 2022 -0400
+++ b/channeldownloader.py	Wed May 18 20:05:47 2022 -0400
@@ -13,6 +13,7 @@
 import glob
 import os
 import re
+import time
 try:
     import urllib.request as compat_urllib
     from urllib.error import HTTPError
@@ -61,6 +62,17 @@
     else:
         return json.loads(open(path, "r", encoding="utf-8").read())
 
+def reporthook(count, block_size, total_size):
+    global start_time
+    if count == 0:
+        start_time = time.time()
+        return
+    duration = time.time() - start_time
+    progress_size = int(count * block_size)
+    speed = int(progress_size / (1024 * duration))
+    percent = int(count * block_size * 100 / total_size)
+    print(" downloading %d%%        \r" % (percent), end="")
+
 
 parser = argparse.ArgumentParser(description="Downloads (deleted) videos from YTPMV creators")
 parser.add_argument("-c", "--channel", help="channel URL", metavar='<url>', required=True)
@@ -82,7 +94,7 @@
     os.mkdir(output)
 
 ytdl_opts = {
-    "outtmpl": "%s/%(title)s-%(id)s.%(ext)s" % (output),
+    "outtmpl": output + "/%(title)s-%(id)s.%(ext)s",
     "retries": 100,
     "nooverwrites": True,
     "call_home": False,
@@ -152,8 +164,13 @@
                     contenttype = headers.info().getheader("Content-Type")
                 else:
                     contenttype = headers.getheader("Content-Type")
-                ext = "webm" if contenttype == "video/webm" else "mp4"
-                compat_urllib.urlretrieve("https://web.archive.org/web/2oe_/http://wayback-fakeurl.archive.org/yt/%s" % i["id"], "%s/%s-%s.%s" % (output, sanitize_filename(i["title"], restricted=True), i["id"], ext))
+                if contenttype == "video/webm":
+                    ext = "webm"
+                elif contenttype == "video/mp4":
+                    ext = "mp4"
+                else:
+                    raise HTTPError(url=None, code=None, msg=None, hdrs=None, fp=None)
+                compat_urllib.urlretrieve("https://web.archive.org/web/2oe_/http://wayback-fakeurl.archive.org/yt/%s" % i["id"], "%s/%s-%s.%s" % (output, sanitize_filename(i["title"], restricted=True), i["id"], ext), reporthook)
                 print(" downloaded %s-%s.%s" % (sanitize_filename(i["title"], restricted=True), i["id"], ext))
             except HTTPError:
                 print(" video not available on the Wayback Machine!")
@@ -164,3 +181,4 @@
             with open("%s/%s-%s.info.json" % (output, sanitize_filename(i["title"], restricted=True), i["id"]), "w", encoding="utf-8") as jsonfile:
                 jsonfile.write(json.dumps(i, ensure_ascii=False).decode('utf-8'))
                 print(" saved %s" % os.path.basename(jsonfile.name))
+