comparison kemonopartydownloader.py @ 46:522ad91a230e

Add kemonopartydownloader.py committer: GitHub <noreply@github.com>
author Paper <37962225+mrpapersonic@users.noreply.github.com>
date Sat, 31 Jul 2021 01:38:03 -0400
parents
children 717feded576e
comparison
equal deleted inserted replaced
45:ed1618304c9b 46:522ad91a230e
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
7 import http.cookiejar
8 import time
9 from urllib.error import HTTPError
10
11 def downloadfile(i, x, count):
12 with req.get(f"https://data.kemono.party{x['path']}", stream=True) as r:
13 r.raise_for_status()
14 if not os.path.exists("{4}\\{0}_{1}p_{2}_{3}".format(i["id"], count, i["title"], os.path.basename(x["path"]), output)):
15 with open("{4}\\{0}_{1}p_{2}_{3}".format(i["id"], count, i["title"], os.path.basename(x["path"]), output), "wb") as f:
16 for chunk in r.iter_content(chunk_size=4096):
17 f.write(chunk)
18 print("image {0} successfully downloaded!".format(count))
19 else:
20 print("image {0} already downloaded! skipping...".format(count))
21
22 parser = argparse.ArgumentParser(description="Downloads (deleted) videos from YTPMV creators")
23 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
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
26 parser.add_argument("-o", "--output", help="output folder, defaults to user ID", metavar='<url>')
27 args = parser.parse_args()
28
29 req = requests.Session()
30
31 if args.proxy:
32 if args.proxy[:6] == "socks5":
33 httpproxy = args.proxy
34 httpsproxy = args.proxy
35 elif args.proxy[0:5] == "https":
36 httpsproxy = args.proxy
37 elif args.proxy[0:4] == "http":
38 httpproxy = args.proxy
39 else:
40 print("unknown proxy format! defaulting to HTTP...")
41 httpproxy = args.proxy
42 if httpproxy:
43 req.proxies = {
44 "http": httpproxy,
45 }
46 if httpsproxy:
47 req.proxies["https"] = httpsproxy
48
49 cj = http.cookiejar.MozillaCookieJar(args.cookies)
50 cj.load(ignore_expires=True)
51 req.cookies = cj
52
53 try:
54 int(args.url)
55 print("do not input user IDs here! use a link instead")
56 exit()
57 except Exception:
58 pass
59
60 if args.url.split("/")[-2] == "post":
61 service = args.url.split("/")[-5]
62 user = args.url.split("/")[-3]
63 post = args.url.split("/")[-1]
64 userdata = req.get("https://kemono.party/api/{0}/user/{1}/post/{2}".format(service, user, post)).json()
65 elif args.url.split("/")[-2] == "user":
66 service = args.url.split("/")[-3]
67 user = args.url.split("/")[-1]
68 userdata = req.get("https://kemono.party/api/{0}/user/{1}".format(service, user)).json()
69
70 if not args.output:
71 output = user
72 else:
73 output = args.output
74
75 if not os.path.isdir(output):
76 if os.path.exists(output):
77 os.remove(output)
78 os.mkdir(output)
79
80 for i in userdata:
81 print(i["id"])
82 post = i["id"]
83 count = 0
84 for x in i["attachments"]:
85 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)):
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:
91 downloadfile(i, x, count)
92 break
93 except HTTPError:
94 time.sleep(10)
95 downloadfile(i, x, count)
96 except Exception as e:
97 print(e)
98 time.sleep(10)