Skip to content

Instantly share code, notes, and snippets.

@waffle2k
Created January 13, 2011 17:01
Show Gist options
  • Save waffle2k/778184 to your computer and use it in GitHub Desktop.
Save waffle2k/778184 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use JSON;
use Data::Dumper;
use Getopt::Long;
use constant MAX => 140;
use constant MAXGOBACK => 999999;
use constant MAXLOOKUP => 99;
sub convertIdsToScreennames
{
my ($screenname,$a_ref) = @_;
my $cmd = "http://api.twitter.com/1/users/lookup.json?screen_name=$screenname&user_id=";
while( scalar @$a_ref > 0 ){
my @lookup;
my $i = 0;
while( $i++ < MAXLOOKUP ){
push( @lookup, pop @$a_ref );
}
print "Executing $cmd" . join( ",", @lookup ) . "\n";
my $cmd2 = $cmd . join( ",", @lookup );
my $result = `wget -O - $cmd2 2>/dev/null`;
chomp( $result );
print $result , "\n";
}
}
my ($screenname,$friendlist,$goback,$verbose);
$goback = int(MAXGOBACK);
my $existingfriends = {};
my $options = GetOptions (
"screenname=s" => \$screenname,
"friendlist=s" => \$friendlist,
"goback=s" => \$goback,
"verbose" => \$verbose,
);
die( "Please provide a screenname\n" ) unless $screenname;
if( $friendlist ){
open FD, "<$friendlist";
while( <FD> ){
chomp;
$existingfriends->{$_}++;
}
close FD;
}
my $cmd = "http://api.twitter.com/1/statuses/friends.json?screen_name=$screenname";
my $results = `wget -O - $cmd 2>/dev/null`;
chomp( $results );
my $ref = from_json( $results );
my @friends;
for( @$ref ){
my $screenname = $_->{screen_name};
push( @friends, $screenname )
unless defined $existingfriends->{$screenname};
}
my $shoutout = '#ff';
my $i = 0;
my @shouted;
FRIEND: for my $friend (@friends){
last FRIEND if $i++ >= $goback;
print "Assigning shoutout[$shoutout] to newshoutout...\n" if $verbose;
my $newshoutout = $shoutout;
print "Appending friend[$friend] to newshoutout...\n" if $verbose;
$newshoutout .= " \@$friend";
push( @shouted, $friend );
if( length $newshoutout >= MAX ){
print "Printing shoutout...\n" if $verbose;
print "$shoutout\n";
print "Creating new shoutout string...\n" if $verbose;
$shoutout = "#ff \@$friend";
next FRIEND;
}
print "Assinging newshoutout [$newshoutout] to shoutout...\n" if $verbose;
$shoutout = $newshoutout;
}
print $shoutout . "\n"
unless( $shoutout eq '#ff' );
# update the friend list
if( $friendlist ){
open FD, ">>$friendlist" or die("Cannot open $friendlist for appending: $!\n");
print FD join( "\n", @shouted ) . "\n";
close FD;
}
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment