From 663e16ccf29c6696c17866567e346cfba722f702 Mon Sep 17 00:00:00 2001 From: azuki774s Date: Sat, 2 Sep 2023 15:36:21 +0900 Subject: [PATCH 1/2] wip add refresh button press func --- deployment/money-forward.env.sample | 3 ++- src/money-forward/main.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/deployment/money-forward.env.sample b/deployment/money-forward.env.sample index 45e86db..ea3f885 100644 --- a/deployment/money-forward.env.sample +++ b/deployment/money-forward.env.sample @@ -1,3 +1,4 @@ id=xxxxx@gmail.com pass=xxxxxx -urls="https://moneyforward.com/bs/xxxxx,https://moneyforward.com/accounts/yyyyy" +urls="https://moneyforward.com/bs/xxxxx,https://moneyforward.com/accounts/yyyyy,https://moneyforward.com/cf" # 取得するページを , 区切りで記載 +refresh_xpaths="xxxxxxxx,xxxxxxxx" # https://moneyforward.com に表示される金融機関等の[更新]ボタンのXPATHを , 区切りで記載 diff --git a/src/money-forward/main.py b/src/money-forward/main.py index 5a3ae29..78a53ad 100644 --- a/src/money-forward/main.py +++ b/src/money-forward/main.py @@ -37,6 +37,7 @@ def main(): lg.info("Get driver") driver = get_driver() + # login try: html = money.login(driver) except Exception as e: @@ -46,6 +47,11 @@ def main(): urls = os.getenv("urls").split(",") + + # Refresh Button + # TODO + + # download HTML for url in urls: try: html = money.get_from_url(driver, url) From 5680939075301c85174774f475fca6e844ec89fe Mon Sep 17 00:00:00 2001 From: azuki774s Date: Sat, 2 Sep 2023 16:06:18 +0900 Subject: [PATCH 2/2] add update button func --- src/money-forward/main.py | 14 ++++++++++++-- src/money-forward/money.py | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/money-forward/main.py b/src/money-forward/main.py index 78a53ad..89f6ea9 100644 --- a/src/money-forward/main.py +++ b/src/money-forward/main.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import os import sys +import time from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By @@ -10,6 +11,8 @@ import logging from pythonjsonlogger import jsonlogger +ROOTPAGE_URL="https://moneyforward.com" + lg = logging.getLogger(__name__) lg.setLevel(logging.DEBUG) h = logging.StreamHandler() @@ -47,9 +50,16 @@ def main(): urls = os.getenv("urls").split(",") - # Refresh Button - # TODO + money.move_page(driver, ROOTPAGE_URL) + refresh_xpaths = os.getenv("refresh_xpaths").split(",") + for xpath in refresh_xpaths: + try: + money.press_from_xpath(driver, xpath) + lg.info("press update button: %s", xpath) + time.sleep(30) # 反映されるように30sec待っておく + except Exception as e: + lg.warn('failed to press update button: %s', e) # download HTML for url in urls: diff --git a/src/money-forward/money.py b/src/money-forward/money.py index 61f8de8..bfcdf40 100644 --- a/src/money-forward/money.py +++ b/src/money-forward/money.py @@ -98,6 +98,25 @@ def get_from_url_cf_lastmonth(driver): return +def move_page(driver, url): + wait = WebDriverWait(driver=driver, timeout=30) + lg.info("move page url: " + url) + driver.get(url) + return + +def press_from_xpath(driver, xpath): + """ + 指定したxpathのリンクを押す + ページはすでに遷移済にしておくこと + """ + xpath_link = driver.find_element( + by=By.XPATH, + value=xpath, + ) + xpath_link.click() + return + + def write_html(html, url): today = dt.date.today() # 出力:datetime.date(2020, 3, 22) yyyymmdd = "{0:%Y%m%d}".format(today) # 20200322