Mercurial > codedump
comparison kemonopartydownloader.py @ 51:6eb6c657c7eb
Update kemonopartydownloader.py
committer: GitHub <noreply@github.com>
author | Paper <37962225+mrpapersonic@users.noreply.github.com> |
---|---|
date | Sat, 31 Jul 2021 03:02:29 -0400 |
parents | 717feded576e |
children | ae64a0c8831b |
comparison
equal
deleted
inserted
replaced
50:6661f088a504 | 51:6eb6c657c7eb |
---|---|
1 # example args.url: https://kemono.party/fanbox/user/5375435/post/2511461 | 1 # example args.url: https://kemono.party/fanbox/user/5375435/post/2511461 |
2 import argparse | 2 import argparse |
3 import http.cookiejar | 3 import http.cookiejar |
4 import json | |
5 import os | 4 import os |
6 import re | 5 import re |
7 import requests # pip install requests | 6 import requests # pip install requests |
8 import time | 7 import time |
9 import unicodedata | |
10 import urllib.request | |
11 from urllib.error import HTTPError | 8 from urllib.error import HTTPError |
12 | 9 |
10 | |
13 def sanitize(filename): | 11 def sanitize(filename): |
14 return re.sub("^[\w\-. ]+$", "_", filename) | 12 return re.sub(r"[\/:*?\"<>|]", "_", filename) |
13 | |
15 | 14 |
16 def downloadfile(i, x, count): | 15 def downloadfile(i, x, count): |
17 if not os.path.exists("{4}\\{0}_{1}p_{2}_{3}".format(i["id"], count, sanitize(i["title"]), os.path.basename(x["path"]), output)) or str(os.stat("{4}\\{0}_{1}p_{2}_{3}".format(i["id"], count, sanitize(i["title"]), os.path.basename(x["path"]), output)).st_size) != req.head(f"https://data.kemono.party{x['path']}").headers["Content-Length"]: | 16 filename = "{4}\\{0}_{1}p_{2}_{3}".format(i["id"], count, sanitize(i["title"]), os.path.basename(x["path"]), output) |
18 with req.get(f"https://data.kemono.party{x['path']}", stream=True) as r: | 17 if os.path.exists(filename): |
18 filesize = os.stat(filename).st_size | |
19 else: | |
20 filesize = 0 | |
21 if str(filesize) != req.head(f"https://data.kemono.party{x['path']}").headers["Content-Length"]: | |
22 print("unfinished download") | |
23 with req.get(f"https://data.kemono.party{x['path']}", stream=True, headers={"Range": f"bytes={filesize}-"}) as r: | |
19 r.raise_for_status() | 24 r.raise_for_status() |
20 with open("{4}\\{0}_{1}p_{2}_{3}".format(i["id"], count, sanitize(i["title"]), os.path.basename(x["path"]), output), "wb") as f: | 25 with open(filename, "ab") as f: |
21 for chunk in r.iter_content(chunk_size=4096): | 26 for chunk in r.iter_content(chunk_size=4096): |
22 f.write(chunk) | 27 f.write(chunk) |
23 print("image " + str(count) + " successfully downloaded!") | 28 print("image " + str(count) + " successfully downloaded!") |
29 return | |
24 else: | 30 else: |
25 print("image " + str(count) + " already downloaded!") | 31 print("image " + str(count) + " already downloaded!") |
32 return | |
33 | |
26 | 34 |
27 parser = argparse.ArgumentParser(description="Downloads (deleted) videos from YTPMV creators") | 35 parser = argparse.ArgumentParser(description="Downloads (deleted) videos from YTPMV creators") |
28 parser.add_argument("-u", "--url", help="user URL", metavar='<url>', required=True) | 36 parser.add_argument("-u", "--url", help="user URL", metavar='<url>', required=True) |
29 parser.add_argument("-c", "--cookies", help="", metavar='<url>', required=True) # required because of DDoS-GUARD | 37 parser.add_argument("-c", "--cookies", help="", metavar='<url>', required=True) # required because of DDoS-GUARD |
30 parser.add_argument("-p", "--proxy", help="proxy\n supported types: http, https, socks5 (requires pysocks)", metavar='<url>') # SOCKS proxy support is through PySocks - pip install pysocks | 38 parser.add_argument("-p", "--proxy", help="proxy\n supported types: http, https, socks5 (requires pysocks)", metavar='<url>') # SOCKS proxy support is through PySocks - pip install pysocks |