Skip to content

Commit

Permalink
Merge pull request #22 from azuki774/alert-mail
Browse files Browse the repository at this point in the history
Alert mail
  • Loading branch information
azuki774 authored Feb 19, 2024
2 parents ba9a70c + 5ad9634 commit ca26676
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 201 deletions.
40 changes: 0 additions & 40 deletions .github/workflows/image-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,43 +205,3 @@ jobs:
file: ./build/Dockerfile-money-forward
push: true
tags: ${{ steps.meta.outputs.tags }}

build_and_push_api:
runs-on: ubuntu-latest
env:
IMAGE_NAME: bill-fetcher-api
steps:
- name: checkout
uses: actions/checkout@v3

- 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-api
# 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-api
push: true
tags: ${{ steps.meta.outputs.tags }}
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
CONTAINER_NAME_API=bill-fetcher-api
CONTAINER_NAME_AUELECT=bill-fetcher-auelect
CONTAINER_NAME_REMIX=bill-fetcher-remix
CONTAINER_NAME_SBI=bill-fetcher-sbi
Expand All @@ -8,7 +7,6 @@ CONTAINER_NAME_MONEY_FORWARD=bill-fetcher-money-forward

.PHONY: build start stop clean
build:
docker build -t $(CONTAINER_NAME_API) -f build/Dockerfile-api .
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 All @@ -26,7 +24,6 @@ debug:
docker compose -f deployment/compose.yml up

clean:
docker image rm $(CONTAINER_NAME_API)
docker image rm $(CONTAINER_NAME_AUELECT)
docker image rm $(CONTAINER_NAME_REMIX)
docker image rm $(CONTAINER_NAME_SBI)
Expand Down
10 changes: 0 additions & 10 deletions build/Dockerfile-api

This file was deleted.

4 changes: 0 additions & 4 deletions requirements/api_requirements.txt

This file was deleted.

104 changes: 0 additions & 104 deletions src/flaskapp.py

This file was deleted.

39 changes: 0 additions & 39 deletions src/gunicorn.py

This file was deleted.

50 changes: 50 additions & 0 deletions src/moneyforward/alert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import smtplib, ssl
from email.mime.text import MIMEText

import logging
import json
import os
from pythonjsonlogger import jsonlogger

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)


notify_address = os.getnev("notify_address")
gmail_account = os.getnev("mail_user")
gmail_password = os.getnev("mail_pass")


def make_mime_text(mail_to, subject, body):
msg = MIMEText(body, "html")
msg["Subject"] = subject
msg["To"] = mail_to
msg["From"] = gmail_account
return msg


def send_gmail(msg):
server = smtplib.SMTP_SSL(
"smtp.gmail.com", 465, context=ssl.create_default_context()
)
server.set_debuglevel(0)
server.login(gmail_account, gmail_password)
server.send_message(msg)


def send_alert_main(body):
lg.info("send alert mail")
joblabel = os.getenv("job_label")
msg = make_mime_text(
mail_to=notify_address,
subject="job: {0}: 取得に失敗しました".format(joblabel),
body=body,
)
send_gmail(msg)
8 changes: 7 additions & 1 deletion src/moneyforward/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from selenium.webdriver.chrome.service import Service
import money
import logging
import alert
from pythonjsonlogger import jsonlogger

ROOTPAGE_URL = "https://moneyforward.com"
Expand Down Expand Up @@ -69,12 +70,17 @@ def main():
try:
html = money.get_from_url(driver, url)
money.write_html(html, url)
if url == "https://moneyforward.com/cf": # このページは先月分のデータも取っておく
if (
url == "https://moneyforward.com/cf"
): # このページは先月分のデータも取っておく
html = money.get_from_url_cf_lastmonth(driver)
money.write_html(html, url + "_lastmonth")
except Exception as e:
lg.error("failed to get HTML: %s", e)
driver.quit()

# alert
alert.send_alert_main(str(e))
sys.exit(1)

driver.quit()
Expand Down

0 comments on commit ca26676

Please sign in to comment.