forked from Volvagia356/pixiv-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixivdl.py
executable file
·89 lines (80 loc) · 2.46 KB
/
pixivdl.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
import os,sys,errno
import urllib2
import pickle
from pixiv_api import Pixiv
config_dir=os.environ.get("XDG_CONFIG_HOME",os.path.expanduser("~/.config"))
def print_welcome():
print 'pixiv Downloader (C) 2013 Volvagia356 (C) 2014 Theonik'
print ''
def print_usage():
print "Usage: {} artist_id [directory]".format(sys.argv[0].split('/')[-1])
print ''
def check_version():
if sys.hexversion<0x02070000:
print "Python 2.7 is required!"
print ''
sys.exit()
def get_session_config():
session_file=config_dir+"/pixivdl/session"
try:
session_id=open(session_file).readline().strip()
return session_id
except IOError:
print "Login information not found."
print "Please refer to 'Logging in' in README"
print ''
def download_work(work,prefix):
requests=work.get_files()
page=0
for request in requests:
if len(requests)>1:
filename="{}_p{}.{}".format(work.id,page,work.format)
page+=1
else:
if 'zip' in request.get_selector():
filename="{}.zip".format(work.id)
else:
filename="{}.{}".format(work.id,work.format)
full_path=prefix+'/'+filename
if os.path.isfile(full_path):
print filename, "already exists."
continue
print "Downloading", filename
try:
img=urllib2.urlopen(request)
f=open(full_path,'wb')
f.write(img.read())
except urllib2.HTTPError as e:
print e
print request.get_full_url()
def main():
print_welcome()
check_version()
if len(sys.argv)<2:
print_usage()
sys.exit()
artist_id=sys.argv[1]
session_id=get_session_config()
p=Pixiv(session_id)
works=p.get_works_all(artist_id)
if len(sys.argv)==3:
directory=sys.argv[2]
else:
if works:
directory = "./img/{}-(ID_{})".format(works[0].artist_nickname,artist_id)
try:
os.makedirs(directory)
os.makedirs(directory)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
else:
print 'This request is empty.'
sys.exit()
for work in works:
download_work(work,directory)
pickle.dump(works,open(directory+"/metadata.pickle",'wb'))
print ''
if __name__=="__main__":
main()