LoginSignup
3
2

More than 3 years have passed since last update.

Raspberrypi + Pythonで、今日のゴミの日を取得して、LINE通知とアレクサにお知らせしてもらう

Posted at

ゴミ出しを忘れないよう、とりあえず:smiley:作ってみた。
cron で1日1回一ヶ月間動かしてみて様子見。
※url、token は伏せています。

#!/usr/bin/python3
# 市ゴミの日カレンダーの HTML を解析して
# 今日が何のゴミの日かを LINE Notify に通知する
# 該当なしの場合何もしない
#
import subprocess as sp
import re
import requests
import datetime
from bs4 import BeautifulSoup
import urllib.request as req
from string import digits

SECRET = XXX
def send_line_notify(notification_message):
    line_notify_token = 'yXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
    line_notify_api = 'https://notify-api.line.me/api/notify'
    headers = {'Authorization': f'Bearer {line_notify_token}'}
    data = {'message': f'message: {notification_message}'}
    requests.post(line_notify_api, headers = headers, data = data)

yyyymm = str(datetime.datetime.today().strftime("%Y-%m"))
url = "http://www.hoge.city.jp/gomical/calendar.html?date=" + yyyymm + "&id=" + str(SECRET) + "#gaba
ge-calendar"
response = req.urlopen(url)
parse_html = BeautifulSoup(response,"html.parser")
print (parse_html.title.string)

table = parse_html.findAll("table",{"class":"calendar"})[0]     # calendar class を探す
rows = table.findAll("tr")

dd = str(datetime.datetime.today().strftime("%d"))
for row in rows:
    for cell in row.findAll(['td','th']): # カレンダーテーブルを読み込む
        if len(cell.get_text()) > 3:    # ゴミの日以外は日付2文字しかない
            cal_dd = cell.get_text()
            m = re.search(r'\d+', cal_dd)  # 1日はもやすごみ  の1 を取得する
            if dd == m.group(): # dd (Today) と、カレンダーの日付が同じ(今日がゴミの日なら)
                tbl = str.maketrans('','', digits) # カレンダーから取得した日付を除去する
                str = cal_dd.translate(tbl)
                msg = "今日は" + str + "の日です"
                rc = sp.run("/var/www/html/alexa.sh -e 'speak:" + msg + "'", shell=True, stdout=True, stderr=sp.PIPE, text=True).returncode
                send_line_notify(msg)
#end of Gabage.py
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2