Last active
July 4, 2024 07:11
-
-
Save aont/26453af27107fa076c4b8c77d42816e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
import time | |
import contextlib | |
import os | |
from appium import webdriver | |
from appium.options.common.base import AppiumOptions | |
from appium.webdriver.common.appiumby import AppiumBy | |
from selenium.webdriver.common.action_chains import ActionChains | |
from selenium.webdriver.common.actions import interaction | |
from selenium.webdriver.common.actions.action_builder import ActionBuilder | |
from selenium.webdriver.common.actions.pointer_input import PointerInput | |
from selenium.common.exceptions import NoSuchElementException as SeleniumNoSuchElementException | |
with contextlib.ExitStack() as exit_stack: | |
options = AppiumOptions() | |
options.load_capabilities({ | |
"appium:automationName": "UiAutomator2", | |
"appium:platformName": "Android", | |
"appium:newCommandTimeout": 3600, | |
"appium:connectHardwareKeyboard": True | |
}) | |
sys.stderr.write("debug: connecting to appium\n") | |
driver = webdriver.Remote("http://127.0.0.1:4723", options=options) | |
exit_stack.callback(driver.quit) | |
result = driver.execute_script("mobile: shell", {"command": "settings", "args": ["put", "system", "screen_brightness", "0"]}) | |
sys.stderr.write(f"debug: brightness {result}\n") | |
exit_stack.callback(driver.execute_script, "mobile: shell", {"command": "settings", "args": ["put", "system", "screen_brightness", "255"]}) | |
# address_bar = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.FrameLayout[@resource-id=\"com.android.chrome:id/control_container\"]") | |
# url_bar = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.EditText[@resource-id=\"com.android.chrome:id/url_bar\"]") | |
# url_bar.send_keys("about:blank\n") | |
# driver.execute_script("mobile:performEditorAction", {"action": "done"}) | |
# # url_bar.send_keys(selenium.webdriver.common.keys.Keys.) | |
# driver.start_activity() | |
# driver.terminate_app() | |
# sys.exit() | |
awll = driver.find_element(by=AppiumBy.XPATH, value="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout") | |
width, height = json.loads("["+awll.get_attribute("bounds").replace("][","],[")+"]")[1] | |
address_bar = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.FrameLayout[@resource-id=\"com.android.chrome:id/control_container\"]") | |
address_bar_height = json.loads("["+address_bar.get_attribute("bounds").replace("][","],[")+"]")[1][1] | |
str_card_menu = "のカードメニュー" | |
fn_titleset_txt = "titleset.txt" | |
if os.path.exists(fn_titleset_txt): | |
with open(fn_titleset_txt, "rt", encoding="UTF-8") as fp: | |
fp_read = fp.read() | |
title_set = set(fp_read.splitlines()) | |
else: | |
title_set = set() | |
fp = open(fn_titleset_txt, "at", encoding="UTF-8", buffering=1,) | |
exit_stack.enter_context(fp) | |
while True: | |
no_new_item_count = 0 | |
title_set_cur = set() | |
while True: | |
title_set_prev = title_set_cur | |
title_set_cur = set() | |
new_item_exists = False | |
awb_list = driver.find_elements(by=AppiumBy.XPATH, value="//android.view.ViewGroup/android.widget.Button") | |
vg_list = driver.find_elements(by=AppiumBy.XPATH, value="//android.view.ViewGroup") | |
viewgroup_list = [*awb_list, *vg_list] | |
for viewgroup in viewgroup_list: | |
content_desc = viewgroup.get_attribute("content-desc") | |
if not content_desc.endswith(str_card_menu): | |
continue | |
title_name = content_desc[:-len(str_card_menu)] | |
title_set_cur.add(title_name) | |
if title_name in title_set: | |
continue | |
viewgroup_y = viewgroup.location["y"] | |
if viewgroup_y < address_bar_height: | |
continue | |
if viewgroup_y > height: | |
continue | |
title_set.add(title_name) | |
fp.write(title_name+"\n") | |
new_item_exists = True | |
sys.stderr.write(f"debug: {title_name}\n") | |
sys.stderr.write("debug: click menu\n") | |
viewgroup.click() | |
sys.stderr.write("debug: click add_reading_list\n") | |
try: add_reading_list = driver.find_element(by=AppiumBy.XPATH, value="//android.view.View[@content-desc=\"リーディング リストに追加\"]") | |
except SeleniumNoSuchElementException: add_reading_list = None | |
if add_reading_list is None: | |
sys.stderr.write("debug: check elements\n") | |
elem_list = driver.find_elements(by=AppiumBy.XPATH, value="//android.widget.FrameLayout[@resource-id=\"com.android.chrome:id/bottom_sheet_content\"]/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup") | |
if len(elem_list) < 9: continue | |
assert elem_list[-1].get_attribute("content-desc") == "閉じる" | |
add_reading_list = elem_list[3] | |
add_reading_list.click() | |
# if not new_item_exists: | |
# sys.stderr.write(f"debug: {title_set_cur} {title_set_prev}\n") | |
if title_set_cur == title_set_prev: | |
sys.stderr.write(f"debug: title_set_cur == title_set_prev\n") | |
no_new_item_count += 1 | |
else: | |
no_new_item_count = 0 | |
if no_new_item_count >= 10: | |
break | |
sys.stderr.write("debug: scroll\n") | |
actions = ActionChains(driver) | |
actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) | |
actions.w3c_actions.pointer_action.move_to_location(width//2, (height*5)//8) | |
actions.w3c_actions.pointer_action.pointer_down() | |
actions.w3c_actions.pointer_action.move_to_location(width//2, (height*3)//8) | |
actions.w3c_actions.pointer_action.release() | |
# actions.pause(1.0) | |
# actions.scroll_by_amount(0, height//4).perform() | |
actions.perform() | |
# actions.scroll_by_amount | |
# actions.scroll_by_amount(0, height//4) | |
time.sleep(2) | |
sys.stderr.write("debug: lock\n") | |
driver.execute_script('mobile: lock', {}) | |
sys.stderr.write("debug: sleep 5 sec\n") | |
time.sleep(5) | |
is_locked = driver.execute_script('mobile: isLocked') | |
assert is_locked | |
sys.stderr.write("debug: sleep 10 min\n") | |
time.sleep(10) | |
sys.stderr.write("debug: unlock\n") | |
driver.execute_script('mobile: unlock') | |
sys.stderr.write("debug: sleep 5 sec\n") | |
time.sleep(5) | |
is_locked = driver.execute_script('mobile: isLocked') | |
assert not is_locked | |
# driver.execute_script('mobile: pressKey', {"keycode": 4}) | |
sys.stderr.write("debug: terminate chrome\n") | |
driver.terminate_app("com.android.chrome") | |
sys.stderr.write("debug: sleep 2 sec\n") | |
time.sleep(2) | |
sys.stderr.write("debug: launch chrome\n") | |
driver.activate_app("com.android.chrome") | |
sys.stderr.write("debug: sleep 10 sec\n") | |
time.sleep(10) | |
# driver.quit() | |
# "com.android.chrome" | |
# "com.google.android.apps.chrome.Main" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment