randomfox (randomfox) wrote,
randomfox
randomfox

Friendfeed contact categorizer

This Perl script sorts your Friendfeed contacts according to reciprocation, i.e. it shows who are your mutual subscriptions, only subscribers, and only subscriptions. Because this script doesn't authenticate with Friendfeed, it doesn't work on users with private feeds.


#!perl -w
# ffsubs.pl - Categorize Friendfeed contacts according to whether they are
# mutual subscriptions, only subscriptions, or only subscribers.
#
# Usage: ffsubs.pl username
#
# Because this script doesn't authenticate with Friendfeed, it doesn't work
# on users with private feeds.

use strict;
use XML::Simple;
use LWP::UserAgent; 

my $MAXRETRY = 5;

# Call Friendfeed API.
sub call_friendfeed {
    my $url = shift;
    my $retrycount = 0;
    while (1) {
	my $ua = new LWP::UserAgent;

	my $response = $ua->get($url);
	$response->is_success and return $response->content;

	++$retrycount;
	$retrycount > $MAXRETRY and die $response->as_string;

	warn "Retrying...\n";
	sleep 3;
    }
}

# Report subscriptions/subscribers by reciprocation category.
sub report_subs {
    my $subscribers = shift;
    my $subscriptions = shift;

    my $count = 0;
    print "Mutual subscriptions:\n";
    for my $id (keys %$subscribers) {
	defined($subscriptions->{$id}) and print ++$count, ": $id\n";
    }
    print "\n";

    $count = 0;
    print "Subscriber only:\n";
    for my $id (keys %$subscribers) {
	defined($subscriptions->{$id}) or print ++$count, ": $id\n";
    }
    print "\n";

    $count = 0;
    print "Subscription only:\n";
    for my $id (keys %$subscriptions) {
	defined($subscribers->{$id}) or print ++$count, ": $id\n";
    }
}

# Parse XML and show subscriptions/subscribers report.
sub process_xml {
    my $xmlstr = shift;

    my $xml = XMLin($xmlstr, 
	ForceArray => ['subscriber', 'subscription', 'service'], 
	KeyAttr => []);

    my %subscribers;
    my %subscriptions;

    for my $user (@{$xml->{'subscriber'}}) {
	$subscribers{$user->{id}} = 1;
    }

    for my $user (@{$xml->{'subscription'}}) {
	# Ignore groups.
	$user->{type} eq 'group' and next;
	$subscriptions{$user->{id}} = 1;
    }

    report_subs(\%subscribers, \%subscriptions);
}

@ARGV >= 1 or die "Usage: $0 username\n";

my $username = shift;

my $xmlstr = call_friendfeed("http://friendfeed-api.com/v2/feedinfo/$username?format=xml");

process_xml($xmlstr);

__END__

Tags: friendfeed, perl
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 3 comments