締め切りに間に合わなかったので、JSON を使用する必要がありました。
JSON を取得する
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://api.instagram.com/v1/users/xxxxxx/media/recent?access_token=" + ConfigurationManager.AppSettings["instagramAccessToken"]);
request.Method = "GET";
String json = String.Empty;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
json = reader.ReadToEnd();
reader.Close();
dataStream.Close();
}
Linq to SQL を使用して、それを読み取り、データベースに挿入します。
dynamic dyn = JsonConvert.DeserializeObject(json);
foreach (var data in dyn.data)
{
RES_PostInstagram reg = new RES_PostInstagram();
reg.filter = data.filter;
reg.idPost = data.id;
reg.image = data.images.standard_resolution.url;
reg.link = data.link;
reg.publicar = true;
reg.thumbnail = data.images.thumbnail.url;
reg.type = data.type;
reg.created_time = this.UnixTimeStampToDateTime((double)data.created_time);
if (data.caption != null)
reg.caption = data.caption.text;
foreach (string tag in data.tags)
reg.tags += tag + ", ";
// se o post nao existir no banco inserir
RES_PostInstagram existe = (from p in this.ctx.RES_PostInstagrams
where p.idPost == reg.idPost
select p).FirstOrDefault();
if (existe == null)
{
this.ctx.RES_PostInstagrams.InsertOnSubmit(reg);
this.ctx.SubmitChanges();
}
}
Unix タイムスタンプを DateTime に変換します。
public DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}