diff --git a/main.py b/main.py index c6fe317..c5b4d61 100644 --- a/main.py +++ b/main.py @@ -10,7 +10,7 @@ import re import trio import trio_websocket as ws -from selenium.common.exceptions import StaleElementReferenceException, TimeoutException +from selenium.common.exceptions import StaleElementReferenceException, TimeoutException, InvalidSessionIdException from selenium.webdriver import Chrome, ChromeOptions from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By @@ -332,8 +332,6 @@ def main(driver: WebDriver): cursor = 0 status = Status.ACTIVE - occurrences = {} - request = None sent = 0 errors = 0 warnings = 0 @@ -343,28 +341,27 @@ def main(driver: WebDriver): current = cursor cursor += 1 - recipient = str(recipients[current]).strip() name = names[current] code = codes[current] axis = index[current] - occurrence = occurrences.setdefault(code, [0]) if code else [0] - try: outbox.put(Command('setProgress', cursor, name, recipient, sent, warnings, errors)) - except ShutDown: break + occurrence = pandas.Series.count(frame[frame[cc] == code][cs]) if code is not None else 0 + recipient = str(recipients[current]).strip() if (remarks := frame.loc[axis, cs]) is not None and str(remarks).strip(): tell(f'已跳过项目 {recipient}') - occurrence[0] += 1 continue - if (maximum := parameters.get('max_occurrence')) and occurrence[0] >= maximum: - tell(f'收件人 {recipient} 所属组织出现次数已超出限制 {occurrence}', level=1) + if (maximum := parameters.get('max_occurrence')) and occurrence >= maximum: + tell(f'已跳过项目 {recipient}') warnings += 1 continue while status.isactive(): try: + outbox.put(Command('setProgress', cursor, name, recipient, sent, warnings, errors, attempt)) tell('正在发送:%s (%.2f %%)' % (recipient, current / limit * 100)) + clean = True attempt += 1 request = get_request() @@ -457,7 +454,6 @@ def main(driver: WebDriver): click("div.io-ox-alert.io-ox-alert-error button[data-action='close']") except TimeoutException: frame.loc[axis, cs] = '✔️' - occurrence[0] += 1 sent += 1 break @@ -475,25 +471,37 @@ def main(driver: WebDriver): tell('已超出最大重试上限', level=1) errors += 1 break + except (KeyboardInterrupt, InvalidSessionIdException, ShutDown): + request = None + exception = None except Faillable as o: request = o.request + exception = None except Exception as e: - tell(f'发生错误 ({attempt})', e, level=0) - if connection.closed or isinstance(e, KeyboardInterrupt): - status = Status.TERMINATED - break - outbox.put(Command('setStatus', 'FAILED')) - request = inbox.get() + request = None + exception = e + if exception is not None: + try: + tell(f'发生错误 ({attempt})', exception, level=0) + outbox.put(Command('setStatus', 'FAILED')) + request = inbox.get() + except ShutDown: + pass + + if request == 'CANCEL': + outbox.put(Command('setStatus', 'CANCEL')) + status = Status.INACTIVE + break if request == 'BYPASS': outbox.put(Command('setStatus', 'BYPASS')) break if request == 'RESUME': outbox.put(Command('setStatus', 'RESUME')) continue - if request == 'CANCEL': - outbox.put(Command('setStatus', 'CANCEL')) - status = Status.INACTIVE + + tell('程序中断', level=1) + status = Status.TERMINATED progress = cursor / limit * 100 tell('当前进度:%.2f %%' % progress) @@ -512,7 +520,7 @@ def main(driver: WebDriver): break except Exception as e: tell(f'写入文件时发生错误 ({attempt})', e, level=1) - time.sleep(1) + time.sleep(parameters.get('interval')) continue if status.isalive(): outbox.put(Command('setStatus', 'FINISH'))