The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.
#!/usr/bin/env perl
use Net::Twitter;
use Getopt::Long;
use App::Twpin ':all';
use Carp qw/carp croak/;

our $VERSION = 0.007;

my @commands = qw/config update/;
my $cmd      = shift @ARGV;

my $twpin;

if (!defined $cmd) {
    usage();
} elsif ($cmd eq 'config') {
    my ($user, $password, $apiurl, %opt, $encode);
    GetOptions(
        "user=s"   => \$user,
        "pass=s"   => \$pass,
        "apiurl=s" => \$apiurl,
        "encode=s" => \$encode,
    ) or usage();

    !$user && !$pass && !$apiurl && !$encode && usage();
    $user   and $opt{user}   = $user;
    $pass   and $opt{pass}   = $pass;
    $apiurl and $opt{apiurl} = $apiurl;
    $encode and $opt{encode} = $encode;

    tw_config(%opt);
} elsif ($cmd eq 'follow') {
    my $user;
    usage() unless $user = shift;
    tw_follow(get_instance(), $user);
} elsif ($cmd eq 'unfollow') {
    my $user;
    usage() unless $user = shift;
    tw_unfollow(get_instance(), $user);
} elsif ($cmd eq 'update') {
    tw_update(get_instance(), @ARGV);
} elsif ($cmd eq 'status') {
	my $user;
    GetOptions("user=s" => \$user,);
    tw_list(get_instance(), $user);
} elsif ($cmd eq 'following') {
    tw_get_following(get_instance());
} elsif ($cmd eq 'follower') {
    tw_get_follower(get_instance());
} else {
    usage();
}

sub usage {
    print STDERR <<"usage";

twpin version $VERSION
twpin config [-u|--user] [-p|--pass] [-a|--apiurl] [-e|--encode]
        build or rebuild configuration file
      status [-u|--user screenname]
        list recent status
      update "tweets"
        update tweets to twitter
      follow screen_name
        follow somebody on twitter
      unfollow screen_name
        unfollow somebody on twitter
      follower
        list your followers
      following
        list your followings
      help
        show this message

usage
    exit;
}

sub get_instance {

    if (!$twpin) {
        $twpin = Net::Twitter->new(
            traits => [qw/API::REST/],
            tw_load_config(),
        ) or croak "error";
    }
    return $twpin;
}

__END__