ESN 95056-090121-867699-65


Document Name: Destroying Twitter Friendships with twitterdeaf.pl
Document Description: Destroying Twitter Friendships with twitterdeaf.pl

Destroying Twitter Friendships with twitterdeaf.pl


2009/01/21

As I said this morning at Twitter Deaf, I decided to unfollow people who follow to many other people. No offense, but if you follow a lot of people, you are effectively deaf to Twitter messages, so why should I follow you? You can't "hear" me and if I'm following hundreds of people who follow me, I can't "hear" you, either. I started deleting people by hand but quickly realized that was silly: Twitter has a simple API.

So I wrote a little Perl script to hunt the people with too many friends. I decided to start the cut at 300 - under that, you are safe for this first pass. I may be more draconian later and I may add more criteria; for example I may unfollow if you are a daily chatterbox. Again, no offense but chatty people can make it hard to hear other people.

Some things to explain about the code: I pulled info from the Twitter API page. I wrote it in simple Perl, no hard to find modules.

I've always thought LWP's "get" was odd for returning a string rather than an array of lines, but that's what it does, so it has to be split into an array.

Twitter limits how many calls you can make per hour so watch out for that if you have a lot of people.

You need to be logged in to Twitter before you run this. You can do authentication with LWP, but why bother?

I didn't put in error checking for failure to be logged in or for exceeding the call limit. You can do that with the regular LWP also if you want to go to the trouble, but this is a one-off, so why bother yet again?

I decided NOT to call the API to "destroy" the friendship and simply used this to get a list of the people and their counts. If you need to get beyond page 1 (100 friends), you need to set $x to the next page and so on. Of course you can put this in a loop but remember the 200 status call limit.. you'll need to be waiting around anyway. What this saves you is having to click on each person before you decide their fate.

I got my list and then went back into Twitter and deleted each person manually. Note there is plenty of other information in the status returned; you may want to print out more of it (like Updates, which is "statuses_count" in the "show" results - dumb, dumb API!) to help you decide.

If you really want to call "destroy" automatically, go ahead - that's your business, not mine.

After doing this, I'm down to 46 people. If I need to cut it more, I will, but it feels pretty good. I feel like I can hear again - I'm not Twitter Deaf..

 #!/usr/bin/perl
 use LWP::Simple;
 use URI::URL;
 my $count=0;
 my $max=300;
 
  my $url= url('http://twitter.com/statuses/friends/pcunix.xml');
  $url->query_form(page=>$x);
  my $response=get($url);
  my @responses=split /\n/,$response; 
  foreach (@responses) {
    if (/<screen_name>/) {
      push @ids,$_;
      $count++;
   }
 }
 foreach (@ids) {
   s/<screen_name>//;
   s/<.screen_name>/.xml/;
   $id=$_;
   $count++;
   if ($count > 199)  {
      print "Pausing 60 minutes..\n";
      $count=0;
      sleep 3600;
    }
   my $show=get("http://twitter.com/users/show/$_");
   my @show= split /\n/,$show;
   foreach (@show) {
     if (/<friends_count>/) {
       s/<friends_count>//;
       s/<.friends_count>//;
       if ($_ > $max) {
            print "($count) $id $_\n";
            get("http://twitter.com/friendships/destroy/$id");
       } # large number
     } # if count
    } # foreach @show
  } # foreach @ids
 
 

Author: Anthony Lawrence - Contact Author
Publisher: Anthony Lawrence
Licensee Name: Anthony Lawrence
Reference URL: http://aplawrence.com/Web/destroy_twitter_friends.html
Copyright: All Rights Reserved
Registration Date: 1/21/2009 10:53:10 PM UTC
Views: 183




NUMLY.COM