diff --git a/main.py b/main.py index 63298c6..5310633 100644 --- a/main.py +++ b/main.py @@ -257,17 +257,12 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): selection = 0 sp.set('actions', lambda: flow.capabilities()) - sp.set('cancel', lambda: flow.do(Cancel, force=True)) - sp.set('skip', lambda: flow.do(Skip, force=True)) + sp.set('cancel', lambda: flow.queue(Cancel)) + sp.set('skip', lambda: flow.queue(Skip)) sp.set('progress', lambda: progress) - while True: + while not flow.react(Wait): try: - flow.allow(Wait) - flow.allow(Sleep) - flow.do(Wait) - flow.react() - for i in range(len(driver.window_handles), 1, -1): driver.switch_to.window(driver.window_handles[i-1]) driver.close() @@ -319,9 +314,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): logger.info('Downloading invoices (%d)', page) data.extend(result) - flow.do(Wait) - flow.do(Sleep) - flow.react() + flow.react(Wait, Sleep) except Skip: pass except Cancel: @@ -385,8 +378,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): try: for i, item in enumerate(data, 1): - flow.do(Wait) - flow.react() + flow.react(Wait) number: str = item['number'] logger.info('[%d/%d] Preprocessing data for %s', i, len(data), number) progress['number'] = number @@ -462,8 +454,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): filename = f'Order-Import-{profile.name}-{datetime.now().strftime('%Y%m%d-%H%M%S-%f')}.xlsx' file = Path(parameters['directory']).joinpath(filename) logger.info('Saving excel file to %s', str(file)) - flow.do(Wait) - flow.react() + flow.react(Wait) workbook.save(file) except Skip: pass @@ -487,8 +478,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): click(".product-import-img-box .mm-selector-rendered") click(".mm-outside.mm-select-dropdown ul li:nth-child(%d) span" % (1 if options.get('draft') else 6)) - flow.do(Wait) - flow.react() + flow.react(Wait) locate(".big-file-upload input", wait=False).send_keys(str(file)) click(".product-import-img-footer button") click(".product-import-img-footer button.mm-button__primary") @@ -498,8 +488,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): innerText = driver.find_element(By.CSS_SELECTOR, ".img-box-result-title").get_attribute('innerText') if innerText == '导入完成': break except: - flow.do(Sleep) - flow.react() + flow.react(Sleep) click(".mm-notification-container .mm-icon-close") click(".product-import-img-footer button.mm-button__primary") @@ -508,12 +497,10 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): if err.get_attribute('disabled') is None: logger.warning('Incomplete import detected; downloaded 1 related document') err.click() - flow.do(Sleep) - flow.react() + flow.react(Sleep) click(".mm-tbody table tbody tr:nth-child(1) td:nth-child(3) a") - flow.do(Sleep) - flow.react() + flow.react(Sleep) logger.info('Done') except Skip: pass @@ -579,8 +566,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): progress['index'] = index flow.allow(Cancel) flow.allow(Skip) - flow.do(Wait) - flow.react() + flow.react(Wait) try: for page in count(1): @@ -598,21 +584,17 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): 'array_flag': 0 }]) driver.get(url.encode()) - flow.do(Wait) - flow.do(Sleep) - flow.react() + flow.react(Wait, Sleep) url = Parse(driver.current_url) if url.get('page') != page: raise Exception(number) - flow.do(Sleep) - flow.react() + flow.react(Sleep) link = locate(".virtual-list-wrap .vue-recycle-scroller .vue-recycle-scroller__item-wrapper > div:nth-child(1) .cell[data-cci='1'] a", wait=False) if link.text != number: continue logger.info('[%d/%d] Processing %s...', index+1, len(data), number) link.click() driver.switch_to.window(driver.window_handles[3]) - flow.do(Wait) - flow.react() + flow.react(Wait) click(".sticky .okki-space-item:nth-child(1) button") break except Skip: @@ -636,9 +618,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): driver.switch_to.new_window('tab') url = Parse(WEBURL % 'crm/business/list') driver.get(url.encode({ 'mode': 'list' })) - flow.do(Wait) - flow.do(Sleep) - flow.react() + flow.react(Wait, Sleep) try: click(".new-wrapper .paas-next-invoice-list-filter-line-wrapper .okki-btn-background-ghost", wait=False) except: pass @@ -650,14 +630,11 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): url.set('curPage', page) url.set('pageSize', 1) driver.get(url.encode({ 'keyword': match, 'search_field': 'serial_keyword' })) - flow.do(Wait) - flow.do(Sleep) - flow.react() + flow.react(Wait, Sleep) url = Parse(driver.current_url) if url.get('curPage') != page: raise Exception(match) - flow.do(Sleep) - flow.react() + flow.react(Sleep) cell = locate(".virtual-list-wrap .vue-recycle-scroller .row-item > .cell:nth-child(3) .ow-serial-read-pretty_ellipsis", wait=False) if cell.text != match: continue link = locate(".virtual-list-wrap .vue-recycle-scroller .row-item > .cell:nth-child(6) a", wait=False) @@ -681,8 +658,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): if opportunity is not None: try: - flow.do(Wait) - flow.react() + flow.react(Wait) dropdown = locate("#rc_select_1") dropdown.clear() dropdown.send_keys(opportunity) @@ -716,8 +692,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): for page in count(1): hits = 0 iteration = 0 - flow.do(Wait) - flow.react() + flow.react(Wait) while hits < pagination and iteration < parameters['attempts']: iteration += 1 @@ -727,8 +702,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): rows = wrapper.find_elements(By.CSS_SELECTOR, ".row-item") for row in reversed(rows) if iteration > 1 else rows: - flow.do(Wait) - flow.react() + flow.react(Wait) driver.execute_script("arguments[0].scrollIntoView({ block: 'center' });", wrapper) driver.execute_script("arguments[0].scrollTo(0, arguments[1]);", wrapper, height) serial = row.text.split('\n', 1)[0].strip() @@ -740,8 +714,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): driver.execute_script("arguments[0].scroll(400, 0);", wrapper) driver.execute_script("arguments[0].scrollIntoView({ block: 'center' });", row) driver.execute_script("arguments[0].scrollIntoView({ block: 'center' });", wrapper) - flow.do(Sleep) - flow.react() + flow.react(Sleep) target = row.find_element(By.CSS_SELECTOR, ".cell[data-cci='6'] input") if (target.get_attribute('value') == '0'): @@ -755,8 +728,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): button = locate(".text-right li.okki-pagination-next button", condition=None) if button.get_attribute('disabled') is not None and len(ids) < len(positions): raise Exception('Product list imcomplete; expected %d, got %d' % (len(positions), len(ids))) - flow.do(Wait) - flow.react() + flow.react(Wait) click(button) except Skip: index += 1 @@ -771,19 +743,16 @@ def main(driver: WebDriver, logger = logging.getLogger('main')): try: click(".ow-box button.okki-btn-round", wait=False) - flow.do(Sleep) - flow.react() + flow.react(Sleep) except Exception as e: logger.warning('Unable to unset additional fees; skipping', exc_info=e) try: - flow.do(Wait) - flow.react() + flow.react(Wait) flow.allow(Cancel, False) flow.allow(Skip, False) click(".sticky.bottom-0 button.okki-btn-primary", condition=None) - flow.do(Sleep) - flow.react() + flow.react(Sleep) except Skip: pass except Cancel: diff --git a/requirements.txt b/requirements.txt index 69d7c7d..3f46d6e 100644 Binary files a/requirements.txt and b/requirements.txt differ