-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgz3.pyw
206 lines (183 loc) · 5.44 KB
/
gz3.pyw
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# -*- coding: utf-8 -*-
import requests,os,shutil,sys,threading,time,clipboard
from tkinter import *
proxy = {'http':'http://127.0.0.1:8002'}
die = 0
def tryLink(link):
global die
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 += 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)
# Very lousy way of multi threading
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(bookname+ssid+' has '+die+' pages incomplete')
wd = Tk() # 總窗口
wd.title("GZU ZJK")
rows = 10 # 開 10 行
# 獲取剪貼板
# 每一條的框架
class pair(object):
# 文本框
def __init__(self,r):
self.r = r
self.ent = Entry(wd,width=50)
self.ent.grid(row=self.r,column=0)
# 刪舊加新
def newText(self):
self.ent.delete(0,END)
self.ent.insert(0,clipboard.paste())
# 按鈕及行爲
def fra(self):
but = Button(wd,text='Paste',command=self.newText)
but.grid(row=self.r,column=1)
# 根據框架生成 rows 條
# 命令形如 p1 = pair(1) 和 p1.fra()
for i in range(1,rows+1):
exec('p' + str(i) + '=pair(' + str(i) + ')')
exec('p' + str(i) + '.fra()')
# 一串形如 p1.ent.get() 的命令
def dl():
for i in range(1,rows+1):
exec('getBook(p' + str(i) + '.ent.get())')
# 最終按鈕
dlB = Button(wd,text='Start Downloading',command=dl)
dlB.grid(row=rows+1,column=0)
wd.update() # 窗口的大小已改變,更新變量數據
wdW = wd.winfo_width()
wdH = wd.winfo_height()
scH = wd.winfo_screenheight()
wdGeo = '%dx%d+%d+%d'%(wdW, wdH, 0, scH-wdH-100)
wd.geometry(wdGeo)
wd.mainloop()