Mercurial > codedump
comparison kemonopartydownloader.py @ 49:717feded576e
Update kemonopartydownloader.py
committer: GitHub <noreply@github.com>
author | Paper <37962225+mrpapersonic@users.noreply.github.com> |
---|---|
date | Sat, 31 Jul 2021 02:24:22 -0400 |
parents | 522ad91a230e |
children | 6eb6c657c7eb |
comparison
equal
deleted
inserted
replaced
48:267e3e6bba63 | 49:717feded576e |
---|---|
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 json | |
3 import urllib.request | |
4 import os | |
5 import requests # pip install requests | |
6 import argparse | 2 import argparse |
7 import http.cookiejar | 3 import http.cookiejar |
4 import json | |
5 import os | |
6 import re | |
7 import requests # pip install requests | |
8 import time | 8 import time |
9 import unicodedata | |
10 import urllib.request | |
9 from urllib.error import HTTPError | 11 from urllib.error import HTTPError |
10 | 12 |
13 def sanitize(filename): | |
14 return re.sub("^[\w\-. ]+$", "_", filename) | |
15 | |
11 def downloadfile(i, x, count): | 16 def downloadfile(i, x, count): |
12 with req.get(f"https://data.kemono.party{x['path']}", stream=True) as r: | 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"]: |
13 r.raise_for_status() | 18 with req.get(f"https://data.kemono.party{x['path']}", stream=True) as r: |
14 if not os.path.exists("{4}\\{0}_{1}p_{2}_{3}".format(i["id"], count, i["title"], os.path.basename(x["path"]), output)): | 19 r.raise_for_status() |
15 with open("{4}\\{0}_{1}p_{2}_{3}".format(i["id"], count, i["title"], os.path.basename(x["path"]), output), "wb") as f: | 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: |
16 for chunk in r.iter_content(chunk_size=4096): | 21 for chunk in r.iter_content(chunk_size=4096): |
17 f.write(chunk) | 22 f.write(chunk) |
18 print("image {0} successfully downloaded!".format(count)) | 23 print("image " + str(count) + " successfully downloaded!") |
19 else: | 24 else: |
20 print("image {0} already downloaded! skipping...".format(count)) | 25 print("image " + str(count) + " already downloaded!") |
21 | 26 |
22 parser = argparse.ArgumentParser(description="Downloads (deleted) videos from YTPMV creators") | 27 parser = argparse.ArgumentParser(description="Downloads (deleted) videos from YTPMV creators") |
23 parser.add_argument("-u", "--url", help="user URL", metavar='<url>', required=True) | 28 parser.add_argument("-u", "--url", help="user URL", metavar='<url>', required=True) |
24 parser.add_argument("-c", "--cookies", help="", metavar='<url>', required=True) # required because of DDoS-GUARD | 29 parser.add_argument("-c", "--cookies", help="", metavar='<url>', required=True) # required because of DDoS-GUARD |
25 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 | 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 |
81 print(i["id"]) | 86 print(i["id"]) |
82 post = i["id"] | 87 post = i["id"] |
83 count = 0 | 88 count = 0 |
84 for x in i["attachments"]: | 89 for x in i["attachments"]: |
85 count += 1 | 90 count += 1 |
86 if os.path.exists("{4}\\{0}_{1}p_{2}_{3}".format(int(i["id"]) - 1, count, i["title"], os.path.basename(x["path"]), output)): | 91 while not os.path.exists("{4}\\{0}_{1}p_{2}_{3}".format(int(i["id"]) - 1, count, sanitize(i["title"]), os.path.basename(x["path"]), output)): |
87 print("image {0} already downloaded! skipping...".format(count)) | |
88 continue | |
89 while not os.path.exists("{4}\\{0}_{1}p_{2}_{3}".format(int(i["id"]) - 1, count, i["title"], os.path.basename(x["path"]), output)): | |
90 try: | 92 try: |
91 downloadfile(i, x, count) | 93 downloadfile(i, x, count) |
92 break | 94 break |
93 except HTTPError: | 95 except HTTPError: |
94 time.sleep(10) | 96 time.sleep(10) |