Skip to content

Instantly share code, notes, and snippets.

@hironow
Last active August 29, 2015 14:01
Show Gist options
  • Save hironow/640e013792f336c92385 to your computer and use it in GitHub Desktop.
Save hironow/640e013792f336c92385 to your computer and use it in GitHub Desktop.
Get japanese holidays list by google calendar.
# -*- coding: utf-8 -*-
from datetime import date
import requests
def getHolidays(year, country='ja'):
"""Get holidays list by year.
:param int year: year
:param str country: country code, future work
:return holidays: holidays list by year
:rtype: dict, {datetime.date: 'holiday_name', ...}
"""
holidays = {}
host = 'http://www.google.com/calendar/feeds/'
if country == 'ja':
# mozilla
cal = 'outid3el0qkcrsuf89fltf7a4qbacgt9@import.calendar.google.com'
# google ja main 'japanese__ja@holiday.calendar.google.com'
# google ja sub 'japanese@holiday.calendar.google.com'
else:
raise ValueError('This country code does not work.')
proj = '/public/full-noattendees'
url = host + cal + proj
values = {'start-min': date(int(year), 1, 1).strftime('%Y-%m-%d'),
'start-max': date(int(year), 12, 31).strftime('%Y-%m-%d'),
'alt': 'json'}
r = requests.get(url=url, params=values)
# get json
elements = r.json()['feed']['entry']
for element in elements:
for k, v in element.iteritems():
if k == 'gd$when':
# set holiday date
year, month, day = v[0]['startTime'].split('-')
holiday = date(int(year), int(month), int(day))
if k == 'title':
# set holiday name
holiday_names = v['$t'].split()
if holiday_names[0] == u'子供の日':
# for formal
holiday_names[0] = u'こどもの日'
holiday_name = holiday_names[0].strip()
holidays[holiday] = holiday_name
return holidays
if __name__ == '__main__':
year = 2014
holidays = getHolidays(year, country='ja')
for i, (day, day_name) in enumerate(sorted(holidays.items())):
print i+1, day, day_name.encode('utf-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment