Skip to content

Instantly share code, notes, and snippets.

@zachwhalen
Created February 28, 2014 22:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachwhalen/9281774 to your computer and use it in GitHub Desktop.
Save zachwhalen/9281774 to your computer and use it in GitHub Desktop.
This is a slightly cleaned up version of the code that runs @rom_txt. It's pretty simple -- it just loads a big text file and picks a random few lines.
#/usr/bin/local/perl
use Net::Twitter;
use Data::Dumper;
use LWP::Simple;
# get API credentials at http://dev.twitter.com
my $nt = Net::Twitter->new(
traits => [qw/API::RESTv1_1/],
consumer_key => '',
consumer_secret => '',
access_token => '',
access_token_secret => '',
ssl => 1
);
# path to text file
$big = '';
open READ, "$big";
@lines = <READ>;
close READ;
# pick a starting line
$seed = int(rand($#lines));
# my text file is organized where the first string on each line is a rom name, then there's a tab, then some characters
($rom, $txt) = split("\t", $lines[$seed]);
# make room for the rom name
$meta = length($rom) + 14;
if (length $text < (140 - $meta)){
# start building a tweet
$msg = $text;
$add = 1;
$try = $text;
while (length($msg) < $len){
($so, $next) = split("\t",$lines[$seed + $add]);
$try = $msg . $next;
if (length($try) > $len){
last;
}else{
$msg = $try;
}
$add += 1;
}
$tweet = $msg . " - #" . $rom;
}else{
# compose as multiple tweets
while (length $txt > (140 - $meta)){
# .. maybe I'll add this later
}
}
# do tweet
my $result = $nt->update("$tweet");
# Error messages if it didn't work
if ( my $err = $@ ) {
die $@ unless blessed $err && $err->isa('Net::Twitter::Error');
warn "HTTP Response Code: ", $err->code, "\n",
"HTTP Message......: ", $err->message, "\n",
"Twitter error.....: ", $err->error, "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment