Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 419 lines (324 sloc) 13.868 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
package Net::Twitter::Core;

our $VERSION = '4.01004';

# ABSTRACT: A perl interface to the Twitter API

use 5.008001;
use Moose;
use Carp::Clan qw/^(?:Net::Twitter|Moose|Class::MOP)/;
use JSON;
use URI::Escape;
use HTTP::Request::Common;
use Net::Twitter::Error;
use Scalar::Util qw/blessed reftype/;
use List::Util qw/first/;
use HTML::Entities ();
use Encode qw/encode_utf8/;
use DateTime;
use Data::Visitor::Callback;
use Try::Tiny;

use namespace::autoclean;

has useragent_class => ( isa => 'Str', is => 'ro', default => 'LWP::UserAgent' );
has useragent_args => ( isa => 'HashRef', is => 'ro', default => sub { {} } );
has username => ( isa => 'Str', is => 'rw', predicate => 'has_username' );
has password => ( isa => 'Str', is => 'rw', predicate => 'has_password' );
has ssl => ( isa => 'Bool', is => 'ro', default => 0 );
has netrc => ( isa => 'Str', is => 'ro', predicate => 'has_netrc' );
has netrc_machine => ( isa => 'Str', is => 'ro', default => 'api.twitter.com' );
has decode_html_entities => ( isa => 'Bool', is => 'rw', default => 0 );
has useragent => ( isa => 'Str', is => 'ro', default => "Net::Twitter/$Net::Twitter::Core::VERSION (Perl)" );
has source => ( isa => 'Str', is => 'ro', default => 'twitterpm' );
has ua => ( isa => 'Object', is => 'rw', lazy => 1, builder => '_build_ua' );
has clientname => ( isa => 'Str', is => 'ro', default => 'Perl Net::Twitter' );
has clientver => ( isa => 'Str', is => 'ro', default => $Net::Twitter::Core::VERSION );
has clienturl => ( isa => 'Str', is => 'ro', default => 'http://search.cpan.org/dist/Net-Twitter/' );
has _base_url => ( is => 'rw' ); ### keeps role composition from bitching ??
has _json_handler => (
    is => 'rw',
    default => sub { JSON->new->utf8 },
    handles => { _from_json => 'decode' },
);

sub _legacy_synthetic_args { qw/authenticate since/ }

sub _remap_legacy_synthetic_args {
    my ( $self, $args ) = @_;

    $args->{"-$_"} = delete $args->{$_} for grep exists $args->{$_}, $self->_legacy_synthetic_args;
}

sub _natural_args {
    my ( $self, $args ) = @_;

    map { $_ => $args->{$_} } grep !/^-/, keys %$args;
}

around BUILDARGS => sub {
    my $next = shift;
    my $class = shift;

    my %options = @_ == 1 ? %{$_[0]} : @_;

    # Twitter now requires SSL connections. Since Net::Twitter is used for
    # Twitter API compatible services that may not require, or indeed allow
    # SSL, we won't change the default, yet. We'll have a deprecation cycle
    # where we warn users if they don't have an ssl option set and let them
    # know enabling ssl will be the default in the future.
    unless ( exists $options{ssl} ) {
        warn <<'';
The Twitter API now requires SSL. Add ( ssl => 1 ) to the options passed to new
to enable it. For backwards compatibility, SSL is disabled by default in this
version. Passing the ssl option to new will disable this warning. If you are
using a Twitter API compatbile service that does not support SSL, add
( ssl => 0 ) to disable this warning and preserve non-SSL connections in future
upgrades.

        $options{ssl} = 0;
    }

    # aliases
    for ( [ user => 'username' ], [ pass => 'password' ] ) {
        my ( $alias, $base ) = @$_;
        if ( exists $options{$alias} ) {
            if ( !defined $options{$base} ) {
                $options{$base} = delete $options{$alias};
            }
            else {
                carp "Both $base and $alias provided. Ignoring $alias";
            }
        }
    }

    if ( delete $options{identica} ) {
        %options = (
            apiurl => 'http://identi.ca/api',
            searchapiurl => 'http://identi.ca/api',
            apirealm => 'Laconica API',
            oauth_urls => {
                request_token_url => "https://identi.ca/api/oauth/request_token",
                authentication_url => "https://identi.ca/api/oauth/authenticate",
                authorization_url => "https://identi.ca/api/oauth/authorize",
                access_token_url => "https://identi.ca/api/oauth/access_token",
                xauth_url => "https://identi.ca/api/oauth/access_token",
            },
            %options,
        );
    }

    return $next->($class, \%options);
};

sub BUILD {
    my $self = shift;

    if ( $self->has_netrc ) {
        require Net::Netrc;

        # accepts '1' for backwards compatibility
        my $host = $self->netrc eq '1' ? $self->netrc_machine : $self->netrc;
        my $nrc = Net::Netrc->lookup($host)
            || croak "No .netrc entry for $host";

        my ($user, $pass) = $nrc->lpa;
        $self->username($user);
        $self->password($pass);
    }

    $self->credentials($self->username, $self->password) if $self->has_username;
}

sub _build_ua {
    my $self = shift;

    eval "use " . $self->useragent_class;
    croak $@ if $@;

    my $ua = $self->useragent_class->new(%{$self->useragent_args});
    $ua->agent($self->useragent);
    $ua->default_header('X-Twitter-Client' => $self->clientname);
    $ua->default_header('X-Twitter-Client-Version' => $self->clientver);
    $ua->default_header('X-Twitter-Client-URL' => $self->clienturl);
    $ua->env_proxy;

    return $ua;
}

sub credentials {
    my ($self, $username, $password) = @_;

    $self->username($username);
    $self->password($password);

    return $self; # make it chainable
}

sub _encode_args {
    my ($self, $args) = @_;

    # Values need to be utf-8 encoded. Because of a perl bug, exposed when
    # client code does "use utf8", keys must also be encoded.
    # see: http://www.perlmonks.org/?node_id=668987
    # and: http://perl5.git.perl.org/perl.git/commit/eaf7a4d2
    return { map { utf8::upgrade($_) unless ref($_); $_ } %$args };
}

sub _json_request {
    my ($self, $http_method, $uri, $args, $authenticate, $dt_parser) = @_;
    
    my $msg = $self->_prepare_request($http_method, $uri, $args, $authenticate);
    my $res = $self->_send_request($msg);

    return $self->_parse_result($res, $args, $dt_parser);
}

sub _prepare_request {
    my ($self, $http_method, $uri, $args, $authenticate) = @_;

    my $msg;

    my %natural_args = $self->_natural_args($args);

    $self->_encode_args(\%natural_args);
    if( $http_method eq 'PUT' ) {
        $msg = PUT(
            $uri,
            'Content-Type' => 'application/x-www-form-urlencoded',
            Content => $self->_query_string_for( \%natural_args ) );
    }
    elsif ( $http_method =~ /^(?:GET|DELETE)$/ ) {
        $uri->query($self->_query_string_for(\%natural_args));
        $msg = HTTP::Request->new($http_method, $uri);
    }
    elsif ( $http_method eq 'POST' ) {
        # if any of the arguments are (array) refs, use form-data
        $msg = (first { ref } values %natural_args)
             ? POST($uri,
                    Content_Type => 'form-data',
                    Content => [
                        map { ref $_ ? $_ : encode_utf8 $_ } %natural_args,
                    ],
               )
             : POST($uri, Content => $self->_query_string_for(\%natural_args))
             ;
    }
    else {
        croak "unexpected HTTP method: $http_method";
    }

    $self->_add_authorization_header($msg, \%natural_args) if $authenticate;

    return $msg;
}

# Make sure we encode arguments *exactly* the same way Net::OAuth does
# ...by letting Net::OAuth encode them.
sub _query_string_for {
    my ( $self, $args ) = @_;

    my @pairs;
    while ( my ($k, $v) = each %$args ) {
        push @pairs, join '=', map URI::Escape::uri_escape_utf8($_,'^\w.~-'), $k, $v;
    }

    return join '&', @pairs;
}

# Basic Auth, overridden by Role::OAuth, if included
sub _add_authorization_header {
    my ( $self, $msg ) = @_;

    $msg->headers->authorization_basic($self->username, $self->password)
        if $self->has_username && $self->has_password;
}

sub _send_request { shift->ua->request(shift) }

has _decode_html_entities_visitor => (
    is => 'rw',
    lazy => 1,
    default => sub {
        Data::Visitor::Callback->new(
            plain_value => sub {
                return unless defined $_;

                $_ = HTML::Entities::decode_entities($_);
            }
        )
    },
);

sub _decode_html_entities { shift->_decode_html_entities_visitor->visit(@_) }

# By default, Net::Twitter does not inflate objects, so just return the
# hashref, untouched. This is really just a hook for Role::InflateObjects.
sub _inflate_objects { return $_[2] }

sub _parse_result {
    my ($self, $res, $args, $datetime_parser) = @_;

    # workaround for Laconica API returning bools as strings
    # (Fixed in Laconi.ca 0.7.4)
    my $content = $res->content;
    $content =~ s/^"(true|false)"$/$1/;

    my $obj = try { $self->_from_json($content) };
    $self->_decode_html_entities($obj) if $obj && $self->decode_html_entities;

    # filter before inflating objects
    if ( (my $since = delete $args->{-since}) && defined $obj ) {
        $self->_filter_since($datetime_parser, $obj, $since);
    }

    # inflate the twitter object(s) if possible
    $self->_inflate_objects($datetime_parser, $obj);

    # Twitter sometimes returns an error with status code 200
    if ( ref $obj && reftype $obj eq 'HASH' && (exists $obj->{error} || exists $obj->{errors}) ) {
        die Net::Twitter::Error->new(twitter_error => $obj, http_response => $res);
    }

    return $obj if $res->is_success && defined $obj;

    my $error = Net::Twitter::Error->new(http_response => $res);
    $error->twitter_error($obj) if ref $obj;

    die $error;
}

# Return a DateTime object, given $since as one of:
# - DateTime object
# - string in format "YYYY-MM-DD"
# - string in the same format as created_at values for the particular
# Twitter API (Search and REST have different created_at formats!)
# - an integer with epoch time (in seconds)
# Otherwise, throw an exception
sub _since_as_datetime {
    my ($self, $since, $parser) = @_;

    return $since if blessed($since) && $since->isa('DateTime');

    if ( my ($y, $m, $d) = $since =~ /^(\d{4})-(\d{2})-(\d{2})$/ ) {
        return DateTime->new(month => $m, day => $d, year => $y);
    }

    return eval { DateTime->from_epoch(epoch => $since) }
        || eval { $parser->parse_datetime($since) }
        || croak
"Invalid 'since' parameter: $since. Must be a DateTime, epoch, string in Twitter timestamp format, or YYYY-MM-DD.";
}

sub _filter_since {
    my ($self, $datetime_parser, $obj, $since) = @_;

    # $since can be a DateTime, an epoch value, or a Twitter formatted timestamp
    my $since_dt = $self->_since_as_datetime($since, $datetime_parser);

    my $visitor = Data::Visitor::Callback->new(
        ignore_return_values => 1,
        array => sub {
            my ($visitor, $data) = @_;

            return unless $self->_contains_statuses($data);

            # truncate $data when we reach an item as old or older than $since_dt
            my $i = 0;
            while ( $i < @$data ) {
                last if $datetime_parser->parse_datetime($data->[$i]{created_at}) <= $since_dt;
                ++$i;
            }
            $#{$data} = $i - 1;
        }
    );

    $visitor->visit($obj);
}

# check an arrayref to see if it contains statuses
sub _contains_statuses {
    my ($self, $arrayref) = @_;

    my $e = $arrayref->[0] || return;
    return unless ref $e && reftype $e eq 'HASH';
    return exists $e->{created_at} && exists $e->{text} && exists $e->{id};
}

sub _user_or_undef {
    my ( $self, $orig, $type, @rest ) = @_;

    return try {
        $orig->($self, @rest);
    }
    catch {
        die $_ unless /The specified user is not a $type of this list/;
        undef;
    };
}

1;

__END__

=for Pod::Coverage BUILD credentials

=head1 NAME

Net::Twitter::Core - Net::Twitter implementation

=head1 SYNOPSIS

use Net::Twitter::Core;

my $nt = Net::Twitter::Core->new_with_traits(traits => [qw/API::Search/]);

my $tweets = $nt->search('perl twitter')

=head1 DESCRIPTION

This module implements the core features of C<Net::Twitter>. See L<Net::Twitter> for full documentation.

Although this module can be used directly, you are encouraged to use C<Net::Twitter> instead.

=head1 AUTHOR

Marc Mims <marc@questright.com>

=head1 LICENSE

Copyright (c) 2009 Marc Mims

The Twitter API itself, and the description text used in this module is:

Copyright (c) 2009 Twitter

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
Something went wrong with that request. Please try again.