Skip to content

Instantly share code, notes, and snippets.

@homoluctus
Last active August 12, 2019 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save homoluctus/6633da08af271340e71cf4309ab17182 to your computer and use it in GitHub Desktop.
Save homoluctus/6633da08af271340e71cf4309ab17182 to your computer and use it in GitHub Desktop.
当日の天気を知らせてくれるSlack Botを作ってみた(Python + Selenium + Slack + AWS ECS + Github Actions)
ACCESS_TOKEN=xxxxxxxx
CHANNEL=xxxxxxxx
URL=https://tenki.jp/forecast/3/16/4410/13101/3hours.html
TARGET=forecast-point-3h-today
import sys
from .slack import upload
from .screenshot import take_screenshot
try:
filepath = take_screenshot()
upload(filepath)
except Exception as err:
sys.exit(err)
FROM python:3.7-alpine
WORKDIR /tmp
RUN apk add --no-cache \
fontconfig \
chromium \
chromium-chromedriver && \
wget --no-cache -nv https://oscdl.ipa.go.jp/IPAfont/ipag00303.zip && \
mkdir -p /usr/share/fonts/ipa && \
unzip ipag00303.zip -x *.txt -d /usr/share/fonts/ipa && \
fc-cache -f && \
apk del --no-cache --purge fontconfig && \
rm -rf /tmp/* && \
addgroup executor && \
adduser -D -G executor executor && \
mkdir -p /home/executor/app && \
pip3 install --no-cache-dir pipenv
WORKDIR /home/executor
COPY --chown=executor:executor Pipfile* /home/executor/
RUN pipenv install --system --deploy --clear
COPY --chown=executor:executor src /home/executor/app
USER executor
CMD ["/usr/local/bin/python3", "-m", "app"]
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[packages]
requests = "*"
selenium = "*"
[requires]
python_version = "3.7"
import os
import sys
from pathlib import Path
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
try:
URL = os.environ['URL']
TARGET = os.environ['TARGET']
except KeyError:
sys.exit('URLとTARGET環境変数を設定してください')
def take_screenshot():
options = set_option()
browser = Chrome(options=options)
browser.get(URL)
fileapth = take_by_id(browser)
return filepath
def set_option():
"""Configure Chrome options"""
options = Options()
options.add_argument('--headless')
options.add_argument('--incognito')
options.add_argument('--hide-scrollbars')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
return options
def save_image(filepath='tmp.png', data):
with open(filepath, 'wb') as f:
f.write(data)
if Path(filepath).exists():
return filepath
else:
raise Exception(f'Could not save image data to {filepath}')
def take_by_id(browser):
script = f'''
const ele = document.getElementById({repr(TARGET)});
window.scrollTo(0, ele.getBoundingClientRect().top - 50);
'''
browser.execute_script(script)
img = browser.find_element_by_id(TARGET).screenshot_as_png
return save_image(img)
import os
import sys
import requests
URI = 'https://slack.com/api/files.upload'
CHANNEL = os.getenv('CHANNEL', 'general')
try:
ACCESS_TOKEN = os.environ['ACCESS_TOKEN']
except KeyError:
sys.exit('You need to set evironment variable called "ACCESS_KEY"')
def upload(filepath):
"""upload filepath to slack
Args:
filepath: relative or absolute path of png file
Returns:
True
Exception
"""
try:
files = {'file': open(filepath, 'rb')}
params = {
'token': ACCESS_TOKEN,
'channels': CHANNEL,
'filetype': 'binary',
}
res = requests.post(URI, params=params, files=files, timeout=10)
if not res.ok:
raise Exception(res.error)
return True
except Exception as err:
raise err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment