-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from azuki774/add-api
Add API Server
- Loading branch information
Showing
16 changed files
with
283 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM ghcr.io/azuki774/selenium-chrome:0.2.0 | ||
ENV PYTHONDONTWRITEBYTECODE 1 | ||
ENV PYTHONUNBUFFERED 1 | ||
|
||
COPY requirements/ /tmp/ | ||
RUN pip install --upgrade pip && pip install -r /tmp/api_requirements.txt | ||
RUN pip install -r /tmp/moneyforward_requirements.txt | ||
COPY src/ /src | ||
WORKDIR /src/ | ||
ENTRYPOINT ["gunicorn", "flaskapp:app", "--config", "gunicorn.py"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
FROM python:3.11-bullseye | ||
RUN python -m pip install --upgrade pip | ||
COPY requirements/ /tmp/ | ||
RUN python -m pip install --upgrade pip && pip install -r /tmp/auelect_requirements.txt | ||
COPY src/auelect/ /src/ | ||
RUN pip install -r src/requirement.txt | ||
ENTRYPOINT ["python3", "-u", "/src/main.py"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
FROM ghcr.io/azuki774/selenium-chrome:latest | ||
COPY src/money-forward/ /src/ | ||
RUN pip install -r /src/requirements.txt | ||
FROM ghcr.io/azuki774/selenium-chrome:0.2.0 | ||
COPY requirements/ /tmp/ | ||
RUN pip install --upgrade pip && pip install -r /tmp/moneyforward_requirements.txt | ||
|
||
COPY src/moneyforward/ /src/ | ||
ENTRYPOINT ["python3", "-u", "/src/main.py"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
FROM ghcr.io/azuki774/selenium-chrome:latest | ||
|
||
COPY requirements/ /tmp/ | ||
RUN pip install --upgrade pip && pip install -r /tmp/sbi_requirements.txt | ||
COPY src/sbi/ /src/ | ||
RUN pip install -r /src/requirement.txt | ||
ENTRYPOINT ["python3", "-u", "/src/main.py"] |
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
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を , 区切りで記載 |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Flask==2.3.3 | ||
gunicorn==21.2.0 | ||
requests==2.31.0 | ||
python-json-logger==2.0.7 |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
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 | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.DEBUG) | ||
h = logging.StreamHandler() | ||
h.setLevel(logging.DEBUG) | ||
json_fmt = jsonlogger.JsonFormatter( | ||
fmt="%(asctime)s %(levelname)s %(filename)s %(lineno)s %(message)s", | ||
json_ensure_ascii=False, | ||
) | ||
h.setFormatter(json_fmt) | ||
logger.addHandler(h) | ||
drv = None | ||
drv_locked = False # driver を同時に操作しないためのロック関数 | ||
|
||
app = Flask("flaskapp") | ||
@app.route("/", methods=["GET"]) | ||
def index_get(): | ||
return "OK" | ||
|
||
@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 | ||
logger.info("api setup start") | ||
logger.info("get driver") | ||
drv = driver.get_driver() | ||
logger.info("money forward login") | ||
try: | ||
money.login(drv) | ||
except Exception as e: | ||
logger.error("failed to login. maybe changing xpath: %s", e) | ||
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() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
import json | ||
|
||
# TCPソケット | ||
socket_path = "0.0.0.0:" + str(os.getenv("PORT", 9876)) | ||
bind = socket_path | ||
|
||
# Debugging | ||
reload = True | ||
|
||
# Logging | ||
accesslog = "-" | ||
access_log_format = json.dumps( | ||
{ | ||
"remote_address": r"%(h)s", | ||
"user_name": r"%(u)s", | ||
"date": r"%(t)s", | ||
"status": r"%(s)s", | ||
"method": r"%(m)s", | ||
"url_path": r"%(U)s", | ||
"query_string": r"%(q)s", | ||
"protocol": r"%(H)s", | ||
"response_length": r"%(B)s", | ||
"referer": r"%(f)s", | ||
"user_agent": r"%(a)s", | ||
"request_time_seconds": r"%(L)s", | ||
} | ||
) | ||
# loglevel = 'info' | ||
loglevel = "debug" | ||
logfile = "./log/app.log" | ||
logconfig = None | ||
|
||
# Proc Name | ||
proc_name = "Infrastructure-Practice-Flask" | ||
|
||
# Worker Processes | ||
workers = 1 | ||
worker_class = "sync" |
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
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