-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathdownload_illusts.py
57 lines (44 loc) · 1.75 KB
/
download_illusts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
import os
import sys
from pixivpy3 import AppPixivAPI, ByPassSniApi
sys.dont_write_bytecode = True
# get your refresh_token, and replace _REFRESH_TOKEN
# https://github.com/upbit/pixivpy/issues/158#issuecomment-778919084
_REFRESH_TOKEN = "0zeYA-PllRYp1tfrsq_w3vHGU1rPy237JMf5oDt73c4"
def main():
sni = False
if not sni:
api = AppPixivAPI()
else:
api = ByPassSniApi() # Same as AppPixivAPI, but bypass the GFW
api.require_appapi_hosts()
api.auth(refresh_token=_REFRESH_TOKEN)
# get rankings
json_result = api.illust_ranking("day", date="2019-01-01")
directory = "illusts"
if not os.path.exists(directory):
os.makedirs(directory)
# download top3 day rankings to 'illusts' dir
for idx, illust in enumerate(json_result.illusts[:4]):
image_url = illust.meta_single_page.get("original_image_url", illust.image_urls.large)
print("{}: {}".format(illust.title, image_url))
# try four args in MR#102
if idx == 0:
api.download(image_url, path=directory, name=None)
elif idx == 1:
url_basename = os.path.basename(image_url)
extension = os.path.splitext(url_basename)[1]
name = "illust_id_%d_%s%s" % (illust.id, illust.title, extension)
api.download(image_url, path=directory, name=name)
elif idx == 2:
api.download(image_url, path=directory, fname="illust_%s.jpg" % (illust.id))
else:
# path will not work due to fname is a handler
api.download(
image_url,
path="/foo/bar",
fname=open("{}/illust_{}.jpg".format(directory, illust.id), "wb"),
)
if __name__ == "__main__":
main()