Skip to content

Commit

Permalink
add update status button
Browse files Browse the repository at this point in the history
  • Loading branch information
azuki774 committed Sep 9, 2023
1 parent e5d7bfb commit cdeaa93
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 3 additions & 0 deletions deployment/api.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[email protected]
pass=xxxxxx
refresh_xpaths="xxxxxxxx,xxxxxxxx" # https://moneyforward.com に表示される金融機関等の[更新]ボタンのXPATHを , 区切りで記載
50 changes: 49 additions & 1 deletion src/flaskapp.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from flask import Flask
from flask import Flask, Response
from pythonjsonlogger import jsonlogger
import logging
import os
import sys
import asyncio
import time
import driver
import moneyforward.money as money

Expand All @@ -17,6 +19,7 @@
h.setFormatter(json_fmt)
logger.addHandler(h)
drv = None
drv_locked = False # driver を同時に操作しないためのロック関数

app = Flask("flaskapp")
@app.route("/", methods=["GET"])
Expand All @@ -25,20 +28,52 @@ def index_get():

@app.route("/moneyforward/cf", methods=["GET"])
def moneyforward_get():
global drv_locked
if drv_locked:
return Response(status=503)
drv_locked = True
html = money.get_from_url(drv, "https://moneyforward.com/cf")

drv_locked = False
return str(html.decode("utf-8"))

@app.route("/moneyforward/cf/lastmonth", methods=["GET"])
def moneyforward_lastmonth_get():
global drv_locked
if drv_locked:
return Response(status=503)
drv_locked = True
html = money.get_from_url_cf_lastmonth(drv)

drv_locked = False
return str(html.decode("utf-8"))

@app.route("/moneyforward/status", methods=["GET"])
def moneyforward_status():
global drv_locked
if drv_locked:
return Response(status=503)
drv_locked = True
xpaths = os.getenv("refresh_xpaths").split(",")
ret_f_json = money.get_status(drv, xpaths)

drv_locked = False
return ret_f_json

@app.route("/moneyforward/status", methods=["PUT"])
def moneyforward_status_update():
global drv_locked
if drv_locked:
return Response(status=503)
drv_locked = True

money.move_page(drv, "https://moneyforward.com")
# Async update button
asyncio.new_event_loop().run_in_executor(None, _async_update_button, drv)

# async で driver を使っているので drv_locked を解除しない
return "Received"

def main():
## Get Initial Browser setup & login
global drv
Expand All @@ -53,4 +88,17 @@ def main():
sys.exit(1)
logger.info("money forward login successful")

def _async_update_button(driver):
global drv_locked
refresh_xpaths = os.getenv("refresh_xpaths").split(",")
for xpath in refresh_xpaths:
try:
money.press_from_xpath(driver, xpath)
logger.info("press update button: %s", xpath)
time.sleep(5) # 同時押し負荷対策
except Exception as e:
logger.warn('failed to press update button: %s', e)
finally:
drv_locked = False

main()

0 comments on commit cdeaa93

Please sign in to comment.