Skip to content

Instantly share code, notes, and snippets.

@semifor
Last active January 13, 2017 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save semifor/6d15c5443511a8eed0f114b7c56a1cbb to your computer and use it in GitHub Desktop.
Save semifor/6d15c5443511a8eed0f114b7c56a1cbb to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# ABSTRACT: Twitter API CLI
# Examples:
#
# twitter-api verify_credentials
# twitter-api --screen_name=semifor show_user
# twitter-api update 'current status: hackking'
#
# You can use get and post, to:
#
# twitter-api get account/verify_credentials
use 5.14.1;
use warnings;
use Getopt::Casual;
use JSON::MaybeXS;
use Twitter::API;
my $quiet = delete $ARGV{q};
my $endpoint = shift @ARGV // die 'no endpoint specified';
my ( $key, $secret, $token, $token_secret ) = map {
my $k = 'TWITTER_API_'.$_;
$ENV{$k} // die "\$ENV{$k} required";
} qw/CONSUMER_KEY CONSUMER_SECRET ACCESS_TOKEN ACCESS_TOKEN_SECRET/;
my $client = Twitter::API->new_with_traits(
traits => [ qw/ApiMethods NormalizeBooleans RetryOnError/ ],
consumer_key => $key,
consumer_secret => $secret,
access_token => $token,
access_token_secret => $token_secret,
);
my $method = $client->can($endpoint) || die "invalid endpoint: $endpoint";
my $r = $client->$method(@ARGV, \%ARGV);
say JSON::MaybeXS->new(utf8 => 1, pretty => 1)->encode($r) unless $quiet;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment