API-Instagram

 view release on metacpan or  search on metacpan

lib/API/Instagram.pm  view on Meta::CPAN

package API::Instagram;

# ABSTRACT: Object Oriented Interface for the Instagram REST and Search APIs

our $VERSION = '0.013';

use Moo;
# with 'MooX::Singleton';

use Carp;
use strict;
use warnings;
use Digest::MD5 'md5_hex';

use URI;
use JSON;
use Furl;

use API::Instagram::User;
use API::Instagram::Location;
use API::Instagram::Tag;
use API::Instagram::Media;
use API::Instagram::Media::Comment;
use API::Instagram::Search;

has client_id         => ( is => 'ro', required => 1 );
has client_secret     => ( is => 'ro', required => 1 );
has redirect_uri      => ( is => 'ro', required => 1 );
has scope             => ( is => 'ro', default => sub { 'basic' } );
has response_type     => ( is => 'ro', default => sub { 'code'  } );
has grant_type        => ( is => 'ro', default => sub { 'authorization_code' } );
has code              => ( is => 'rw', isa => sub { confess "Code not provided"        unless $_[0] } );
has access_token      => ( is => 'rw', isa => sub { confess "No access token provided" unless $_[0] } );
has no_cache          => ( is => 'rw', default => sub { 0 } );

has _ua               => ( is => 'ro', default => sub { Furl->new() } );
has _obj_cache        => ( is => 'ro', default => sub { { User => {}, Media => {}, Location => {}, Tag => {}, 'Media::Comment' => {} } } );
has _endpoint_url     => ( is => 'ro', default => sub { 'https://api.instagram.com/v1'                 } );
has _authorize_url    => ( is => 'ro', default => sub { 'https://api.instagram.com/oauth/authorize'    } );
has _access_token_url => ( is => 'ro', default => sub { 'https://api.instagram.com/oauth/access_token' } );

has _debug => ( is => 'rw', lazy => 1 );

my $instance;
sub BUILD { $instance = shift }

sub instance { $instance //= shift->new(@_) }

sub get_auth_url { 
	my $self = shift;

	carp "User already authorized with code: " . $self->code if $self->code;

	my @auth_fields = qw(client_id redirect_uri response_type scope);
	for ( @auth_fields ) {
		carp "ERROR: $_ required for generating authorization URL" and return unless defined $self->$_;
	}

	my $uri = URI->new( $self->_authorize_url );
	$uri->query_form( map { $_ => $self->$_ } @auth_fields );
	$uri->as_string();
}


sub get_access_token {
	my $self = shift;

	my @access_token_fields = qw(client_id redirect_uri grant_type client_secret code);
	for ( @access_token_fields ) {
		carp "ERROR: $_ required for generating access token." and return unless defined $self->$_;
	}

	my $data = { map { $_ => $self->$_ } @access_token_fields };
	my $json = $self->_request( 'post', $self->_access_token_url, $data, { token_not_required => 1 } );

	wantarray ? ( $json->{access_token}, $self->user( $json->{user} ) ) : $json->{access_token};
}


sub media { shift->_get_obj( 'Media', 'id', shift ) }

sub user { shift->_get_obj( 'User', 'id', shift // 'self' ) }

sub location { shift->_get_obj( 'Location', 'id', shift, 1 ) }

sub tag { shift->_get_obj( 'Tag', 'name', shift ) }

sub search {
	my $self = shift;
	my $type = shift;
	API::Instagram::Search->new( type => $type )
}


sub popular_medias {
	my $self = shift;
	my $url  = "/media/popular";
	$self->_medias( $url, { @_%2?():@_ } );
}

sub _comment { shift->_get_obj( 'Media::Comment', 'id', shift ) }

#####################################################
# Returns cached wanted object or creates a new one #
#####################################################
sub _get_obj {
	my ( $self, $type, $key, $code, $optional_code ) = @_;

	my $data = { $key => $code };
	$data = $code if ref $code eq 'HASH';
	$code = $data->{$key};

	# Returns if CODE is not optional and not defined or if it's not a string
	return if (!$optional_code and !defined $code) or ref $code;

	# Code used as cache key
	my $cache_code = md5_hex( $code // $data);

	# Returns cached value or creates a new object
	my $return = $self->_cache($type)->{$cache_code} //= ("API::Instagram::$type")->new( $data );

	# Deletes cache if no-cache is set
	delete $self->_cache($type)->{$cache_code} if $self->no_cache;

	return $return;
}

###################################
# Returns a list of Media Objects #
###################################
sub _medias {
	my ($self, $url, $params, $opts) = @_;
	$params->{count} //= 33;
	$params->{url}     = $url;
	[ map { $self->media($_) } $self->_get_list( { %$params, url => $url }, $opts ) ]
}

####################################################################
# Returns a list of the requested items. Does pagination if needed #
####################################################################
sub _get_list {
	my $self   = shift;
	my $params = shift;
	my $opts   = shift;

	my $url      = delete $params->{url} || return [];
	my $count    = $params->{count} // 999_999_999;
	$count       = 999_999_999 if $count < 0;
	$params->{count} = $count;

	my $request = $self->_request( 'get', $url, $params, $opts );
	my $data    = $request->{data};

	# Keeps requesting if total items is less than requested
	# and still there is pagination
	while ( my $pagination = $request->{pagination} ){

		last if     @$data >= $count;
		last unless $pagination->{next_url};

		$opts->{prepared_url} = 1;
		$request = $self->_request( 'get', $pagination->{next_url}, $params, $opts );
		push @$data, @{ $request->{data} };
	}

	return @$data;
}

##############################################################
# Requests the data from the given URL with QUERY parameters #
##############################################################
sub _request {
	my ( $self, $method, $url, $params, $opts ) = @_;

	# Verifies access requirements
	unless ( defined $self->access_token ) {
		if ( !$opts->{token_not_required} or !defined $self->client_id ) {
			carp "A valid access_token is required";
			return {}
		}
	}

	# If URL is not prepared, prepares it
	unless ( $opts->{prepared_url} ){

		$url =~ s|^/||;
		$params->{access_token} = $self->access_token;

		# Prepares the URL
		my $uri = URI->new( $self->_endpoint_url );
		$uri->path_segments( $uri->path_segments, split '/', $url );
		$uri->query_form($params);
		$url = $uri->as_string;
	}



( run in 0.339 second using v1.01-cache-2.11-cpan-140bd7fdf52 )