Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add au #14

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 0 additions & 40 deletions .github/workflows/image-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,6 @@ on:
- v*

jobs:
build_and_push_remix:
runs-on: ubuntu-latest
env:
IMAGE_NAME: bill-fetcher-remix
steps:
- name: checkout
uses: actions/checkout@v2

- name: Set meta
id: meta
uses: docker/metadata-action@v3
with:
# list of Docker images to use as base name for tags
images: |
ghcr.io/azuki774/bill-fetcher-remix
# generate Docker tags based on the following events/attributes
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=semver,pattern=latest

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GH_ACCESS_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: ./build/Dockerfile-remix
push: true
tags: ${{ steps.meta.outputs.tags }}

build_and_push_sbi:
runs-on: ubuntu-latest
env:
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CONTAINER_NAME_AU=bill-fetcher-au
CONTAINER_NAME_AUELECT=bill-fetcher-auelect
CONTAINER_NAME_REMIX=bill-fetcher-remix
CONTAINER_NAME_SBI=bill-fetcher-sbi
Expand All @@ -7,6 +8,7 @@ CONTAINER_NAME_MONEY_FORWARD=bill-fetcher-money-forward

.PHONY: build start
build:
docker build -t $(CONTAINER_NAME_AU) -f build/Dockerfile-au .
docker build -t $(CONTAINER_NAME_AUELECT) -f build/Dockerfile-auelect .
docker build -t $(CONTAINER_NAME_REMIX) -f build/Dockerfile-remix .
docker build -t $(CONTAINER_NAME_SBI) -f build/Dockerfile-sbi .
Expand Down
4 changes: 4 additions & 0 deletions build/Dockerfile-au
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM ghcr.io/azuki774/selenium-chrome:0.2.0-rc.3
COPY src/au/ /src/
RUN pip install -r /src/requirements.txt
ENTRYPOINT ["python3", "-u", "/src/main.py"]
20 changes: 15 additions & 5 deletions deployment/compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
version: '3'
services:
money-forward:
image: bill-fetcher-money-forward
container_name: bill-fetcher-money-forward
# money-forward:
# image: bill-fetcher-money-forward
# container_name: bill-fetcher-money-forward
# env_file:
# - money-forward.env
# volumes:
# - ./:/data/

au:
image: bill-fetcher-au
container_name: bill-fetcher-au
env_file:
- money-forward.env
- au-token.env
volumes:
- ./:/data/
- ./.cookies:/.cookies
command:
- "--login-2fa"

# remix:
# image: bill-fetcher-remix
Expand Down
134 changes: 134 additions & 0 deletions src/au/au.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import os
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import logging
from pythonjsonlogger import jsonlogger
import argparse
import pickle

lg = logging.getLogger(__name__)
lg.setLevel(logging.DEBUG)
h = logging.StreamHandler()
h.setLevel(logging.DEBUG)
json_fmt = jsonlogger.JsonFormatter(fmt='%(asctime)s %(levelname)s %(name)s %(message)s', json_ensure_ascii=False)
h.setFormatter(json_fmt)
lg.addHandler(h)

SECRET_2FA_FILE="/.2fa_secret"
COOKIES='/.cookies'

def load_cookies(driver):
cookies = pickle.load(open(COOKIES, 'rb'))
for cookie in cookies:
driver.add_cookie(cookie)
return

def save_cookies(driver):
pickle.dump(driver.get_cookies(), open(COOKIES, 'wb'))
return

def login(driver):
lg.info("au login start")
url = "https://connect.auone.jp/net/vwc/cca_lg_eu_nets/login?targeturl=https%3A%2F%2Fwww.au.com%2Fenergy%2Fdenki%2Flogin"
driver.get(url)
if os.path.isfile(COOKIES):
lg.info("load cookies")
try:
load_cookies(driver)
lg.info("load cookies success")
except Exception as e:
lg.warning('failed to load cookies. skipping. {0}'.format(e))
else:
lg.warning("cookie file not found")

# After loading cookies
url = "https://connect.auone.jp/net/vwc/cca_lg_eu_nets/login?targeturl=https%3A%2F%2Fwww.au.com%2Fenergy%2Fdenki%2Flogin"
driver.get(url)

telno_field = driver.find_element(
by=By.XPATH,
value="/html/body/div[2]/div/div[1]/div[2]/div/div/form/input[24]",
)
telno_field.send_keys(os.getenv("telno"))

next_button = driver.find_element(
by=By.XPATH,
value="/html/body/div[2]/div/div[1]/div[2]/div/div/form/button[1]",
)
next_button.click()
time.sleep(10)
html = driver.page_source.encode("utf-8").decode("utf-8")
print(html)

pass_field = driver.find_element(
by=By.XPATH,
value="/html/body/div[2]/div/div[1]/div[2]/div/div/form/input[25]",
)
pass_field.send_keys(os.getenv("pass"))

login_button = driver.find_element(
by=By.XPATH,
value="/html/body/div[2]/div/div[1]/div[2]/div/div/form/button[4]",
)
login_button.click()

lg.info("user/pass login ok")
time.sleep(10)
html = driver.page_source.encode("utf-8").decode("utf-8")
print(html)

def login_2fa(driver):
login(driver)
_login_2fa_proc(driver)
return

def _login_2fa_proc(driver):
# 前提: https://connect.auone.jp/net/vwc/cca_lg_eu_nets/cca で止まっている状態
# 2段階認証のためのログイン操作をする
# /.2fa_secret に 2段階認証コードを書くことで認証を進める
lg.info("try 2FA login")
lg.info("please set {0} written 2FA code".format(SECRET_2FA_FILE))
lg.info("docker exec -it <pod_name> /bin/bash")
lg.info("echo '<pass>' > {0}".format(SECRET_2FA_FILE))

while True:
# ファイルが存在するようになるまで待つ
if os.path.isfile(SECRET_2FA_FILE):
break
time.sleep(10)

with open(SECRET_2FA_FILE, encoding='utf-8') as f:
lines = f.read()
secret_2fa_code = str(lines)

pass_field = driver.find_element(
by=By.XPATH,
value="/html/body/div[2]/div/form/input[7]",
)
pass_field.send_keys(secret_2fa_code)

login_button = driver.find_element(
by=By.XPATH,
value="/html/body/div[2]/div/form/button",
)
login_button.click()
lg.info("2FA login information send")
time.sleep(5)
html = driver.page_source.encode("utf-8").decode("utf-8")
print(html)

def get_from_url(driver, url):
wait = WebDriverWait(driver=driver, timeout=30)
driver.get(url)
time.sleep(10)
wait.until(EC.presence_of_all_elements_located)
html = driver.page_source.encode("utf-8").decode("utf-8")
print(html)
return html
51 changes: 51 additions & 0 deletions src/au/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import logging
import driver
from pythonjsonlogger import jsonlogger
import argparse
import sys
import au
import os
import time

lg = logging.getLogger(__name__)
lg.setLevel(logging.DEBUG)
h = logging.StreamHandler()
h.setLevel(logging.DEBUG)
json_fmt = jsonlogger.JsonFormatter(fmt='%(asctime)s %(levelname)s %(name)s %(message)s', json_ensure_ascii=False)
h.setFormatter(json_fmt)
lg.addHandler(h)

COOKIES='/.cookies'

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--login-2fa', action='store_true') # default false
args = parser.parse_args()
lg.info("au start")

lg.info("get driver")
driver = driver.get_driver()

if args.login_2fa:
# login mode
lg.info("au page login mode")
au.login_2fa(driver)
lg.info("save cookies")
au.save_cookies(driver)
time.sleep(3600)
sys.exit(0)

au.login(driver)

lg.info("save cookies")
au.save_cookies(driver)

lg.info("fetch url: " + "https://mieru.auone.jp/#/results/daily")
au.get_from_url(driver, "https://mieru.auone.jp/#/results/daily")

lg.info("auelect end")
1 change: 1 addition & 0 deletions src/au/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-json-logger>=2.0.7