O'Reilly logo

Flickr Hacks by Jim Bumgardner, Paul Bausch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #26. Find Your Friends' Favorite Tags

With some Perl and the Flickr API, you can see which tags are most popular among your contacts.

Tagging individual photos [Hack #10] on Flickr can help you organize your own photos. It also means Flickr can show interesting group patterns, like the Tags page shown in Figure 4-7.

Popular tags page

Figure 4-7. Popular tags page

With this page, at a glance you can see which tags are being used most often by all Flickr members. With a bit of scripting, you can narrow down this list and find out which tags are being used most by just your Flickr contacts. This list of contacts' tags is a good way to visualize what your friends are interested in, and it's a bit more personal than the overall tags page.

The Code

To run this hack, you'll need a couple of nonstandard Perl modules. Make sure you have Flickr::API (http://search.cpan.org/~iamcal/Flickr-API/lib/Flickr/API.pm) for working with Flickr, and XML::Parser::Lite::Tree::Xpath (http://search.cpan.org/~iamcal/XML-Parser-Lite-Tree-XPath-0.02/XPath.pm) for sorting through the API responses.

With those two modules in place, copy the following code to a file called friends_tags_list.pl. Add your NSID and Flickr API key (both of which you can find at the Flickr API site, http://www.flickr.com/services/api/) to the code.

#!/usr/bin/perl
# friends_tags_list.pl
#
# Find out which tags your Flickr contacts are 
# using most often. This script contacts the 
# Flickr API, gets a list of your contacts, and 
# finds their most popular tags. The script then
# adds up the totals and lists the tags by the
# most popular. You can get a Flickr
# API key and look up your Flickr NSID at:
#
# http://www.flickr.com/services/api/
# 
# Usage: perl friends_tags_list.pl

use strict;
use Flickr::API;
use Flickr::API::Response;
use XML::Parser::Lite::Tree::XPath;

# Set your NSID
my $nsid = 'insert your NSID';
my $api_key = 'insert your Flickr API key';
my @tags;
my @sortTags;

# Get your list of contacts
my $api = new Flickr::API({'key' => $api_key});

my $response = $api->execute_method('flickr.contacts.getPublicList', {
                'user_id' => $nsid,
});

# Make sure there's a response
if (!$response->{success}) {
    die "Contact list not found! $response->{error_message}";
}

# Parse the response and loop through contacts
my $xpath = new XML::Parser::Lite::Tree::XPath($response->{tree});
my @contacts = $xpath->select_nodes('/contacts/contact');
foreach (@contacts) {
    my $usernsid = $_->{attributes}->{nsid};

    # Find this contact's tags
    my $user_res = $api->execute_method('flickr.tags.getListUserPopular', {
                'user_id' => $usernsid,
    });
    
    # Make sure there's a response
    if (!$user_res->{success}) {
        warn "Tags not found! $user_res->{error_message}";
    }
    
    # Parse the response and loop through photos
    my $userxpath = new XML::Parser::Lite::Tree::XPath($user_res->{tree});
    my @utags = $userxpath->select_nodes('/who/tags/tag');
    foreach (@utags) {
        my $match_index = 0;
        my $usertagcount = $_->{attributes}->{count};
        my $usertag = $_->{children}[0]->{content};
        
        # Make sure there's only one entry for each tag    
        for my $i ( 0 .. $#tags) {
            if ($tags[$i]->{tag} eq $usertag) {
                $match_index = $i;
            }
        }

        # If a tag already exists, add to the total
        # otherwise, add the new tag to the hash
        if ($match_index > 0) {
$tags[$match_index]->{tagcount} = $tags[$match_index]->{tagcount} + $usertagcount;
        } else {
            my $thisTag = {
                tag => $usertag,
                tagcount => $usertagcount,
            };
            push @tags, $thisTag;
        }
    }
}

# --- Begin Output -----

# Sort the hashes numerically to put popular tags first
@sortTags = sort { $b->{'tagcount'} <=> $a->{'tagcount'} } @tags;

# Print the tags in a simple text list
foreach (@sortTags) {
    my $tagcount = $_->{tagcount};
    my $tag = $_->{tag};
    print "$tag ($tagcount)\n";
}

The script fetches a list of contacts for the given NSID and uses the flickr.tags.getListUserPopular API method to find the 10 most popular tags for each contact. The script also adds the totals of the tags to see which tags are the most popular across all of your contacts.

Running the Hack

To run the script, call it from a command prompt, piping the output to a text file, like so:

perl friends_tags_list.pl > friends_tags.txt

Take a look at friends_tags.txt and you should see something like the following list of tags:

cameraphone (7676)
sanfrancisco (3345)
archive (2999)
7610 (2628)
nyc (1765)
sxsw (1608)
2001 (1512)
sxswi (1259)
travel (1248)
sf (1212)
moblog (1198)
europe (1109)
sxswinteractive (1018)
livemusic (986)
austintx (945)
oregon (733)
sharptm150 (636)
dog (606)
2005 (570)
s700i (559)
lifeblog (523)
nokia6630 (522)
seattle (479)
...

You'll see your contacts' most popular tags, along with the number of times each tag has been used by your contacts. You might learn some things about your friends that you didn't know already, and you might find some tags that you can start using as well.

Hacking the Hack

If you want to display your contacts' tags in a tag map with relative font sizes similar to the Tags page at Flickr, you can tweak this script a bit to output HTML instead of text. Replace everything after the # --- Begin Output --- comment with the following code:

# --- Begin Output -----

# Sort the hashes alphabetically
@sortTags = sort { $a->{'tag'} cmp $b->{'tag'} } @tags;

# Print the top of the page
print <<"END_HEADER";
<html>
<head>
    <style type="text/css">
        body { font-family:arial; width:760px;}
        h3 { font: normal 18px Arial, Helvetica, sans-serif; color: #FF0084; margin-bottom: 5px; }
    </style>
</head>
<body>
<h3>My Contacts' Tags</h3>
END_HEADER

# Print the tags as an HTML Tag Map with relative sizes
foreach (@sortTags) {
    my $fontsize;
    my $tagcount = $_->{tagcount};
    $tagcount /= 10;
    my $tag = $_->{tag};
    my $minfontsize = 9;
    my $fontsize = 9 + $tagcount;
    if ($fontsize >= 60) {
        $fontsize = 60;    
    }
    $fontsize = sprintf("%.0f", $fontsize); 
    print "<a href=\"http://www.flickr.com/photos/tags/$tag\"";
    print "style=\"font-size:$fontsize";
    print "px;font-weight:normal;text-decoration:none;";
    print "line-height:110%;\">$tag</a>&nbsp; \n";
}

# Print the bottom of the page
print <<"END_FOOTER";
</body>
</html>
END_FOOTER

This new code sorts the tags alphabetically instead of by the number of times each tag has been used by your contacts. The script then generates an HTML page, giving each tag a font size based on the number of times it has been used. Run the code in the same way, but send the output to an HTML file, like this:

perl friends_tag_list.pl > friends_tags.html

Open friends_tags.html in a browser, and you'll see a list of tags like the one in Figure 4-8.

Viewing friends' tags as a tag map

Figure 4-8. Viewing friends' tags as a tag map

Each tag links to the public tag page for that particular tag, so you can see how Flickr members across the site have used the tag. With some more Flickr API work, you could probably find a way to link to your contacts' photos for a given tag, but that's another hack! In the meantime, you can get a quick glance at your friends' tagging. And once you've mastered your friend's tags, you might want to try your hand at other ways to make tag clouds [Hack #14] .

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required