0%

E站下载器的开发与使用

闲着无聊把我之前写的e站下载器重构了一遍,顺便做了个简化的界面,记录一下开发过程。
同时给大家演示一下这个程序怎么使用。

E站下载器使用

软件下载链接: EH下载器
注:暂时只支持里站下载

视频教程

# 开发过程

文件代码

配置文件部分

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
#默认保存地址为空 保存目录为软件下的Download目录
#保存目录可自行配置 例:D:\Download
[dirpath]
dirpath=

#图片下载并发数上限
[sysinfo]
max_threading=30
#Exhentai账号信息
[userinfo]
igneous:df9724040
ipb_member_id:4483572
ipb_pass_hash:b1d7d5acd649a01a1643124c8a0918a8
#代理设置 true 为开 false 为关
[proxy]
bool=false
modle=socks5
ip=127.0.0.1
port=1088
[headers]
User-Agent=Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6

#是否打包压缩
[pack]
bool=true
model=zip

文件下载部分

EHDownload.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
#导入模块
import requests
import os
import re
import threading
import configparser
import zipfile
import tkinter.messagebox as messagebox
#新增下载类
class EHDownload(object):

def __init__(self,eurl):
self.eurl = eurl
self.eurl_model = eurl[8:16]
self.all_init()

#读取配置文件部分
def all_init(self):
self.config_file = configparser.ConfigParser()
self.config_file.read(os.path.join(os.path.abspath('.'),'config.ini'))
print(os.path.abspath('.'))
self.max_threading = int( self.config_file.get('sysinfo','max_threading') )
self.proxy_init()
self.cookies_init()
self.headers_init()
self.dir_path_init()
def proxy_init(self):
self.proxies = {}
if self.config_file.get('proxy','bool') != 'true' :
self.proxy_bool=False
print("不使用代理")
else:
self.proxy_bool=True
self.proxies['http'] = self.config_file.get('proxy','modle') + '://' + self.config_file.get('proxy','ip') + ':'+ self.config_file.get('proxy','port');
self.proxies['https'] = self.proxies['http']
def cookies_init(self):
self.cookies={}
self.cookies['igneous']=self.config_file.get('userinfo','igneous')
self.cookies['ipb_member_id']=self.config_file.get('userinfo','ipb_member_id')
self.cookies['ipb_pass_hash']=self.config_file.get('userinfo','ipb_pass_hash')
def headers_init(self):
self.headers={}
self.headers['User-Agent'] = self.config_file.get('headers','User-Agent')
def dir_path_init(self):
try:
self.dirpath=self.config_file.get('dirpath','dirpath')
if self.dirpath == '':
self.dirpath=os.path.abspath('.') + '/Download'
except Exception as ex:
self.dirpath=os.path.abspath('.') + '/Download'
try:
os.makedirs(self.dirpath)
except Exception as ex:
pass
#文件下载部分
def Download(self):
self.Download_Message()
print("图片数量" + str(self.page_count))
if self.page_count > 40:
self.Download_Message_f()
print(len(self.message_text))
self.Download_Img()
self.PackFiles()
#首页下载
def Download_Message(self):
self.message_text=[]
message_text = requests.get(self.eurl,headers=self.headers,proxies = self.proxies,cookies=self.cookies).text
#正则获取本子名等信息
regex = r'<h1 id="g(.*?)<'
qa = re.compile(regex)
filename = re.findall(qa,message_text)
self.filename_en = filename[0][3:]
self.filename_jp = filename[1][3:]
self.message_text.append(message_text)

#正则获得图片数量
regex_pagecount = r'Length:</td><td class="gdt2">(.*?) pages'
pa_pagecount = re.compile(regex_pagecount)
self.page_count = int(re.findall(pa_pagecount,message_text)[0])

#待更新
self.message1 = '111'
self.message2 = '222'
#生成新文件夹目录
self.dira = self.filename_jp.lstrip().replace('?',' ').replace('*',' ').replace(':',' ').replace('"',' ').replace('<',' ').replace('>',' ').replace('\\',' ').replace('/',' ').replace('|',' ')
self.dira = os.path.join(self.dirpath,self.dira)
try :
os.mkdir(self.dira)
except Exception as ex:
print()
print(self.dira)
#分页下载
def Download_Message_f(self):
for i in range(1,int((self.page_count-1)/40)+1):
eurl_n = self.eurl + '?p=' + str(i)
message_text = requests.get(eurl_n,headers=self.headers,proxies = self.proxies,cookies=self.cookies).text
self.message_text.append(message_text)
#图片下载
def Download_Img(self):
i = 0
pool = []
regex = r'href="https:\/\/exhentai.org\/s\/(.*?)"'
pa = re.compile(regex)
for t in self.message_text:
ma = re.findall(pa,t)
for url_message in ma:
pool.append(threading.Thread(target=EHDownload.Download_Img_One,args=(self,url_message,i)) )
i = i + 1
for x in pool:
x.start()
while True:
if( len(threading.enumerate())<self.max_threading ):
break
for x in pool:
x.join()

def Download_Img_One(self,url_message,i):
pageurl = "https://exhentai.org/s/" + url_message
pagetext = requests.get(pageurl,headers=self.headers,proxies = self.proxies,cookies=self.cookies).text
regex2 = r'<img id="img" src="(.*?)" style'
paa = re.compile(regex2)
maa = re.findall(paa,pagetext)
print(maa[0])
imgdate = requests.get(maa[0],headers=self.headers,proxies = self.proxies,cookies=self.cookies)
if imgdate.status_code != 200 :
return
imgdate= imgdate.content
with open(self.dira+'/'+str(i)+'.'+maa[0][-3:],'wb') as f:
f.write(imgdate)
def PackFiles(self):
if self.config_file.get('pack','bool') != 'true' :
return
outFullName = self.dira + '.zip'
zip = zipfile.ZipFile(outFullName,"w",zipfile.ZIP_DEFLATED)
for path,dirnames,filenames in os.walk(self.dira):
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
fpath = path.replace(self.dira,'')

for filename in filenames:
zip.write(os.path.join(path,filename),os.path.join(fpath,filename))
zip.close()



if __name__=='__main__':
eurl = r'https://exhentai.org/g/1352016/97259b7457/'
# eurl = input("请输入网址:")
app = EHDownload(eurl)
app.Download()
messagebox.showinfo('Message', '下载完成!')

界面设计部分

window.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
from tkinter import Frame,Entry,Button,LEFT,RIGHT,YES,BOTH,Text
import tkinter.messagebox as messagebox
import EHDownload
import requests
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master,bg='gray')
self.pack(expand=YES,fill=BOTH)
self.window_init()
self.createWidgets()

def window_init(self):
self.master.title('EH下载器')
self.master.bg='gray'
self.master.resizable(False, False)#固定窗体
self.master.iconbitmap('EHDownload.ico')

def createWidgets(self):
self.nameInput = Entry(self,width='48',font=('微软雅黑',14))
self.nameInput.pack(side=LEFT,fill='y',padx=2,pady=2)
self.alertButton = Button(self, text='下载', width='8',command=self.download_button,height='1')
self.alertButton.pack(side=LEFT,padx=2,pady=2)

def download_button(self):
purl = self.nameInput.get() or 'NULL'
if purl is 'NULL':
messagebox.showwarning('Message', '请输入网址!')
return
if purl[8:16] != 'exhentai' and purl[8:16] != 'e-hentai' :
messagebox.showwarning('Message', '请勿输入他站网址!')
return
x = EHDownload.EHDownload(purl)
if x.proxy_bool == True:
try:
requests.get('https://exhentai.org/',headers=x.headers,proxies = x.proxies,cookies=x.cookies)
except Exception as ex:
messagebox.showwarning('Message', '网络错误!')
return
download_bool = messagebox.askokcancel('Message', '点击确定开始下载!')
if download_bool == True:
x.Download()
messagebox.showinfo('Message', '下载完成!')


app = Application()
# 主消息循环:
app.mainloop()

程序生成命令

1
pyinstaller -w -F -i EHDownload.ico window.py