Skip to content

Commit c707e9a

Browse files
committed
refactor: replace legacy urllib code
1 parent f312e0e commit c707e9a

File tree

6 files changed

+569
-524
lines changed

6 files changed

+569
-524
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Windows 11 系统演示如下:
8484

8585
## 从源代码运行
8686

87-
从源代码运行的方法适用于所有支持图形界面的操作系统,只需 Python >= 3.8(推荐 3.12 及以上版本)。
87+
从源代码运行的方法适用于所有支持图形界面的操作系统,只需 Python >= 3.10(推荐 3.12 及以上版本)。
8888

8989
对于 Linux 系统:
9090

main.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import asyncio
23
import sys
34
from ipaddress import ip_network
45

@@ -14,7 +15,7 @@
1415
from dlgSettings import dlgSettings
1516
from threads import ScanThread, SpeedtestThread
1617
from ui_MainWindow import Ui_MainWindow
17-
from utils import open_url, read_url, time_repr
18+
from utils import open_url, read_url, read_urls_parallel, time_repr
1819

1920

2021
app = QApplication(sys.argv)
@@ -207,27 +208,21 @@ def on_btnWait_Import_clicked(self):
207208
elif dlg.ui.radioCustomURL.isChecked():
208209
url = dlg.ui.customURLEdit.text()
209210
try:
210-
new_ips = read_url(url, timeout=5)
211+
new_ips = asyncio.run(read_url(url))
211212
except Exception:
212213
QMessageBox.critical(self, self.tr('错误'), self.tr('%s 获取失败。请检查网络状况,然后再试。') % url)
213214
return
214215
else: # radioOnline
215-
new_ips = set()
216-
timeout = 3.5
217-
for checkBox, urls in zip((dlg.ui.chkBox_off4, dlg.ui.chkBox_ext4, dlg.ui.chkBox_ext6,
218-
dlg.ui.chkBox_full4, dlg.ui.chkBox_full6),
219-
ONLINE_SERVICES):
220-
if checkBox.isChecked():
221-
for url in urls:
222-
try:
223-
current_ips = read_url(url, timeout)
224-
except Exception:
225-
continue
226-
break
227-
else:
228-
QMessageBox.critical(self, self.tr('错误'), self.tr('%s 获取失败。请检查网络状况,然后再试。') % checkBox.text())
229-
return
230-
new_ips |= current_ips
216+
checkBoxes = (dlg.ui.chkBox_off4, dlg.ui.chkBox_ext4, dlg.ui.chkBox_ext6,
217+
dlg.ui.chkBox_full4, dlg.ui.chkBox_full6)
218+
selected = [(cb, urls) for cb, urls in zip(checkBoxes, ONLINE_SERVICES) if cb.isChecked()]
219+
if not selected:
220+
return
221+
cbs, url_groups = zip(*selected)
222+
new_ips, failed = asyncio.run(read_urls_parallel(url_groups))
223+
if failed:
224+
names = self.tr('、').join(cbs[i].text() for i in failed)
225+
QMessageBox.warning(self, self.tr('警告'), self.tr('%s 获取失败。请检查网络状况,然后再试。') % names)
231226
new_ips = sorted(new_ips, key=lambda ip: tuple(map(int, ip.split('.'))))
232227
if dlg.ui.radioReplace.isChecked():
233228
self._replace_ips(new_ips)

0 commit comments

Comments
 (0)