Instagram API

http://distillery.s3.amazonaws.com/static/home/images/instagramTitle.png:image:w400
Instagram APIを利用して画像を取得してみました。


通常はOAuth認証を行うみたいですが、
人気画像の取得は認証なしでもいけるみたい。

Instagram API Endpoints • Instagram Developer Documentation

さっそく、上記の公式サイトの手順に従い、
アプリケーションを登録してclient_id を取得します。


以下が画像のURLを出力するコード

#!/usr/bin/env perl

use strict;
use LWP::UserAgent;
use JSON;
use URI;
use Data::Dumper;

my $url = "https://api.instagram.com/v1/media/popular";
my $uri = URI->new($url);
$uri->query_form(client_id => YOUR_CLIENT_ID);
my $ua = LWP::UserAgent->new();
my $result = JSON->new->decode($ua->get($uri->as_string)->content);
#print Dumper($result);
my @images = @{$result->{data}};
for(my $i=0;$i<$#images+1;$i++){
  my $image_url = $images[$i]{images}{standard_resolution}{url};
  # image_size 612x612
  print $image_url."\n";
}

1;

結果はJSONで返されてきます!
他にフィルターやコメントの情報も入ってます。


もし、以下のようなメッセージがでた場合、

LWP will support https URLs if the LWP::Protocol::https module is installed.

モジュールのLWP::Protocol::httpsをインストールすれば大丈夫です。


ref:

Instagram
Instagram APIを触ってみる « trace
YappoLogs: LWP::UserAgent の HTTPS 対応は LWP::Protocol::https というパッケージに分離されました