Created
February 28, 2014 22:57
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/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