| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #!/usr/bin/env python3
- """退出笔记:点击「业务合作」,再调用 ``preview-note`` 生成预览 HTML。"""
- from __future__ import annotations
- import sys
- import time
- from pathlib import Path
- from playwright.sync_api import Page
- _REPOSITORY_ROOT_DIRECTORY = Path(__file__).resolve().parent.parent.parent
- if str(_REPOSITORY_ROOT_DIRECTORY) not in sys.path:
- sys.path.insert(0, str(_REPOSITORY_ROOT_DIRECTORY))
- from workplace import playwright as workplace_playwright # noqa: E402
- from workplace import pyautogui as workplace_pyautogui # noqa: E402
- from workplace.download_note_shared import ( # noqa: E402
- BUSINESS_COOPERATION_BUTTON_VISIBLE_TEXT_FOR_OCR_ANCHOR,
- PROJECT_CONFIG_BUSINESS_COOPERATION_BTN_SCREEN_XY_PAIR_CONFIG_KEY,
- REPOSITORY_ROOT_DIRECTORY,
- load_python_module_from_file,
- )
- from workplace.note_pipeline_forbidden_standalone_url_image_fetch import (
- XHS_NOTE_PIPELINE_ABSOLUTE_PROHIBITION_STANDALONE_HTTP_GET_FOR_IMAGE_BYTES,
- )
- from workplace.screenshot import capture_full_screen_screenshot_bgr_numpy_and_save_screenshot_png # noqa: E402
- from workplace.singleton import HOME_PAGE_URL, REPOSITORY_CONFIG_INI_SNAPSHOT # noqa: E402
- DEFAULT_NOTE_LIST_ZERO_BASED_INDEX_STRING = "0"
- def _load_open_browser_module():
- return load_python_module_from_file(
- REPOSITORY_ROOT_DIRECTORY / "workplace" / "open-browser.py",
- "workplace_open_browser_exit_note",
- )
- def _load_move_mouse_to_pos_module():
- return load_python_module_from_file(
- REPOSITORY_ROOT_DIRECTORY / "workplace" / "move-mouse-to-pos.py",
- "workplace_move_mouse_to_pos_exit_note",
- )
- def _load_ocr_pos_module():
- return load_python_module_from_file(
- REPOSITORY_ROOT_DIRECTORY / "workplace" / "ocr-pos.py",
- "workplace_ocr_pos_exit_note",
- )
- def _load_config_save_read_module():
- return load_python_module_from_file(
- REPOSITORY_ROOT_DIRECTORY / "workplace" / "config-save-read.py",
- "workplace_config_save_read_exit_note",
- )
- def _load_preview_note_module():
- return load_python_module_from_file(
- REPOSITORY_ROOT_DIRECTORY / "workplace" / "preview-note" / "preview-note.py",
- "workplace_preview_note_exit_note",
- )
- def start(
- command_line_argument_strings: list[str] | None = None,
- *,
- existing_playwright_page: Page | None = None,
- cached_image_body_by_request_url: dict[str, bytes] | None = None,
- ) -> int:
- _ = XHS_NOTE_PIPELINE_ABSOLUTE_PROHIBITION_STANDALONE_HTTP_GET_FOR_IMAGE_BYTES
- _ = cached_image_body_by_request_url
- command_line_arguments = (
- command_line_argument_strings
- if command_line_argument_strings is not None
- else sys.argv[1:]
- )
- note_list_position_zero_based_index_string = (
- command_line_arguments[0]
- if command_line_arguments
- else DEFAULT_NOTE_LIST_ZERO_BASED_INDEX_STRING
- )
- workplace_pyautogui.init_singleton()
- workplace_playwright.init_singleton()
- move_mouse_to_pos_script_module = _load_move_mouse_to_pos_module()
- ocr_pos_script_module = _load_ocr_pos_module()
- config_save_read_script_module = _load_config_save_read_module()
- preview_note_script_module = _load_preview_note_module()
- def _run_using_playwright_page(playwright_page_for_search_results: Page) -> int:
- note_detail_playwright_page = playwright_page_for_search_results
- note_detail_playwright_page.bring_to_front()
- project_config_dictionary = REPOSITORY_CONFIG_INI_SNAPSHOT.project_config
- business_cooperation_button_screen_xy_integer_pair = (
- config_save_read_script_module.input_keyword_xy_pair_from_config(
- project_config_dictionary,
- PROJECT_CONFIG_BUSINESS_COOPERATION_BTN_SCREEN_XY_PAIR_CONFIG_KEY,
- )
- )
- if business_cooperation_button_screen_xy_integer_pair is None:
- full_screen_screenshot_bgr_numpy_for_business_cooperation_button = (
- capture_full_screen_screenshot_bgr_numpy_and_save_screenshot_png()
- )
- business_cooperation_label_center_screen_xy_integer_pair = (
- ocr_pos_script_module.ocr_find_text_center(
- full_screen_screenshot_bgr_numpy_for_business_cooperation_button,
- BUSINESS_COOPERATION_BUTTON_VISIBLE_TEXT_FOR_OCR_ANCHOR,
- )
- )
- if business_cooperation_label_center_screen_xy_integer_pair is None:
- raise RuntimeError(
- "OCR did not find on-screen anchor text: "
- f"{BUSINESS_COOPERATION_BUTTON_VISIBLE_TEXT_FOR_OCR_ANCHOR!r}",
- )
- business_cooperation_button_screen_xy_integer_pair = (
- int(business_cooperation_label_center_screen_xy_integer_pair[0]),
- int(business_cooperation_label_center_screen_xy_integer_pair[1]),
- )
- config_save_read_script_module.merge_input_keyword_config(
- business_cooperation_btn_pos=business_cooperation_button_screen_xy_integer_pair,
- )
- business_cooperation_click_screen_x = int(
- business_cooperation_button_screen_xy_integer_pair[0],
- )
- business_cooperation_click_screen_y = int(
- business_cooperation_button_screen_xy_integer_pair[1],
- )
- move_mouse_to_pos_script_module.start(
- business_cooperation_click_screen_x,
- business_cooperation_click_screen_y,
- )
- time.sleep(0.35)
- workplace_pyautogui.click_here()
- return preview_note_script_module.start(
- [note_list_position_zero_based_index_string],
- )
- if existing_playwright_page is not None:
- return _run_using_playwright_page(existing_playwright_page)
- open_browser_script_module = _load_open_browser_module()
- with workplace_playwright.sync_playwright_singleton() as playwright_sync_driver:
- first_step_exit_code, playwright_page = (
- open_browser_script_module.prepare_page_at_home_url(
- playwright_sync_driver,
- HOME_PAGE_URL,
- cdp=None,
- )
- )
- if first_step_exit_code != 0:
- return first_step_exit_code
- if playwright_page is None:
- return 1
- return _run_using_playwright_page(playwright_page)
- def main() -> int:
- return start()
- __all__ = ["DEFAULT_NOTE_LIST_ZERO_BASED_INDEX_STRING", "start", "main"]
- if __name__ == "__main__":
- raise SystemExit(main())
|