Mercurial > codedump
comparison channeldownloader.py @ 70:eafe13de3f76
Update channeldownloader.py
committer: GitHub <noreply@github.com>
author | Paper <37962225+mrpapersonic@users.noreply.github.com> |
---|---|
date | Wed, 01 Jun 2022 07:19:02 -0400 |
parents | 63e6bc911606 |
children | 80bd4a99ea00 |
comparison
equal
deleted
inserted
replaced
69:63e6bc911606 | 70:eafe13de3f76 |
---|---|
72 percent = int(count * block_size * 100 / total_size) | 72 percent = int(count * block_size * 100 / total_size) |
73 print(" downloading %d%% \r" % (percent), end="") | 73 print(" downloading %d%% \r" % (percent), end="") |
74 | 74 |
75 | 75 |
76 parser = argparse.ArgumentParser(description="Downloads (deleted) videos from YTPMV creators") | 76 parser = argparse.ArgumentParser(description="Downloads (deleted) videos from YTPMV creators") |
77 parser.add_argument("-c", "--channel", help="channel URL", metavar='<url>', required=True) | 77 parser.add_argument("-c", "--channel", help="channel URL", metavar="<url>", required=True) |
78 parser.add_argument("-d", "--database", help="json database (https://finnrepo.a2hosted.com/YTPMV_Database)", metavar='<path>', required=True) | 78 parser.add_argument("-d", "--database", help="json database (https://finnrepo.a2hosted.com/YTPMV_Database)", metavar="<path>", required=True) |
79 parser.add_argument("-o", "--output", help="output directory, defaults to the channel ID", metavar='<output>') | 79 parser.add_argument("-o", "--output", help="output directory, defaults to the channel ID", metavar="<output>") |
80 args = parser.parse_args() | 80 args = parser.parse_args() |
81 | 81 |
82 if args.channel[:8] == "https://" or args.channel[:7] == "http://": | 82 if args.channel[:8] == "https://" or args.channel[:7] == "http://": |
83 channel = args.channel.split("/")[-1] | 83 channel = args.channel.split("/")[-1] |
84 else: | 84 else: |
118 | 118 |
119 for i in load_split_files(args.database)["videos"]: | 119 for i in load_split_files(args.database)["videos"]: |
120 uploader = i["uploader_id"] if "uploader_id" in i else None | 120 uploader = i["uploader_id"] if "uploader_id" in i else None |
121 if uploader == channel: | 121 if uploader == channel: |
122 print("%s:" % i["id"]) | 122 print("%s:" % i["id"]) |
123 if os.path.exists(output + "/" + sanitize_filename(i["title"], restricted=True) + "-" + i["id"] + ".info.json"): | 123 # :skull: |
124 # todo: put this in a function? | |
125 if any(x in os.listdir(output) for x in [sanitize_filename(i["title"] + "-" + i["id"] + ".mp4", restricted=True), | |
126 sanitize_filename(i["title"] + "-" + i["id"] + ".mkv", restricted=True), | |
127 sanitize_filename(i["title"] + "-" + i["id"] + ".webm", restricted=True)]): | |
124 print(" video already downloaded!") | 128 print(" video already downloaded!") |
125 continue | 129 continue |
126 # this code is *really* ugly... todo a rewrite? | 130 # this code is *really* ugly... todo a rewrite? |
127 with youtube_dl.YoutubeDL(ytdl_opts) as ytdl: | 131 with youtube_dl.YoutubeDL(ytdl_opts) as ytdl: |
128 try: | 132 try: |
147 os.rename(output + "/" + fname, output + "/" + sanitize_filename(i["title"], restricted=True) + "-" + fname) | 151 os.rename(output + "/" + fname, output + "/" + sanitize_filename(i["title"], restricted=True) + "-" + fname) |
148 else: | 152 else: |
149 print("ID file not found!") | 153 print("ID file not found!") |
150 else: | 154 else: |
151 print(" video does not have a Internet Archive page! attempting to download from the Wayback Machine...") | 155 print(" video does not have a Internet Archive page! attempting to download from the Wayback Machine...") |
152 try: | 156 try: # we could use yt-dlp's extractor, but then we would need to craft a fake wayback machine url, |
157 # and we wouldn't even know if it worked. so let's continue using our little "hack" | |
153 headers = compat_urllib.urlopen("https://web.archive.org/web/2oe_/http://wayback-fakeurl.archive.org/yt/%s" % i["id"]) | 158 headers = compat_urllib.urlopen("https://web.archive.org/web/2oe_/http://wayback-fakeurl.archive.org/yt/%s" % i["id"]) |
154 if hasattr(headers.info(), "getheader"): | 159 if hasattr(headers.info(), "getheader"): |
155 contenttype = headers.info().getheader("Content-Type") | 160 contenttype = headers.info().getheader("Content-Type") |
156 else: | 161 else: |
157 contenttype = headers.getheader("Content-Type") | 162 contenttype = headers.getheader("Content-Type") |
168 except Exception as e: | 173 except Exception as e: |
169 print(" unknown error downloading video!\n") | 174 print(" unknown error downloading video!\n") |
170 print(e) | 175 print(e) |
171 # metadata | 176 # metadata |
172 with open("%s/%s-%s.info.json" % (output, sanitize_filename(i["title"], restricted=True), i["id"]), "w", encoding="utf-8") as jsonfile: | 177 with open("%s/%s-%s.info.json" % (output, sanitize_filename(i["title"], restricted=True), i["id"]), "w", encoding="utf-8") as jsonfile: |
173 jsonfile.write(json.dumps(i, ensure_ascii=False).decode('utf-8')) | 178 jsonfile.write(json.dumps(i).decode("utf-8")) |
174 print(" saved %s" % os.path.basename(jsonfile.name)) | 179 print(" saved %s" % os.path.basename(jsonfile.name)) |
175 | 180 |