[PYTHON] Import "Sapporo City Household Garbage Collection Day Calendar" into Google Calendar

Introduction

Do you know "Sapporo City ICT Utilization Platform DATA-SMART CITY SAPPORO"?

"Sapporo City ICT Utilization Platform DATA-SMART CITY SAPPORO" jointly operated by Sapporo City and Sapporo Industrial Promotion Foundation is open data and big data owned by the public and private sectors as a common platform for creating innovation by utilizing data. We collect, manage, and provide. In addition, the data posted on this site can also be used with the CKAN standard API.

That being said, "Sapporo City Household Garbage Collection Day Calendar" is open to the public here. The content of this time is to try to incorporate this into Google Calendar.

Data confirmation

Let's take a look at the data that is actually published.

Sapporo City Household Garbage Collection Date Calendar (October 1, 2017-October 8, 2019)


date,Day of the week,Chuo Ward 1,Chuo Ward 2,Chuo Ward 3,Chuo Ward 4,Chuo Ward 5,Chuo Ward 6,Toyohira Ward 1,Toyohira Ward 2,Toyohira Ward 3,Toyohira Ward 4,Kiyota Ward 1,Kiyota Ward 2,Kita Ward 1,Kita Ward 2,Kita Ward 3,Kita Ward 4,Kita Ward 5,Kita Ward 6,Higashi Ward 1,East Ward 2,East Ward 3,East Ward 4,East Ward 5,East Ward 6,Shiroishi Ward 1,Shiroishi Ward 2,Shiroishi Ward 3,Shiroishi Ward 4,Atsubetsu Ward 1,Atsubetsu Ward 2,Atsubetsu Ward 3,Atsubetsu Ward 4,Minami Ward 1,Minami Ward 2,Minami Ward 3,Minami Ward 4,Minami Ward 5,Minami Ward 6,Minami Ward 7,Nishi-ku 1,Nishi-ku 2,Nishi-ku 3,Nishi-ku 4,Teine Ward 1,Teine Ward 2,Teine Ward 3
2017-10-01,Day,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2017-10-02,Month,1,1,1,10,9,8,10,8,9,1,1,1,1,1,1,10,8,9,1,1,1,10,8,9,9,2,8,1,1,1,1,1,1,1,1,11,9,9,8,1,1,1,9,8,11,8
2017-10-03,fire,8,10,9,1,1,1,1,1,1,10,9,8,9,10,8,1,1,1,10,8,9,1,1,1,1,1,1,8,9,9,8,2,9,11,8,1,1,1,1,9,11,8,1,1,1,1
…

Sapporo City Household Garbage Collection Date Calendar (October 1, 2019-September 30, 2020)


date,Day of the week,Chuo Ward 1,Chuo Ward 2,Chuo Ward 3,Chuo Ward 4,Chuo Ward 5,Chuo Ward 6,Toyohira Ward 1,Toyohira Ward 2,Toyohira Ward 3,Toyohira Ward 4,Kiyota Ward 1,Kiyota Ward 2,Kita Ward 1,Kita Ward 2,Kita Ward 3,Kita Ward 4,Kita Ward 5,Kita Ward 6,Higashi Ward 1,East Ward 2,East Ward 3,East Ward 4,East Ward 5,East Ward 6,Shiroishi Ward 1,Shiroishi Ward 2,Shiroishi Ward 3,Shiroishi Ward 4,Atsubetsu Ward 1,Atsubetsu Ward 2,Atsubetsu Ward 3,Atsubetsu Ward 4,Minami Ward 1,Minami Ward 2,Minami Ward 3,Minami Ward 4,Minami Ward 5,Minami Ward 6,Minami Ward 7,Nishi-ku 1,Nishi-ku 2,Nishi-ku 3,Nishi-ku 4,Teine Ward 1,Teine Ward 2,Teine Ward 3
2019-10-1,fire,8,10,9,1,1,1,1,1,1,10,9,8,9,10,8,1,1,1,10,8,9,1,1,1,1,1,1,8,9,9,8,11,9,2,8,1,1,1,1,9,2,8,1,1,1,1
2019-10-2,water,10,9,8,9,8,10,9,10,8,8,10,9,10,8,9,9,10,8,9,10,8,9,10,8,11,8,9,11,8,11,11,9,8,9,2,8,8,2,9,8,9,2,2,9,8,2
…

The first row shows the item name of each column, and the second and subsequent rows show the garbage type for each region for each day by ID.

"Chuo-ku 1" and "Chuo-ku 2" after the 3rd column of the 1st row are described in here. It's an area number.

The garbage type IDs on the second and subsequent lines are the numbers summarized in the "Garbage type / number correspondence table".

Garbage type / number correspondence table


symbol,Garbage seeds
1,Burnable garbage
2,Non-burnable garbage
8,Bottles, cans, pets
9,Container plastic
10,Miscellaneous
11,Branches / leaves / grass
Blank,No collection

Implementation policy

First of all, after deciding to use Python this time, I decided to implement it with the following policy based on the confirmed data.

input

--CSV of Sapporo City Household Garbage Collection Day Calendar --For CSV reading, I will use pandas for ease. Obviously over-engineered: sweat_smile: --Region code --Enter the area number as a command line option. --The garbage type / number correspondence table will be held directly in the source.

output

--Standard output in iCalendar format ――This format is a standard for importing into google calendar. --I will use a library called icalendar. This is the library that I always use for my icalendar articles.

Implementation details

As follows. You can also find it on github.

sapporo_gc_calendar.py


"""Sapporo City household waste collection day calendar ical.

Make the Sapporo City household waste collection date calendar ical.
See below for the original file.
https://ckan.pf-sapporo.jp/dataset/garbage_collection_calendar
"""
import sys
from argparse import ArgumentParser, FileType

import dateutil.parser
import pandas
from icalendar import Calendar, Event, vDate


def main():
    """Main function."""
    p = ArgumentParser(description='Sapporo City household garbage collection day calendar CSV is converted to ical')
    p.add_argument('-a', '--area', help='Area name')
    p.add_argument(
        'input',
        type=FileType(encoding='utf-8'), help='Sapporo City Household Garbage Collection Day Calendar CSV')
    args = p.parse_args()

    df = pandas.read_csv(args.input, dtype='object')

    if args.area is None:
        print('Area name that can be entered:', file=sys.stderr)
        for area in df.columns[2:]:
            print(' ' + area, file=sys.stderr)

        return

    if args.area not in df.columns[2:]:
        print('Region name not found.', file=sys.stderr)
        return

    gctypes = {
        '1': 'Burnable garbage',
        '2': 'Non-burnable garbage',
        '8': 'Bottles, cans, pets',
        '9': 'Container plastic',
        '10': 'Miscellaneous',
        '11': 'Branches / leaves / grass'
    }

    ical = Calendar()

    for datestr, gctype in zip(df['date'], df[args.area]):
        if gctype in gctypes:
            dt = dateutil.parser.parse(datestr)
            event = Event()
            event.add('SUMMARY', gctypes[gctype])
            event.add('DTSTART', vDate(dt))
            event.add('DTEND', vDate(dt))
            event.add('TRANSP', 'TRANSPARENT')
            ical.add_component(event)

    print(ical.to_ical().decode('utf-8'))


if __name__ == '__main__':
    main()

I haven't done much, but I'm addicted to one point.

--Date of Sapporo City Household Garbage Collection Date Calendar (October 1, 2017-October 8, 2019) --It is "2017-10-01" and is in ISO 8601 format (YYYY-MM-DD format). --Date of Sapporo City Household Garbage Collection Date Calendar (October 1, 2019-September 30, 2020) ――It is "2019-10-1" and it is slightly out of ISO 8601 ...

Therefore, date.fromisoformat () cannot be used directly for files from October 1, 2019 to September 30, 2020. For this reason, I try to use dateutil.parser.parse () from python-dateutil.

in conclusion

Import the iCalendar format file generated using this tool into google calendar and you're done. If you put it in the google calendar, you can use it for various notifications, so it's convenient.

Recommended Posts

Import "Sapporo City Household Garbage Collection Day Calendar" into Google Calendar
Import the schedule obtained from "Schedule-kun" into Google Calendar