[PYTHON] Import the schedule obtained from "Schedule-kun" into Google Calendar

Overview

With the above post, I got the J-League schedule, so I would like to make use of it. If you can import it into the calendar app you use every day, you can save yourself the trouble of entering it one by one. Therefore Create a file in a format that you can import into Google Calendar.

About iCalendar format

Uninflected word

sample.ics


BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
DTSTART;TZID=Asia/Tokyo:20200223T130000
DTEND;TZID=Asia/Tokyo:20200223T150000
SUMMARY;LANGUAGE=jp-JP:Section 1 Day 3 FC Tokyo
DESCRIPTION;LANGUAGE=jp-JP:kick off:13:00\n
Stadium: Aista\n
club:http://www.fctokyo.co.jp/\n
	JAL:https://www.jal.co.jp/\n
	ANA:https://www.ana.co.jp/\n
END:VEVENT
END:VCALENDAR

code

schedule_all.py


import datetime
import pandas as pd

# 'https://data.j-league.or.jp/'Get from the schedule result of
data = pd.read_csv('./csv/Game_Schedule_2020.csv', sep=',', encoding='utf-8')

キャプチャ1.PNG

schedule_all.py


#Prepare a local file that collects the address of the club HP
club = pd.read_csv('./csv/master_club_2020.csv', sep=',', encoding='utf-8')

I prepared my own data to include the address of the club to play in the detailed data. キャプチャ2.PNG

schedule_all.py


yyyy = 2020
#Specify the team name you want to extract
team = data[(data['home'] == 'Team name') | (data['Away'] == 'Team name')]

Change the "team name" to the team name you want to get.

schedule_all.py


#Create a fixed string
header = (r'BEGIN:VCALENDAR' + '\n'
            r'VERSION:2.0' + '\n')

footer = (r'END:VCALENDAR')

info = ('	JAL:https://www.jal.co.jp/\\n\n'
        '	ANA:https://www.ana.co.jp/\\n')

timezone = ';TZID=Asia/Tokyo'
language = ';LANGUAGE=jp-JP'

Create the strings that make up the template.

schedule_all.py


schedule_all = []

for i in range(len(team)):
    if team['KO time'].iloc[i] == '● Undecided ●':
        break
    #Create the following items for each section from specific team data
    # start_datetime, end_datetime, section, home or away team, kick_off, stadium, club HP
    tmp_date = team['Match day'].iloc[i]
    tmp_date = str(yyyy) + '/' + tmp_date[:5]
    start_time = team['KO time'].iloc[i]
    tmp_date_time = tmp_date + ' ' + start_time + ':00'
    tmp_dtstart = datetime.datetime.strptime(tmp_date_time, '%Y/%m/%d %H:%M:%S')
    tmp_dtend = tmp_dtstart + datetime.timedelta(hours=2)
    # start_datetime
    dtstart = tmp_dtstart.strftime('%Y%m%dT%H%M%S')
    # end_datetime
    dtend = tmp_dtend.strftime('%Y%m%dT%H%M%S')
    # section
    sec = team['section'].iloc[i]
    # team name
    team_name = team['Away'].iloc[i] if team['home'].iloc[i] == 'Team name' else team['home'].iloc[i]
    # kick off
    kickoff = str(team['KO time'].iloc[i])
    # stadium
    stadium = team['Stadium'].iloc[i]
    # club HP
    club_url = club[club['Abbreviation'] == team_name]['Official site URL']

    #Create a character string in iCalender format from data in node units.
    begin = 'BEGIN:VEVENT'
    dstart = 'DTSTART' + timezone + ':' + dtstart
    dend = 'DTEND' + timezone + ':' + dtend
    summary = 'SUMMARY' + language + ':' + sec + ' ' + team_name
    description1 = r'DESCRIPTION' + language + ':' + 'kick off:' + kickoff + '\\n'
    description2 = r'Stadium:' + stadium + '\\n'
    description3 = r'club:' + club_url.values[0] + '\\n\n' + info
    end = 'END:VEVENT' + '\n'
    
    #List section-by-section data
    contents = [begin, dstart, dend, summary, description1, description2, description3, end]
    schedule_all.append(contents)

schedule_all.py


#Creating a calendar file
path = './csv/schedule_all_2020.ics'
with open(path, mode='a', encoding='utf-8') as f:
    f.write(header)
    for s in schedule_all:
        f.write('\n'.join(s))
    f.write(footer)

Finally, loop through the clause-by-section list to create a text file. (* For the sake of explanation, it is divided into a loop process for creating section data and a loop process for creating a text file.) 無題.png

Articles that I used as a reference

Recommended Posts

Import the schedule obtained from "Schedule-kun" into Google Calendar
Access Google Calendar from the iOS app
Get the latest schedule from Google Calendar and notify it on LINE every morning
Import "Sapporo City Household Garbage Collection Day Calendar" into Google Calendar
Scraping the schedule of Hinatazaka46 and reflecting it in Google Calendar
Generate daily reports from Google Calendar
Get holidays with the Google Calendar API
Put the exchange rate obtained from Oanda REST API in Python into MongoDB
I want a Slack bot that calculates and tells me the salary of a part-time job from the schedule of Google Calendar!
For the time being, import them into jupyter
Operate the schedule app using python from iphone
I made a calendar that automatically updates the distribution schedule of Vtuber (Google Calendar edition)