-
Notifications
You must be signed in to change notification settings - Fork 7
/
gz1.py
167 lines (148 loc) · 4.4 KB
/
gz1.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- coding: utf-8 -*-
import requests,os,shutil,sys,threading,time
proxy = {'http':'http://127.0.0.1:8002'}
def tryLink(link):
global die
die = 0
try:
r = requests.get(link, proxies=proxy, stream=True, timeout=None)
except BaseException:
time.sleep(1)
try:
r = requests.get(link, proxies=proxy, stream=True, timeout=None)
except BaseException:
print(link)
die = die + 1
return
else:
return r
else:
return r
def pDown(link, Path):
s = tryLink(link+'.png')
if s.status_code == 200:
with open(Path, 'wb') as fd:
for chunk in s.iter_content(chunk_size=1024):
fd.write(chunk)
del s
elif s.status_code == 404:
tryLink(link+'.jpg')
if s.status_code == 200:
with open(Path, 'wb') as fd:
for chunk in s.iter_content(chunk_size=1024):
fd.write(chunk)
del r
elif s.status_code == 404:
return True
def AttDown(Atts, ssFolder, BasicURL):
FileExist=os.path.isfile(ssFolder+Atts+'.pdg')
if FileExist == False:
pDown(BasicURL+Atts,ssFolder+Atts+'.pdg')
def fowDown(ssFolder, BasicURL):
for num in range(1, 1000):
pgNum=str(num).zfill(3)
ssFile=ssFolder+'fow'+pgNum+'.pdg'
FileExist=os.path.isfile(ssFile)
if FileExist == True:
FileSize=os.path.getsize(ssFile)
if FileSize == 0:
if pDown(BasicURL+'fow'+pgNum, ssFile):
break
else:
if pDown(BasicURL+'fow'+pgNum, ssFile):
break
def tocDown(ssFolder, BasicURL):
for num in range(1, 10000):
pgNum=str(num).zfill(5)
ssFile=ssFolder+'!'+pgNum+'.pdg'
FileExist=os.path.isfile(ssFile)
if FileExist == True:
FileSize=os.path.getsize(ssFile)
if FileSize == 0:
if pDown(BasicURL+'!'+pgNum, ssFile):
break
else:
if pDown(BasicURL+'!'+pgNum, ssFile):
break
class attsDown(threading.Thread):
def __init__(self, ssFolder, BasicURL):
threading.Thread.__init__(self)
self.ssFolder = ssFolder
self.BasicURL = BasicURL
def run(self):
AttDown('cov001', self.ssFolder, self.BasicURL)
AttDown('cov002', self.ssFolder, self.BasicURL)
AttDown('bok001', self.ssFolder, self.BasicURL)
AttDown('bok002', self.ssFolder, self.BasicURL)
AttDown('leg001', self.ssFolder, self.BasicURL)
AttDown('bac001', self.ssFolder, self.BasicURL)
fowDown(self.ssFolder, self.BasicURL)
tocDown(self.ssFolder, self.BasicURL)
class pageDown(threading.Thread):
def __init__(self, firstpage, lastpage, ssFolder, BasicURL):
threading.Thread.__init__(self)
self.ssFolder = ssFolder
self.BasicURL = BasicURL
self.firstpage = firstpage
self.lastpage = lastpage
def run(self):
for num in range(self.firstpage, self.lastpage+1):
pgNum=str(num).zfill(6)
ssFile=self.ssFolder+pgNum+'.pdg'
FileExist=os.path.isfile(ssFile)
if FileExist == True:
FileSize=os.path.getsize(ssFile)
if FileSize == 0:
pDown(self.BasicURL+pgNum, ssFile)
else:
pDown(self.BasicURL+pgNum, ssFile)
def getBook(gzBook):
#釋放變量
BasicURL=gzBook[0]
firstpage=int(gzBook[1])
lastpage=int(gzBook[2])
ssid=gzBook[3]
bookname=gzBook[4]
#文件夾
ssFolder='F:\\ss\\'+bookname+'_'+ssid+'\\'
FolderExist=os.path.isdir(ssFolder)
if FolderExist == False:
os.mkdir(r'f:/ss/'+bookname+'_'+ssid+'/')
#生成bookinfo
BookinfoExist=os.path.isfile(ssFolder+'Bookinfo.dat')
if BookinfoExist == False:
Bookinfo=open(ssFolder+'Bookinfo.dat', 'w+')
Bookinfo.write('[General Information]'+'\n')
Bookinfo.write('书名='+bookname+'\n')
Bookinfo.write('作者='+'\n')
Bookinfo.write('页数='+str(lastpage)+'\n')
Bookinfo.write('SS号='+ssid+'\n')
Bookinfo.write('DX号='+'\n')
Bookinfo.write('出版日期='+'\n')
Bookinfo.write('出版社='+'\n')
Bookinfo.write(BasicURL+'\n')
Bookinfo.close()
thread1 = attsDown(ssFolder, BasicURL)
thread2 = pageDown(firstpage, firstpage+(lastpage-firstpage)//4, ssFolder, BasicURL)
thread3 = pageDown(firstpage+(lastpage-firstpage)//4, firstpage+(lastpage-firstpage)//4*2, ssFolder, BasicURL)
thread4 = pageDown(firstpage+(lastpage-firstpage)//4*2, firstpage+(lastpage-firstpage)//4*3, ssFolder, BasicURL)
thread5 = pageDown(firstpage+(lastpage-firstpage)//4*3, lastpage+1, ssFolder, BasicURL)
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()
thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join()
if die == 0:
shutil.move(ssFolder,'F:\\ss\\done\\'+bookname+'_'+ssid+'\\')
else:
print(die+' pages incomplete')
#與bat對接
def main():
getBook(sys.argv[1:])
if __name__ == '__main__':
main()