Last active
August 29, 2015 14:01
-
-
Save hironow/640e013792f336c92385 to your computer and use it in GitHub Desktop.
Get japanese holidays list by google calendar.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 = '[email protected]' | |
# google ja main '[email protected]' | |
# google ja sub '[email protected]' | |
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