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