|
1 | 1 | # -*- coding: utf-8 -*- |
| 2 | +import asyncio |
2 | 3 | import sys |
3 | 4 | from ipaddress import ip_network |
4 | 5 |
|
|
14 | 15 | from dlgSettings import dlgSettings |
15 | 16 | from threads import ScanThread, SpeedtestThread |
16 | 17 | 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 |
18 | 19 |
|
19 | 20 |
|
20 | 21 | app = QApplication(sys.argv) |
@@ -207,27 +208,21 @@ def on_btnWait_Import_clicked(self): |
207 | 208 | elif dlg.ui.radioCustomURL.isChecked(): |
208 | 209 | url = dlg.ui.customURLEdit.text() |
209 | 210 | try: |
210 | | - new_ips = read_url(url, timeout=5) |
| 211 | + new_ips = asyncio.run(read_url(url)) |
211 | 212 | except Exception: |
212 | 213 | QMessageBox.critical(self, self.tr('错误'), self.tr('%s 获取失败。请检查网络状况,然后再试。') % url) |
213 | 214 | return |
214 | 215 | 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) |
231 | 226 | new_ips = sorted(new_ips, key=lambda ip: tuple(map(int, ip.split('.')))) |
232 | 227 | if dlg.ui.radioReplace.isChecked(): |
233 | 228 | self._replace_ips(new_ips) |
|
0 commit comments