2020年11月25日水曜日

やらかした。。。

 やっちゃいました。。。

流山同好会は日によって活動場所が変わるのですが

その都度お知らせメールを作るのが面倒なので

①googleカレンダーに登録

②リマインダーを使ってメールを受信

③メールのルールでカレンダーからメールを受け取ったら、会員が登録されているメーリングリストへ送信

あとは月一でカレンダーに登録するだけ!って方法を使って、活動場所をお知らせしています。

しかし、カレンダーの登録を間違うと大惨事に。。。

えぇ、実際にやらかしました。

幸い、事前に気づいて訂正できましたが、カレンダー登録を手動でやると、またミスが発生するだろうと考えたので、カレンダー登録をpythonにやらせることにしました。

なかなか当たらない抽選の当選メールをテキストにして、そのテキストをpythonに読み込ませ、要素を抽出して、googleカレンダーに登録させるようにしました。

googleカレンダーへの登録の基本はquickstartの公式にあるスクリプトを改変

from __future__ import print_function
import datetime
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar']

def main():
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.

    if os.path.exists('token.pickle'):

        with open('token.pickle', 'rb') as token:

            creds = pickle.load(token)

    # If there are no (valid) credentials available, let the user log in.

    if not creds or not creds.valid:

        if creds and creds.expired and creds.refresh_token:

            creds.refresh(Request())

        else:

            flow = InstalledAppFlow.from_client_secrets_file(

                'credentials.json', SCOPES)

            creds = flow.run_local_server(port=0)

        # Save the credentials for the next run

        with open('token.pickle', 'wb') as token:

            pickle.dump(creds, token)


    service = build('calendar', 'v3', credentials=creds)


    f = open (r'流山の当選メール.txt','r',encoding = 'UTF-8')


    datalist = f.readlines()


    for data in datalist :


        if not data.find('利用館') == -1 :

            aiki_loc = data[data.find(':')+1:]

            aiki_loc = aiki_loc.replace('\n','')

        elif not data.find('利用施設') == -1 :

            aiki_loc_details = data[data.find(':')+1:]

        elif not data.find('利用日') == -1 :

            aiki_day = data[data.find(':')+1:data.find('(')]

            aiki_day = aiki_day.replace('/','-')

            aiki_day = aiki_day.replace('\n','')

        elif not data.find('利用時間') == -1 :

            aiki_start_time = data[data.find(':')+1:data.find('-')]

            aiki_start_time = aiki_start_time + ':00'

            aiki_end_time = data[data.find('-')+1:]

            aiki_end_time = aiki_end_time.replace('\n','')

            aiki_end_time = aiki_end_time + ':00'

            if aiki_start_time.startswith('14'):

                aiki_loc_details = aiki_loc_details + \

                                    '指導者:未定\n' \

                                    '14:15~15:15 少年部\n' \

                                    '15:30~16:30 一般部'

            elif aiki_start_time.startswith('09') :

                aiki_loc_details = aiki_loc_details + \

                                    '指導者:未定\n' \

                                    '09:15~10:15 少年部\n' \

                                    '10:30~11:30 一般部'

        elif not data.find('確認期間') == -1 :

            event = {

                'summary': '流山同好会',

                'location': aiki_loc,

                'description': aiki_loc_details,

                'reminders' : {

                    'useDefault': 'false',

                    'overrides': [

                    {

                        'method':'email',

                        'minutes':'1440'

                    },

                ]

                },

                'start': {

                'dateTime': aiki_day + 'T' + aiki_start_time,

                'timeZone': 'Japan',

                },

                'end': {

                'dateTime': aiki_day + 'T' + aiki_end_time,

                'timeZone': 'Japan',

                },

            }

            event = service.events().insert(calendarId='xxxxxxxxxxxxxx@group.calendar.google.com',

                                    body=event).execute()

            print (event['id'])


    f.close


if __name__ == '__main__':

    main()

あとは、重複当選と、予約変更時を間違わなければ大丈夫なはず。。。



0 件のコメント:

コメントを投稿