#!/usr/bin/perl use XML::FOAF; use URI; my $uri = $ARGV[0]; my $file = $ARGV[1]; my $rdfs='http://www.w3.org/2000/01/rdf-schema#'; die "Usage $0 ; eg $0 http://www.livejournal.com/users/danbri/data/foaf" unless $uri; # I'm messing around with XML::FOAF based on some code from crschmidt and ben trott # danbri@w3.org # Further playing leads to a script which will go out and fetch all your friends # recursively out to a certain level, and print name/nick/mbox/mbox_sha1sum, along # with level. # Implements some very simple caching (nothing stored locally, just a "seen" cache) # This script is very rough. I don't recommend anyone using it. #crschmid@uiuc.edu %uris; %bigarray; my $p = personFromURI($uri); $uris{$uri} = 1; die "No person described in FOAF data" unless $p; personBlurb($p, 1); knowsSummary($p, 1); foreach (%bigarray) { print $_->{level} . " " . $_->{nick} . " " . $_->{name} . " " . $_->{mbox} . " " . $_->{mbox_sha1sum} . "\n" if $_->{mbox_sha1sum}; } ##### Misc utilities sub dataRefs { my $p=shift; my @data=(); foreach my $mate (@{$p->knows}) { my $sa=$mate->get($rdfs.'seeAlso'); push (@data, $sa); } return \@data; } sub personBlurb { my $p=shift; my $level = shift; if ($p) { $bigarray{$p->nick}->{'name'} = $p->name if $p->name; $bigarray{$p->nick}->{'nick'} = $p->nick if $p->nick; $bigarray{$p->nick}->{'mbox_sha1sum'} = $p->mbox_sha1sum if $p->mbox_sha1sum; $bigarray{$p->nick}->{'mbox'} = $p->mbox if $p->mbox; $bigarray{$p->nick}->{'level'} = $level; } } sub knowsSummary { my $p = shift; my $level = shift; if ($p) { foreach my $mate (@{$p->knows}) { personBlurb($mate, $level); if ($level < 3){ my @d= @{dataRefs($p)}; print STDERR "reading friends of ".$mate->nick.", at level $level\n"; foreach my $uri (@d) { print STDERR "FOAF from: $uri\n"; if (!$uris{$uri}) { my $p2=personFromURI($uri); $uris{$uri} = 1; personBlurb($p2, $level); knowsSummary($p2, $level + 1); } else { print STDERR "Seen $uri, skipping.\n"; } } } } } } sub personFromURI { my $uri = shift; my $foaf = XML::FOAF->new(URI->new($uri), "FOAF-Fetch, danbri+fetchfoaf\@w3.org"); my $p = $foaf->person if $foaf; # replace with PPD code? see end of doc for snippet return $p; } sub allSeeAlso { my $p=shift; ## Get all rdfs:seeAlso arcs in the foaf graph: # this shows a 'raw RDF' style of interaction with the data. my @res=[]; my $enum = $foaf->{model}->getStmts(undef,RDF::Core::Resource->new($rdfs.'seeAlso'),undef); my $statement = $enum->getFirst; while (defined $statement) { push @res, $statement->getObject->getLabel."\n"; $statement = $enum->getNext } $enum->close; return @res; }