続けて、LINEで天気予報の情報を毎朝自分に送信するプログラムを作成。

google homeで聞いたら一発で教えてくれるのだが、朝起きて一番初めにすることはLINEを見ることの場合が多く、意外に便利だなと思った。

LINEnotifyトークンIDは此方から簡単に発行可能。LINEアカウントでログインして、LINEnotifyの利用登録後トークン発行ボタンを押せばOK

タスクスケジューラを使用して、こちらのコードをexe化して、毎朝起動するように設定すればメッセージを受け取れるようになる。

 

(コード)

import requests

import urllib.request as urlreq

from bs4 import BeautifulSoup

 

r = requests.get("https://tenki.jp/forecast/3/15/4510/12227/10days.html")

weather_list = []

soup = BeautifulSoup(r.content, "html.parser")

 

weatherinf0 = soup.table.tr.nextSibling.nextSibling

#print(weatherinf0.txt)

weatherinf1 = weatherinf0.nextSibling.nextSibling

weatherinf2 = weatherinf1.nextSibling.nextSibling

weatherinf3 = weatherinf2.nextSibling.nextSibling

 

weather_list.append(weatherinf0.text)

weather_list.append(weatherinf1.text)

weather_list.append(weatherinf2.text)

weather_list.append(weatherinf3.text)

#print("全容の確認", weather_list)

#print("確認", weather_list[0])

​​

temp_list = []

for i in range(len(weather_list)):

    temp = weather_list[i].split('\n\n\n')

    mojiretu = ','.join(temp)

    mojiretu = mojiretu.replace(',', '')

    print("確認", mojiretu)

    temp_list.append(mojiretu)

    #print("確認", mojiretu)

print("リストのかくにん", temp_list)

 

​​

line_notify_token = '(LINEnotifyトークン)'

line_notify_api = 'https://notify-api.line.me/api/notify'

#payload = {'message': weather_list}

payload = {'message': temp_list}

headers = {'Authorization': 'Bearer ' + line_notify_token}  # 発行したトークン

line_notify = requests.post(line_notify_api, data=payload, headers=headers)

 

以上

 

①プログラミングの基礎を学んで、将来的にトキ消費の創出に繋がるようなサービスを創りたい

②時間効率化のため、自動化スキルを身に付けたい

③自分でものづくりするのって結構楽しい

という理由でPythonの勉強を開始。備忘も兼ねてブログを書いて保存しておくことにしました。

先ずは、メールで受信した議事録を自動で該当する案件のフォルダに保存するコードを作成したので、コードを共有。

win32com.clientをインストールすることで、Outlookの操作をPythonで行うことができる。

他にもいろいろ使えそう。

主に以下の記事を参考に作成。

⇒ https://www.fastclassinfo.com/entry/python_outlook_receivedmails

 

#ステップ1|ライブラリ

from openpyxl import load_workbook

import win32com.client

import datetime

import os

 

#ステップ2|所定フォルダ内の「Book1.xlsm」を指定して読み込む

filepath = r'(Book1.xlsmの保存場所)'

wb = load_workbook(filename=filepath)

ws1 = wb['データ']

 

#ステップ3|集計範囲の取得

startdate=ws1['B2'].value

enddate=ws1['B3'].value

 

#ステップ4|Outlookの情報を取得

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(6)

messages = inbox.Items

 

a=0

Mailcontent =[]

Mailcontent2 =[]

Mailcontent3 =[]

 

Subject_list=[]

for message in messages:

    RT=message.ReceivedTime

    hiduketime = datetime.datetime(RT.year ,RT.month, RT.day, RT.hour, RT.minute, RT.second)

    if startdate <= hiduketime <= enddate:  

        if "面談メモ" in message.Subject:

            if "RE" not in message.Subject:

                print(message.Subject)

                Mailcontent.append(message.body)

                print("確認1",Mailcontent)

                if "【案件名】" in message.body:

                    revMail = Mailcontent[a].replace("【案件名】","【案件名】⇒").split("⇒")

                    Mailcontent2.append(revMail)

                    print("確認2",Mailcontent2)

                    if "\r\n" in Mailcontent2[a][1]:

                        revMail2 = Mailcontent2[a][1].split("\r\n")

                        Mailcontent3.append(revMail2)

                        print("確認3",Mailcontent3[a][0])

                        if os.path.exists(r'C:\Users\gerra\Google ドライブ\仕事関連\これまでの資料\【案件名】'+ Mailcontent3[a][0]) is False:

                            os.mkdir(r'C:\Users\gerra\Google ドライブ\仕事関連\これまでの資料\【案件名】'+ Mailcontent3[a][0])

                        folderpath = r'C:\Users\gerra\Google ドライブ\仕事関連\これまでの資料\【案件名】'+ Mailcontent3[a][0]

                        #タイトルをメールタイトルにしたい。

                        message.saveAs(folderpath +'\\'+ message.Subject + '.msg')

                        print("完了")                        

                    a += 1

 

使ってみるといろいろな課題が見えてきそうだが、仕事場で一度試してみようと思う。