DoboWiki
Top
> .NETプログラミング研究/103 をテンプレートにして作成
.NETプログラミング研究/103 をテンプレートにして作成
開始行:
#title(Googleカレンダーを使って祝日の情報を取得する)
#navi(.NETプログラミング研究)
#contents
*Googleカレンダーを使って祝日の情報を取得する [#he43d2be]
「ある年の何月何日が祝日か」を知るのは、意外と難しいです...
しかし自動的に正確な情報を取得できるのであれば、それに越...
まず考えられるのが、計算で求める方法です。前述した通り、...
祝日を計算で求める方法は、Javaであれば、「[[Java祝日計算 ...
別の方法は、Webサービスを利用するというものです。調べてみ...
また、Googleが提供している「[[Google Calendar APIs>http:/...
**Google Calendar Data API Version2 [#y6458953]
実はこの記事はかなり前に書いたもので、その時はGoogle Cale...
しかしせっかく書いたものですので、ここでバージョン2を使っ...
#code(vbnet){{
'Imports System.IO
'Imports System.Net
'Imports System.Xml
''' <summary>
''' Googleカレンダーから指定した年の祝日を取得する
''' </summary>
''' <param name="year">祝日を取得する年(西暦)。</param>
''' <param name="userId">使用するカレンダーのID。</param>
''' <returns>指定された年の祝日を表す配列。</returns>
Public Shared Function GetNationalHolidaysV2(ByVal year A...
ByVal userId As String) As NationalHoliday()
'URLを作成する
Const googleUrl As String = "http://www.google.com/ca...
Const visibility As String = "public"
Const projection As String = "full-noattendees"
Const maxResults As Integer = 100
'クエリーを作成する
Dim query As String = String.Format( _
"start-min={0}-01-01&start-max={1}-01-01&max-resu...
year, year + 1, maxResults)
'つなげて完成
Dim url As String = _
googleUrl & userId & "/" & visibility & "/" & pro...
'サーバーからデータを受信する
Dim req As HttpWebRequest = DirectCast(WebRequest.Cre...
Dim res As HttpWebResponse = DirectCast(req.GetRespon...
Dim st As Stream = res.GetResponseStream()
Dim sr As New StreamReader(st)
'すべてのデータを受信する
Dim resXml As String = sr.ReadToEnd()
'後始末
sr.Close()
st.Close()
res.Close()
'受信したデータを解析する
'XmlDocumentに読み込む
Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(resXml)
Dim rootElm As XmlElement = xmlDoc.DocumentElement
'<entry>要素を探す
Dim nsmgr As New XmlNamespaceManager(xmlDoc.NameTable)
nsmgr.AddNamespace("ns1", "http://www.w3.org/2005/Ato...
Dim nodeList As XmlNodeList = rootElm.SelectNodes("/n...
'見つかった祝日の名前と日付を取得する
Dim holidays As NationalHoliday() = New NationalHolid...
For i As Integer = 0 To nodeList.Count - 1
Dim entryElm As XmlNode = nodeList(i)
Dim title As String = entryElm("title").InnerXml
Dim startTime As String = entryElm("gd:when").Get...
holidays(i) = New NationalHoliday(title, startTime)
Next
Return holidays
End Function
''' <summary>
''' Googleカレンダーから指定した年の日本の祝日を取得する
''' </summary>
''' <param name="year">祝日を取得する年(西暦)。</param>
''' <returns>指定された年の祝日を表す配列。</returns>
Public Shared Function GetNationalHolidaysV2(ByVal year A...
As NationalHoliday()
'mozilla.org版
Const japaneseHolidaysId As String = _
"outid3el0qkcrsuf89fltf7a4qbacgt9@import.calendar...
'公式版日本語
'const string japaneseHolidaysId =
' "japanese__ja@holiday.calendar.google.com";
'公式版英語
'const string japaneseHolidaysId =
' "japanese@holiday.calendar.google.com";
Return GetNationalHolidaysV2(year, japaneseHolidaysId)
End Function
''' <summary>
''' 祝日を表現したクラス
''' </summary>
Public Class NationalHoliday
Private _name As String
''' <summary>
''' 祝日の名前
''' </summary>
Public ReadOnly Property Name() As String
Get
Return Me._name
End Get
End Property
Private _date As DateTime
''' <summary>
''' 祝日の日付
''' </summary>
Public ReadOnly Property [Date]() As DateTime
Get
Return Me._date
End Get
End Property
''' <summary>
''' NationalHolidayのコンストラクタ
''' </summary>
''' <param name="holidayName">祝日の名前</param>
''' <param name="holidayDate">祝日の日付(RFC3339形式...
Public Sub New(ByVal holidayName As String, ByVal hol...
Me._name = holidayName
Me._date = System.Xml.XmlConvert.ToDateTime(holid...
System.Xml.XmlDateTimeSerializationMode.Local)
'.NET Framework 1.1以前では、次のようにする
'Me._date = XmlConvert.ToDateTime(holidayDate)
End Sub
End Class
}}
#code(csharp){{
//using System;
//using System.IO;
//using System.Net;
//using System.Xml;
/// <summary>
/// Googleカレンダーから指定した年の祝日を取得する
/// </summary>
/// <param name="year">祝日を取得する年(西暦)。</param>
/// <param name="userId">使用するカレンダーのID。</param>
/// <returns>指定された年の祝日を表す配列。</returns>
public static NationalHoliday[] GetNationalHolidaysV2(int...
{
//URLを作成する
const string googleUrl = "http://www.google.com/calen...
const string visibility = "public";
const string projection = "full-noattendees";
const int maxResults = 100;
//クエリーを作成する
string query = string.Format(
"start-min={0}-01-01&start-max={1}-01-01&max-resu...
year, year + 1, maxResults);
//つなげて完成
string url = googleUrl + userId + "/" + visibility + ...
//サーバーからデータを受信する
HttpWebRequest req = (HttpWebRequest)WebRequest.Creat...
HttpWebResponse res = (HttpWebResponse)req.GetRespons...
Stream st = res.GetResponseStream();
StreamReader sr = new StreamReader(st);
//すべてのデータを受信する
string resXml = sr.ReadToEnd();
//後始末
sr.Close();
st.Close();
res.Close();
//受信したデータを解析する
//XmlDocumentに読み込む
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(resXml);
XmlElement rootElm = xmlDoc.DocumentElement;
//<entry>要素を探す
XmlNamespaceManager nsmgr = new XmlNamespaceManager(x...
nsmgr.AddNamespace("ns1", "http://www.w3.org/2005/Ato...
XmlNodeList nodeList = rootElm.SelectNodes("/ns1:feed...
//見つかった祝日の名前と日付を取得する
NationalHoliday[] holidays = new NationalHoliday[node...
for (int i = 0; i < nodeList.Count; i++)
{
XmlNode entryElm = nodeList[i];
string title = entryElm["title"].InnerXml;
string startTime = entryElm["gd:when"].GetAttribu...
holidays[i] = new NationalHoliday(title, startTim...
}
return holidays;
}
/// <summary>
/// Googleカレンダーから指定した年の日本の祝日を取得する
/// </summary>
/// <param name="year">祝日を取得する年(西暦)。</param>
/// <returns>指定された年の祝日を表す配列。</returns>
public static NationalHoliday[] GetNationalHolidaysV2(int...
{
//mozilla.org版
const string japaneseHolidaysId =
"outid3el0qkcrsuf89fltf7a4qbacgt9@import.calendar...
//公式版日本語
//const string japaneseHolidaysId =
// "japanese__ja@holiday.calendar.google.com";
//公式版英語
//const string japaneseHolidaysId =
// "japanese@holiday.calendar.google.com";
return GetNationalHolidaysV2(year, japaneseHolidaysId);
}
/// <summary>
/// 祝日を表現したクラス
/// </summary>
public class NationalHoliday
{
private string _name;
/// <summary>
/// 祝日の名前
/// </summary>
public string Name
{
get { return this._name; }
}
private DateTime _date;
/// <summary>
/// 祝日の日付
/// </summary>
public DateTime Date
{
get { return this._date; }
}
/// <summary>
/// NationalHolidayのコンストラクタ
/// </summary>
/// <param name="holidayName">祝日の名前</param>
/// <param name="holidayDate">祝日の日付(RFC3339形式...
public NationalHoliday(string holidayName, string hol...
{
this._name = holidayName;
this._date = XmlConvert.ToDateTime(holidayDate,
XmlDateTimeSerializationMode.Local);
//.NET Framework 1.1以前では、次のようにする
//this._date = XmlConvert.ToDateTime(holidayDate);
}
}
}}
**Google Calendar Data API Version3 [#w4f1dd58]
バージョン3は2よりハードルが上がっています。最低でもAPI k...
また、結果がJSONで返されます。XMLでは取得できません。JSON...
難しいことは後回しにして、とりあえず試してみましょう。ウ...
https://www.googleapis.com/calendar/v3/calendars/ja.japan...
というURLを開いてみてください(「{YOUR_API_KEY}」の部分は...
それでは、「https://www.googleapis.com/calendar/v3/calend...
***calendarId [#f028bd3d]
「ja.japanese%23holiday@group.v.calendar.google.com」とい...
先頭の「ja.」は、日本語という意味です。「en.」とすると、...
日本以外に色々な国の祝日のカレンダーが用意されています。...
|国名|calendarId|h
|日本|ja.japanese#holiday@group.v.calendar.google.com|
|アメリカ|ja.usa#holiday@group.v.calendar.google.com|
|カナダ|ja.canadian#holiday@group.v.calendar.google.com|
|イギリス|ja.uk#holiday@group.v.calendar.google.com|
|フランス|ja.french#holiday@group.v.calendar.google.com|
|ドイツ|ja.german#holiday@group.v.calendar.google.com|
|イタリア|ja.italian#holiday@group.v.calendar.google.com|
|スペイン|ja.spain#holiday@group.v.calendar.google.com|
|オランダ|ja.dutch#holiday@group.v.calendar.google.com|
|ロシア|ja.russian#holiday@group.v.calendar.google.com|
|オーストラリア|ja.australian#holiday@group.v.calendar.go...
|中国|ja.china#holiday@group.v.calendar.google.com|
|香港|ja.hong_kong#holiday@group.v.calendar.google.com|
|台湾|ja.taiwan#holiday@group.v.calendar.google.com|
|韓国|ja.south_korea#holiday@group.v.calendar.google.com|
|インド|ja.indian#holiday@group.v.calendar.google.com|
|タイ|ja.thai#holiday@group.v.calendar.google.com|
|インドネシア|ja.indonesian#holiday@group.v.calendar.goog...
|フィリピン|ja.philippines#holiday@group.v.calendar.googl...
|シンガポール|ja.singapore#holiday@group.v.calendar.googl...
|ブラジル|ja.brazilian#holiday@group.v.calendar.google.com|
「ja.japanese#holiday@group.v.calendar.google.com」で取得...
***events [#a94ec703]
「events」というのは、[[calendar.events.listメソッド>http...
***timeMinとtimeMax [#g8e6b201]
「timeMin=2012-01-01T00:00:00Z&timeMax=2013-01-01T00:00:0...
日時はRFC3339形式で指定します。また、日にちだけ指定するこ...
***maxResults [#sc6ceed4]
「max-results」には、返して欲しいエントリーの数を指定しま...
***orderBy [#wc0947e8]
「orderBy」を指定すると、エントリーを並び替えた状態で返し...
***singleEvents [#sa25fae1]
「singleEvents」は繰り返しのイベントを展開するかを指定す...
***サンプルコード [#cd84b15c]
以上を踏まえて、Googleカレンダーから祝日の情報(名前と日...
また、「{YOUR_API_KEY}」は有効なAPI keyに書き換えてくださ...
#code(vbnet){{
Imports System.IO
Imports System.Net
Imports System.Xml
Imports System.Collections.Generic
Class Module1
Shared Sub Main()
'2012年の日本の祝日を取得する
Dim holidays As NationalHoliday() = GetNationalHo...
"{YOUR_API_KEY}")
'結果を表示する
For Each holiday As NationalHoliday In holidays
Console.WriteLine("Name: {0}", holiday.Name)
Console.WriteLine("Date: {0}", holiday.Date)
Next
Console.ReadLine()
End Sub
''' <summary>
''' Googleカレンダーから指定した年の祝日を取得する
''' </summary>
''' <param name="year">祝日を取得する年(西暦)。</pa...
''' <param name="apiKey">Google API key。</param>
''' <param name="calendarId">使用するカレンダーのID。...
''' <returns>指定された年の祝日を表す配列。</returns>
Public Shared Function GetNationalHolidays(ByVal year...
ByVal apiKey As String, ByVal calendarId As S...
'URLを作成する
Const googleUrl As String = "https://www.googleap...
Const methodString As String = "events"
Const maxResults As Integer = 100
'クエリーを作成する
Dim query As String = String.Format("key={0}&" & _
"timeMin={1}-01-01T00:00:00Z&" & _
"timeMax={2}-01-01T00:00:00Z&" & _
"maxResults={3}", _
apiKey, year, year + 1, maxResults)
'つなげて完成
Dim url As String = googleUrl & calendarId & "/" ...
'サーバーからデータを受信する
Dim req As HttpWebRequest = DirectCast(WebRequest...
Dim res As HttpWebResponse = DirectCast(req.GetRe...
Dim st As Stream = res.GetResponseStream()
Dim sr As New StreamReader(st)
'すべてのデータを受信する
Dim jsonString As String = sr.ReadToEnd()
'後始末
sr.Close()
st.Close()
res.Close()
'受信したデータを解析する
Dim serializer As New System.Web.Script.Serializa...
Dim jsonDic As Dictionary(Of String, Object) = _
serializer.Deserialize(Of Dictionary(Of Strin...
If Not jsonDic.ContainsKey("items") Then
'itemsがなかったら失敗したと判断する
Return New NationalHoliday(-1) {}
End If
Dim items As System.Collections.ArrayList = _
DirectCast(jsonDic("items"), System.Collectio...
'見つかった祝日の名前と日付を取得する
Dim holidays As NationalHoliday() = New NationalH...
For i As Integer = 0 To items.Count - 1
Dim item As Dictionary(Of String, Object) = _
DirectCast(items(i), Dictionary(Of String...
Dim title As String = DirectCast(item("summar...
Dim startTime As String = DirectCast(DirectCa...
Dictionary(Of String, Object))("date"...
holidays(i) = New NationalHoliday(title, star...
Next
Return holidays
End Function
''' <summary>
''' Googleカレンダーから指定した年の日本の祝日を取得...
''' </summary>
''' <param name="year">祝日を取得する年(西暦)。</pa...
''' <param name="apiKey">使用するカレンダーのID。</pa...
''' <returns>指定された年の祝日を表す配列。</returns>
Public Shared Function GetNationalHolidays(ByVal year...
ByVal apiKey As String) As NationalHoliday()
'mozilla.org版
Const japaneseHolidaysId As String = _
"outid3el0qkcrsuf89fltf7a4qbacgt9@import.cale...
'公式版日本語
'Const japaneseHolidaysId As String = _
' "ja.japanese%23holiday@group.v.calendar.goog...
Return GetNationalHolidays(year, apiKey, japanese...
End Function
''' <summary>
''' 祝日を表現したクラス
''' </summary>
Public Class NationalHoliday
Private _name As String
''' <summary>
''' 祝日の名前
''' </summary>
Public ReadOnly Property Name() As String
Get
Return Me._name
End Get
End Property
Private _date As DateTime
''' <summary>
''' 祝日の日付
''' </summary>
Public ReadOnly Property [Date]() As DateTime
Get
Return Me._date
End Get
End Property
''' <summary>
''' NationalHolidayのコンストラクタ
''' </summary>
''' <param name="holidayName">祝日の名前</param>
''' <param name="holidayDate">祝日の日付(RFC3339...
Public Sub New(ByVal holidayName As String, ByVal...
Me._name = holidayName
Me._date = XmlConvert.ToDateTime(holidayDate, _
XmlDateTimeSerializationMode.Local)
'.NET Framework 1.1以前では、次のようにする
'Me._date = XmlConvert.ToDateTime(holidayDate)
End Sub
End Class
End Class
}}
#code(csharp){{
using System;
using System.IO;
using System.Net;
using System.Xml;
using System.Collections.Generic;
public class Program
{
static void Main(string[] args)
{
//2012年の日本の祝日を取得する
NationalHoliday[] holidays = GetNationalHolidays(
2012, "{YOUR_API_KEY}");
//結果を表示する
foreach (NationalHoliday holiday in holidays)
{
Console.WriteLine("Name: {0}", holiday.Name);
Console.WriteLine("Date: {0}", holiday.Date);
}
}
/// <summary>
/// Googleカレンダーから指定した年の祝日を取得する
/// </summary>
/// <param name="year">祝日を取得する年(西暦)。</pa...
/// <param name="apiKey">Google API key。</param>
/// <param name="calendarId">使用するカレンダーのID。...
/// <returns>指定された年の祝日を表す配列。</returns>
public static NationalHoliday[] GetNationalHolidays(i...
string apiKey, string calendarId)
{
//URLを作成する
const string googleUrl = "https://www.googleapis....
const string methodString = "events";
const int maxResults = 100;
//クエリーを作成する
string query = string.Format(
"key={0}&" +
"timeMin={1}-01-01T00:00:00Z&" +
"timeMax={2}-01-01T00:00:00Z&" +
"maxResults={3}",
apiKey, year, year + 1, maxResults);
//つなげて完成
string url = googleUrl + calendarId + "/" + metho...
//サーバーからデータを受信する
HttpWebRequest req = (HttpWebRequest)WebRequest.C...
HttpWebResponse res = (HttpWebResponse)req.GetRes...
Stream st = res.GetResponseStream();
StreamReader sr = new StreamReader(st);
//すべてのデータを受信する
string jsonString = sr.ReadToEnd();
//後始末
sr.Close();
st.Close();
res.Close();
//受信したデータを解析する
System.Web.Script.Serialization.JavaScriptSeriali...
new System.Web.Script.Serialization.JavaScrip...
Dictionary<string, object> jsonDic =
serializer.Deserialize<Dictionary<string, obj...
if (!jsonDic.ContainsKey("items"))
{
//itemsがなかったら失敗したと判断する
return new NationalHoliday[0];
}
System.Collections.ArrayList items =
(System.Collections.ArrayList)jsonDic["items"];
//見つかった祝日の名前と日付を取得する
NationalHoliday[] holidays = new NationalHoliday[...
for (int i = 0; i < items.Count; i++)
{
Dictionary<string, object> item = (Dictionary...
string title = (string)item["summary"];
string startTime =
(string)((Dictionary<string, object>)item...
holidays[i] = new NationalHoliday(title, star...
}
return holidays;
}
/// <summary>
/// Googleカレンダーから指定した年の日本の祝日を取得...
/// </summary>
/// <param name="year">祝日を取得する年(西暦)。/par...
/// <param name="apiKey">使用するカレンダーのID。</pa...
/// <returns>指定された年の祝日を表す配列。</returns>
public static NationalHoliday[] GetNationalHolidays(i...
{
//mozilla.org版
const string japaneseHolidaysId =
"outid3el0qkcrsuf89fltf7a4qbacgt9@import.cale...
//公式版日本語
//const string japaneseHolidaysId =
// "ja.japanese%23holiday@group.v.calendar.goo...
return GetNationalHolidays(year, apiKey, japanese...
}
/// <summary>
/// 祝日を表現したクラス
/// </summary>
public class NationalHoliday
{
private string _name;
/// <summary>
/// 祝日の名前
/// </summary>
public string Name
{
get { return this._name; }
}
private DateTime _date;
/// <summary>
/// 祝日の日付
/// </summary>
public DateTime Date
{
get { return this._date; }
}
/// <summary>
/// NationalHolidayのコンストラクタ
/// </summary>
/// <param name="holidayName">祝日の名前</param>
/// <param name="holidayDate">祝日の日付(RFC3339...
public NationalHoliday(string holidayName, string...
{
this._name = holidayName;
this._date = XmlConvert.ToDateTime(holidayDate,
XmlDateTimeSerializationMode.Local);
//.NET Framework 1.1以前では、次のようにする
//this._date = XmlConvert.ToDateTime(holidayD...
}
}
}
}}
出力結果は以下のようになります。
#pre{{
Name: 振替休日 / Furikae ky?jitsu / Substitute Holiday
Date: 2012/01/02 0:00:00
Name: 海の日 / Umi no hi / Marine Day
Date: 2007/07/16 0:00:00
Name: 成人の日 / Seijin no hi / Coming-of-age Day
Date: 2007/01/08 0:00:00
Name: 敬老の日 / Keir? no hi / Respect for the Aged Day
Date: 2007/09/17 0:00:00
Name: 振替休日 / Furikae ky?jitsu / Substitute Holiday
Date: 2012/12/24 0:00:00
Name: 体育の日 / Taiiku no hi / Health-Sports Day
Date: 2007/10/08 0:00:00
Name: 振替休日 / Furikae ky?jitsu / Substitute Holiday
Date: 2012/04/30 0:00:00
Name: 天皇誕生日 / Tenn? tanj?bi / Emperor Akihito's Birt...
Date: 2007/12/23 0:00:00
Name: 元日 / Ganjitsu / New Year's Day
Date: 2007/01/01 0:00:00
Name: 勤労感謝の日 / Kinr? kansha no hi / Labour Thanksgi...
Date: 2007/11/23 0:00:00
Name: 春分の日 / Shunbun no hi / Vernal Equinox Day
Date: 2008/03/20 0:00:00
Name: 憲法記念日 / Kenp? kinenbi / Constitution Memorial ...
Date: 2007/05/03 0:00:00
Name: 秋分の日 / Sh?bun no hi / Autumnal Equinox Day
Date: 2012/09/22 0:00:00
Name: 昭和の日 / Sh?wa no hi / Sh?wa Day *
Date: 2007/04/29 0:00:00
Name: 文化の日 / Bunka no hi / Culture Day
Date: 2007/11/03 0:00:00
Name: 建国記念の日 / Kenkoku kinen no hi / National Found...
Date: 2007/02/11 0:00:00
Name: 子供の日 / Kodomo no hi / Children's Day
Date: 2007/05/05 0:00:00
Name: みどりの日 / Midori no hi / Greenery Day *
Date: 2007/05/04 0:00:00
}}
**最後に [#h011aa1d]
年末の忙しい時に取り急ぎ書きなおしたため、もしかしたら不...
最後になりましたが、皆様良いお年をお迎えください。
**コメント [#ad61196d]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]] [[:Category/ASP.NET]],2011-1...
終了行:
#title(Googleカレンダーを使って祝日の情報を取得する)
#navi(.NETプログラミング研究)
#contents
*Googleカレンダーを使って祝日の情報を取得する [#he43d2be]
「ある年の何月何日が祝日か」を知るのは、意外と難しいです...
しかし自動的に正確な情報を取得できるのであれば、それに越...
まず考えられるのが、計算で求める方法です。前述した通り、...
祝日を計算で求める方法は、Javaであれば、「[[Java祝日計算 ...
別の方法は、Webサービスを利用するというものです。調べてみ...
また、Googleが提供している「[[Google Calendar APIs>http:/...
**Google Calendar Data API Version2 [#y6458953]
実はこの記事はかなり前に書いたもので、その時はGoogle Cale...
しかしせっかく書いたものですので、ここでバージョン2を使っ...
#code(vbnet){{
'Imports System.IO
'Imports System.Net
'Imports System.Xml
''' <summary>
''' Googleカレンダーから指定した年の祝日を取得する
''' </summary>
''' <param name="year">祝日を取得する年(西暦)。</param>
''' <param name="userId">使用するカレンダーのID。</param>
''' <returns>指定された年の祝日を表す配列。</returns>
Public Shared Function GetNationalHolidaysV2(ByVal year A...
ByVal userId As String) As NationalHoliday()
'URLを作成する
Const googleUrl As String = "http://www.google.com/ca...
Const visibility As String = "public"
Const projection As String = "full-noattendees"
Const maxResults As Integer = 100
'クエリーを作成する
Dim query As String = String.Format( _
"start-min={0}-01-01&start-max={1}-01-01&max-resu...
year, year + 1, maxResults)
'つなげて完成
Dim url As String = _
googleUrl & userId & "/" & visibility & "/" & pro...
'サーバーからデータを受信する
Dim req As HttpWebRequest = DirectCast(WebRequest.Cre...
Dim res As HttpWebResponse = DirectCast(req.GetRespon...
Dim st As Stream = res.GetResponseStream()
Dim sr As New StreamReader(st)
'すべてのデータを受信する
Dim resXml As String = sr.ReadToEnd()
'後始末
sr.Close()
st.Close()
res.Close()
'受信したデータを解析する
'XmlDocumentに読み込む
Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(resXml)
Dim rootElm As XmlElement = xmlDoc.DocumentElement
'<entry>要素を探す
Dim nsmgr As New XmlNamespaceManager(xmlDoc.NameTable)
nsmgr.AddNamespace("ns1", "http://www.w3.org/2005/Ato...
Dim nodeList As XmlNodeList = rootElm.SelectNodes("/n...
'見つかった祝日の名前と日付を取得する
Dim holidays As NationalHoliday() = New NationalHolid...
For i As Integer = 0 To nodeList.Count - 1
Dim entryElm As XmlNode = nodeList(i)
Dim title As String = entryElm("title").InnerXml
Dim startTime As String = entryElm("gd:when").Get...
holidays(i) = New NationalHoliday(title, startTime)
Next
Return holidays
End Function
''' <summary>
''' Googleカレンダーから指定した年の日本の祝日を取得する
''' </summary>
''' <param name="year">祝日を取得する年(西暦)。</param>
''' <returns>指定された年の祝日を表す配列。</returns>
Public Shared Function GetNationalHolidaysV2(ByVal year A...
As NationalHoliday()
'mozilla.org版
Const japaneseHolidaysId As String = _
"outid3el0qkcrsuf89fltf7a4qbacgt9@import.calendar...
'公式版日本語
'const string japaneseHolidaysId =
' "japanese__ja@holiday.calendar.google.com";
'公式版英語
'const string japaneseHolidaysId =
' "japanese@holiday.calendar.google.com";
Return GetNationalHolidaysV2(year, japaneseHolidaysId)
End Function
''' <summary>
''' 祝日を表現したクラス
''' </summary>
Public Class NationalHoliday
Private _name As String
''' <summary>
''' 祝日の名前
''' </summary>
Public ReadOnly Property Name() As String
Get
Return Me._name
End Get
End Property
Private _date As DateTime
''' <summary>
''' 祝日の日付
''' </summary>
Public ReadOnly Property [Date]() As DateTime
Get
Return Me._date
End Get
End Property
''' <summary>
''' NationalHolidayのコンストラクタ
''' </summary>
''' <param name="holidayName">祝日の名前</param>
''' <param name="holidayDate">祝日の日付(RFC3339形式...
Public Sub New(ByVal holidayName As String, ByVal hol...
Me._name = holidayName
Me._date = System.Xml.XmlConvert.ToDateTime(holid...
System.Xml.XmlDateTimeSerializationMode.Local)
'.NET Framework 1.1以前では、次のようにする
'Me._date = XmlConvert.ToDateTime(holidayDate)
End Sub
End Class
}}
#code(csharp){{
//using System;
//using System.IO;
//using System.Net;
//using System.Xml;
/// <summary>
/// Googleカレンダーから指定した年の祝日を取得する
/// </summary>
/// <param name="year">祝日を取得する年(西暦)。</param>
/// <param name="userId">使用するカレンダーのID。</param>
/// <returns>指定された年の祝日を表す配列。</returns>
public static NationalHoliday[] GetNationalHolidaysV2(int...
{
//URLを作成する
const string googleUrl = "http://www.google.com/calen...
const string visibility = "public";
const string projection = "full-noattendees";
const int maxResults = 100;
//クエリーを作成する
string query = string.Format(
"start-min={0}-01-01&start-max={1}-01-01&max-resu...
year, year + 1, maxResults);
//つなげて完成
string url = googleUrl + userId + "/" + visibility + ...
//サーバーからデータを受信する
HttpWebRequest req = (HttpWebRequest)WebRequest.Creat...
HttpWebResponse res = (HttpWebResponse)req.GetRespons...
Stream st = res.GetResponseStream();
StreamReader sr = new StreamReader(st);
//すべてのデータを受信する
string resXml = sr.ReadToEnd();
//後始末
sr.Close();
st.Close();
res.Close();
//受信したデータを解析する
//XmlDocumentに読み込む
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(resXml);
XmlElement rootElm = xmlDoc.DocumentElement;
//<entry>要素を探す
XmlNamespaceManager nsmgr = new XmlNamespaceManager(x...
nsmgr.AddNamespace("ns1", "http://www.w3.org/2005/Ato...
XmlNodeList nodeList = rootElm.SelectNodes("/ns1:feed...
//見つかった祝日の名前と日付を取得する
NationalHoliday[] holidays = new NationalHoliday[node...
for (int i = 0; i < nodeList.Count; i++)
{
XmlNode entryElm = nodeList[i];
string title = entryElm["title"].InnerXml;
string startTime = entryElm["gd:when"].GetAttribu...
holidays[i] = new NationalHoliday(title, startTim...
}
return holidays;
}
/// <summary>
/// Googleカレンダーから指定した年の日本の祝日を取得する
/// </summary>
/// <param name="year">祝日を取得する年(西暦)。</param>
/// <returns>指定された年の祝日を表す配列。</returns>
public static NationalHoliday[] GetNationalHolidaysV2(int...
{
//mozilla.org版
const string japaneseHolidaysId =
"outid3el0qkcrsuf89fltf7a4qbacgt9@import.calendar...
//公式版日本語
//const string japaneseHolidaysId =
// "japanese__ja@holiday.calendar.google.com";
//公式版英語
//const string japaneseHolidaysId =
// "japanese@holiday.calendar.google.com";
return GetNationalHolidaysV2(year, japaneseHolidaysId);
}
/// <summary>
/// 祝日を表現したクラス
/// </summary>
public class NationalHoliday
{
private string _name;
/// <summary>
/// 祝日の名前
/// </summary>
public string Name
{
get { return this._name; }
}
private DateTime _date;
/// <summary>
/// 祝日の日付
/// </summary>
public DateTime Date
{
get { return this._date; }
}
/// <summary>
/// NationalHolidayのコンストラクタ
/// </summary>
/// <param name="holidayName">祝日の名前</param>
/// <param name="holidayDate">祝日の日付(RFC3339形式...
public NationalHoliday(string holidayName, string hol...
{
this._name = holidayName;
this._date = XmlConvert.ToDateTime(holidayDate,
XmlDateTimeSerializationMode.Local);
//.NET Framework 1.1以前では、次のようにする
//this._date = XmlConvert.ToDateTime(holidayDate);
}
}
}}
**Google Calendar Data API Version3 [#w4f1dd58]
バージョン3は2よりハードルが上がっています。最低でもAPI k...
また、結果がJSONで返されます。XMLでは取得できません。JSON...
難しいことは後回しにして、とりあえず試してみましょう。ウ...
https://www.googleapis.com/calendar/v3/calendars/ja.japan...
というURLを開いてみてください(「{YOUR_API_KEY}」の部分は...
それでは、「https://www.googleapis.com/calendar/v3/calend...
***calendarId [#f028bd3d]
「ja.japanese%23holiday@group.v.calendar.google.com」とい...
先頭の「ja.」は、日本語という意味です。「en.」とすると、...
日本以外に色々な国の祝日のカレンダーが用意されています。...
|国名|calendarId|h
|日本|ja.japanese#holiday@group.v.calendar.google.com|
|アメリカ|ja.usa#holiday@group.v.calendar.google.com|
|カナダ|ja.canadian#holiday@group.v.calendar.google.com|
|イギリス|ja.uk#holiday@group.v.calendar.google.com|
|フランス|ja.french#holiday@group.v.calendar.google.com|
|ドイツ|ja.german#holiday@group.v.calendar.google.com|
|イタリア|ja.italian#holiday@group.v.calendar.google.com|
|スペイン|ja.spain#holiday@group.v.calendar.google.com|
|オランダ|ja.dutch#holiday@group.v.calendar.google.com|
|ロシア|ja.russian#holiday@group.v.calendar.google.com|
|オーストラリア|ja.australian#holiday@group.v.calendar.go...
|中国|ja.china#holiday@group.v.calendar.google.com|
|香港|ja.hong_kong#holiday@group.v.calendar.google.com|
|台湾|ja.taiwan#holiday@group.v.calendar.google.com|
|韓国|ja.south_korea#holiday@group.v.calendar.google.com|
|インド|ja.indian#holiday@group.v.calendar.google.com|
|タイ|ja.thai#holiday@group.v.calendar.google.com|
|インドネシア|ja.indonesian#holiday@group.v.calendar.goog...
|フィリピン|ja.philippines#holiday@group.v.calendar.googl...
|シンガポール|ja.singapore#holiday@group.v.calendar.googl...
|ブラジル|ja.brazilian#holiday@group.v.calendar.google.com|
「ja.japanese#holiday@group.v.calendar.google.com」で取得...
***events [#a94ec703]
「events」というのは、[[calendar.events.listメソッド>http...
***timeMinとtimeMax [#g8e6b201]
「timeMin=2012-01-01T00:00:00Z&timeMax=2013-01-01T00:00:0...
日時はRFC3339形式で指定します。また、日にちだけ指定するこ...
***maxResults [#sc6ceed4]
「max-results」には、返して欲しいエントリーの数を指定しま...
***orderBy [#wc0947e8]
「orderBy」を指定すると、エントリーを並び替えた状態で返し...
***singleEvents [#sa25fae1]
「singleEvents」は繰り返しのイベントを展開するかを指定す...
***サンプルコード [#cd84b15c]
以上を踏まえて、Googleカレンダーから祝日の情報(名前と日...
また、「{YOUR_API_KEY}」は有効なAPI keyに書き換えてくださ...
#code(vbnet){{
Imports System.IO
Imports System.Net
Imports System.Xml
Imports System.Collections.Generic
Class Module1
Shared Sub Main()
'2012年の日本の祝日を取得する
Dim holidays As NationalHoliday() = GetNationalHo...
"{YOUR_API_KEY}")
'結果を表示する
For Each holiday As NationalHoliday In holidays
Console.WriteLine("Name: {0}", holiday.Name)
Console.WriteLine("Date: {0}", holiday.Date)
Next
Console.ReadLine()
End Sub
''' <summary>
''' Googleカレンダーから指定した年の祝日を取得する
''' </summary>
''' <param name="year">祝日を取得する年(西暦)。</pa...
''' <param name="apiKey">Google API key。</param>
''' <param name="calendarId">使用するカレンダーのID。...
''' <returns>指定された年の祝日を表す配列。</returns>
Public Shared Function GetNationalHolidays(ByVal year...
ByVal apiKey As String, ByVal calendarId As S...
'URLを作成する
Const googleUrl As String = "https://www.googleap...
Const methodString As String = "events"
Const maxResults As Integer = 100
'クエリーを作成する
Dim query As String = String.Format("key={0}&" & _
"timeMin={1}-01-01T00:00:00Z&" & _
"timeMax={2}-01-01T00:00:00Z&" & _
"maxResults={3}", _
apiKey, year, year + 1, maxResults)
'つなげて完成
Dim url As String = googleUrl & calendarId & "/" ...
'サーバーからデータを受信する
Dim req As HttpWebRequest = DirectCast(WebRequest...
Dim res As HttpWebResponse = DirectCast(req.GetRe...
Dim st As Stream = res.GetResponseStream()
Dim sr As New StreamReader(st)
'すべてのデータを受信する
Dim jsonString As String = sr.ReadToEnd()
'後始末
sr.Close()
st.Close()
res.Close()
'受信したデータを解析する
Dim serializer As New System.Web.Script.Serializa...
Dim jsonDic As Dictionary(Of String, Object) = _
serializer.Deserialize(Of Dictionary(Of Strin...
If Not jsonDic.ContainsKey("items") Then
'itemsがなかったら失敗したと判断する
Return New NationalHoliday(-1) {}
End If
Dim items As System.Collections.ArrayList = _
DirectCast(jsonDic("items"), System.Collectio...
'見つかった祝日の名前と日付を取得する
Dim holidays As NationalHoliday() = New NationalH...
For i As Integer = 0 To items.Count - 1
Dim item As Dictionary(Of String, Object) = _
DirectCast(items(i), Dictionary(Of String...
Dim title As String = DirectCast(item("summar...
Dim startTime As String = DirectCast(DirectCa...
Dictionary(Of String, Object))("date"...
holidays(i) = New NationalHoliday(title, star...
Next
Return holidays
End Function
''' <summary>
''' Googleカレンダーから指定した年の日本の祝日を取得...
''' </summary>
''' <param name="year">祝日を取得する年(西暦)。</pa...
''' <param name="apiKey">使用するカレンダーのID。</pa...
''' <returns>指定された年の祝日を表す配列。</returns>
Public Shared Function GetNationalHolidays(ByVal year...
ByVal apiKey As String) As NationalHoliday()
'mozilla.org版
Const japaneseHolidaysId As String = _
"outid3el0qkcrsuf89fltf7a4qbacgt9@import.cale...
'公式版日本語
'Const japaneseHolidaysId As String = _
' "ja.japanese%23holiday@group.v.calendar.goog...
Return GetNationalHolidays(year, apiKey, japanese...
End Function
''' <summary>
''' 祝日を表現したクラス
''' </summary>
Public Class NationalHoliday
Private _name As String
''' <summary>
''' 祝日の名前
''' </summary>
Public ReadOnly Property Name() As String
Get
Return Me._name
End Get
End Property
Private _date As DateTime
''' <summary>
''' 祝日の日付
''' </summary>
Public ReadOnly Property [Date]() As DateTime
Get
Return Me._date
End Get
End Property
''' <summary>
''' NationalHolidayのコンストラクタ
''' </summary>
''' <param name="holidayName">祝日の名前</param>
''' <param name="holidayDate">祝日の日付(RFC3339...
Public Sub New(ByVal holidayName As String, ByVal...
Me._name = holidayName
Me._date = XmlConvert.ToDateTime(holidayDate, _
XmlDateTimeSerializationMode.Local)
'.NET Framework 1.1以前では、次のようにする
'Me._date = XmlConvert.ToDateTime(holidayDate)
End Sub
End Class
End Class
}}
#code(csharp){{
using System;
using System.IO;
using System.Net;
using System.Xml;
using System.Collections.Generic;
public class Program
{
static void Main(string[] args)
{
//2012年の日本の祝日を取得する
NationalHoliday[] holidays = GetNationalHolidays(
2012, "{YOUR_API_KEY}");
//結果を表示する
foreach (NationalHoliday holiday in holidays)
{
Console.WriteLine("Name: {0}", holiday.Name);
Console.WriteLine("Date: {0}", holiday.Date);
}
}
/// <summary>
/// Googleカレンダーから指定した年の祝日を取得する
/// </summary>
/// <param name="year">祝日を取得する年(西暦)。</pa...
/// <param name="apiKey">Google API key。</param>
/// <param name="calendarId">使用するカレンダーのID。...
/// <returns>指定された年の祝日を表す配列。</returns>
public static NationalHoliday[] GetNationalHolidays(i...
string apiKey, string calendarId)
{
//URLを作成する
const string googleUrl = "https://www.googleapis....
const string methodString = "events";
const int maxResults = 100;
//クエリーを作成する
string query = string.Format(
"key={0}&" +
"timeMin={1}-01-01T00:00:00Z&" +
"timeMax={2}-01-01T00:00:00Z&" +
"maxResults={3}",
apiKey, year, year + 1, maxResults);
//つなげて完成
string url = googleUrl + calendarId + "/" + metho...
//サーバーからデータを受信する
HttpWebRequest req = (HttpWebRequest)WebRequest.C...
HttpWebResponse res = (HttpWebResponse)req.GetRes...
Stream st = res.GetResponseStream();
StreamReader sr = new StreamReader(st);
//すべてのデータを受信する
string jsonString = sr.ReadToEnd();
//後始末
sr.Close();
st.Close();
res.Close();
//受信したデータを解析する
System.Web.Script.Serialization.JavaScriptSeriali...
new System.Web.Script.Serialization.JavaScrip...
Dictionary<string, object> jsonDic =
serializer.Deserialize<Dictionary<string, obj...
if (!jsonDic.ContainsKey("items"))
{
//itemsがなかったら失敗したと判断する
return new NationalHoliday[0];
}
System.Collections.ArrayList items =
(System.Collections.ArrayList)jsonDic["items"];
//見つかった祝日の名前と日付を取得する
NationalHoliday[] holidays = new NationalHoliday[...
for (int i = 0; i < items.Count; i++)
{
Dictionary<string, object> item = (Dictionary...
string title = (string)item["summary"];
string startTime =
(string)((Dictionary<string, object>)item...
holidays[i] = new NationalHoliday(title, star...
}
return holidays;
}
/// <summary>
/// Googleカレンダーから指定した年の日本の祝日を取得...
/// </summary>
/// <param name="year">祝日を取得する年(西暦)。/par...
/// <param name="apiKey">使用するカレンダーのID。</pa...
/// <returns>指定された年の祝日を表す配列。</returns>
public static NationalHoliday[] GetNationalHolidays(i...
{
//mozilla.org版
const string japaneseHolidaysId =
"outid3el0qkcrsuf89fltf7a4qbacgt9@import.cale...
//公式版日本語
//const string japaneseHolidaysId =
// "ja.japanese%23holiday@group.v.calendar.goo...
return GetNationalHolidays(year, apiKey, japanese...
}
/// <summary>
/// 祝日を表現したクラス
/// </summary>
public class NationalHoliday
{
private string _name;
/// <summary>
/// 祝日の名前
/// </summary>
public string Name
{
get { return this._name; }
}
private DateTime _date;
/// <summary>
/// 祝日の日付
/// </summary>
public DateTime Date
{
get { return this._date; }
}
/// <summary>
/// NationalHolidayのコンストラクタ
/// </summary>
/// <param name="holidayName">祝日の名前</param>
/// <param name="holidayDate">祝日の日付(RFC3339...
public NationalHoliday(string holidayName, string...
{
this._name = holidayName;
this._date = XmlConvert.ToDateTime(holidayDate,
XmlDateTimeSerializationMode.Local);
//.NET Framework 1.1以前では、次のようにする
//this._date = XmlConvert.ToDateTime(holidayD...
}
}
}
}}
出力結果は以下のようになります。
#pre{{
Name: 振替休日 / Furikae ky?jitsu / Substitute Holiday
Date: 2012/01/02 0:00:00
Name: 海の日 / Umi no hi / Marine Day
Date: 2007/07/16 0:00:00
Name: 成人の日 / Seijin no hi / Coming-of-age Day
Date: 2007/01/08 0:00:00
Name: 敬老の日 / Keir? no hi / Respect for the Aged Day
Date: 2007/09/17 0:00:00
Name: 振替休日 / Furikae ky?jitsu / Substitute Holiday
Date: 2012/12/24 0:00:00
Name: 体育の日 / Taiiku no hi / Health-Sports Day
Date: 2007/10/08 0:00:00
Name: 振替休日 / Furikae ky?jitsu / Substitute Holiday
Date: 2012/04/30 0:00:00
Name: 天皇誕生日 / Tenn? tanj?bi / Emperor Akihito's Birt...
Date: 2007/12/23 0:00:00
Name: 元日 / Ganjitsu / New Year's Day
Date: 2007/01/01 0:00:00
Name: 勤労感謝の日 / Kinr? kansha no hi / Labour Thanksgi...
Date: 2007/11/23 0:00:00
Name: 春分の日 / Shunbun no hi / Vernal Equinox Day
Date: 2008/03/20 0:00:00
Name: 憲法記念日 / Kenp? kinenbi / Constitution Memorial ...
Date: 2007/05/03 0:00:00
Name: 秋分の日 / Sh?bun no hi / Autumnal Equinox Day
Date: 2012/09/22 0:00:00
Name: 昭和の日 / Sh?wa no hi / Sh?wa Day *
Date: 2007/04/29 0:00:00
Name: 文化の日 / Bunka no hi / Culture Day
Date: 2007/11/03 0:00:00
Name: 建国記念の日 / Kenkoku kinen no hi / National Found...
Date: 2007/02/11 0:00:00
Name: 子供の日 / Kodomo no hi / Children's Day
Date: 2007/05/05 0:00:00
Name: みどりの日 / Midori no hi / Greenery Day *
Date: 2007/05/04 0:00:00
}}
**最後に [#h011aa1d]
年末の忙しい時に取り急ぎ書きなおしたため、もしかしたら不...
最後になりましたが、皆様良いお年をお迎えください。
**コメント [#ad61196d]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]] [[:Category/ASP.NET]],2011-1...
ページ名:
▲
▼
[
トップ
] [
新規
|
子ページ作成
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]