Skip to content

Instantly share code, notes, and snippets.

@Ovid
Last active January 6, 2017 09:12
Show Gist options
  • Save Ovid/0fcbf988bd2ec9eaace1063c841df2c1 to your computer and use it in GitHub Desktop.
Save Ovid/0fcbf988bd2ec9eaace1063c841df2c1 to your computer and use it in GitHub Desktop.
Multi-line tweets from the command line
#!/usr/bin/env perl
use Modern::Perl;
use Net::Twitter;
use Config::Tiny;
use File::HomeDir;
use utf8::all;
use Text::Wrap;
use Term::ANSIColor;
use autodie ':all';
use Regexp::Common 'URI';
my $config_file = File::HomeDir->my_home . "/.twitter";
die "$config_file is missing\n" if not -e $config_file;
my $config = Config::Tiny->read( $config_file, 'utf8' );
# https://perlmaven.com/sending-tweets-from-a-perl-script
my $nt = Net::Twitter->new(
ssl => 1,
traits => [qw/API::RESTv1_1/],
consumer_key => $config->{overseasexile}{api_key},
consumer_secret => $config->{overseasexile}{api_secret},
access_token => $config->{overseasexile}{access_token},
access_token_secret => $config->{overseasexile}{access_token_secret},
);
my $tweet = get_tweet(@ARGV);
# https://support.twitter.com/articles/78124
my $uri_placeholder = 'X' x 23;
my $http_re = $RE{URI}{HTTP}{ -scheme => 'https?' }{-keep};
my @uris;
while ( $tweet =~ /$http_re/g ) {
push @uris => $1;
}
$tweet =~ s/$http_re/$uri_placeholder/g;
$Text::Wrap::columns = 134;
my @lines = split /\n/ => wrap( '', '', $tweet );
if ( @lines > 1 ) {
my $total = @lines;
my $count = 1;
foreach (@lines) {
$_ .= " $count/$total";
$count++;
if ( length($_) > 140 ) {
die "bad line: $_";
}
}
}
say colored( ['white on_black'], 'Your tweets will read as follows' );
foreach (@lines) {
my $length = length($_);
if (/$uri_placeholder/) {
my $next = shift @uris;
s/$uri_placeholder/$next/;
}
print colored( ['bright_red on_black'], $_ );
say " length: $length characters";
}
print "Is this OK? [y/N] ";
my $response = <STDIN>;
exit unless $response =~ /^\s*[yY]/;
foreach (@lines) {
$nt->update($_);
}
sub get_tweet {
my @argv = @_;
say colored( ['white on_black'], 'Enter tweet stream:' );
my $tweet = '';
if (@argv) {
# This is a better way of handling multi-line tweets because I can more
# easily edit what I'm writing.
my $file = shift @argv;
open my $fh, '<', $file;
$tweet = do { local $/; <$fh> };
}
else {
while ( chomp( my $input = <STDIN> ) ) {
last unless $input =~ /\w/;
$tweet .= " $input";
}
}
$tweet =~ s/^\s+|\s+$//g;
$tweet =~ s/\s+/ /g;
say "Raw input: $tweet";
return $tweet;
}
__END__
=head1 NAME
tweets - A simple multi-line tweeting tool written in Perl
=head1 SYNOPSIS
perl tweets filename # reads text in file
perl tweets # reads from command line
The first version reads your tweets from a file. The second version allows you
to type your tweets and accepts input until it encounters a line not matching
C<\w>.
The text is then broken up into one or more tweets. If more than one tweet,
each tweet will be numbered C<$tweet_number/$total_tweets>. Assumes you don't
have more than 99 tweets in a single stream.
URLs are counted correctly. However, punctuation is often considered part of
the URL and is hard to distinguish. So if you see this:
I like http://www.overseas-exile.com/. You should read it.
Be aware that the trailing period will be read as part of the URL, generating
a 404. So you probably want to write this instead:
I like http://www.overseas-exile.com/ You should read it.
Again with other punctuation characters:
This was written with Net::Twitter (http://search.cpan.org/dist/Net-Twitter/)
In the above, the trailing parenthesis will be included in the URL, again
generating a 404.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment