Net::Delicious::Export::Post - shared functions for exporting del.icio.us posts


Net-Delicious-Export documentation Contained in the Net-Delicious-Export distribution.

Index


Code Index:

NAME

Top

Net::Delicious::Export::Post - shared functions for exporting del.icio.us posts

SYNOPSIS

Top

 use Net::Delicious;
 use Net::Delicious::Export::Post qw (group_by_tag);

 my $del = Net::Delicious->new({...});
 my $it  = $del->recent_posts();

 my $hr_ordered = group_by_tag($it);

DESCRIPTION

Top

Shared function for exporting del.icio.us posts.

FUNCTIONS

Top

&group_by_tag(Net::Delicious::Iterator,\&sort_function)

Build a nested hash reference of posts grouped by tag. This function will DWIM with "hierarchical" tags.

Posts for any given tag set will be grouped as an array reference. They will be ordered by their timestamp.

Valid arguments are :

Returns a hash reference.

&mk_bookmarkid(Net::Delicious::Post)

Returns a Net::Delicious::Export::Post::Bookmarkid object.

The object subclasses Net::Delicious::Post but since its stringify method is overloaded to return the value of its bookmarkid method you can, pretty much, just treat it like a string.

VERSION

Top

1.1

DATE

Top

$Date: 2005/12/11 19:40:53 $

AUTHOR

Top

Aaron Straup Cope <ascope@cpan.org>

SEE AlSO

Top

Net::Delicious::Export

LICENSE

Top

Copyright (c) 2004 Aaron Straup Cope. All Rights Reserved.

This is free software, you may use it and distribute it under the same terms as Perl itself.


Net-Delicious-Export documentation Contained in the Net-Delicious-Export distribution.
use strict;

package Net::Delicious::Export::Post;
use base qw (Exporter);

# $Id: Post.pm,v 1.6 2005/12/11 19:40:53 asc Exp $

use vars qw ($VERSION @EXPORT_OK);

$VERSION = '1.1';

@EXPORT_OK = qw (group_by_tag
		 mk_bookmarkid);

# used by &_addbm

my $by_time = sub {
    $a->time() cmp $b->time();
};


sub group_by_tag {
    my $posts = shift;
    my $sort  = shift;

    my %ordered = ();

    while (my $bm = $posts->next()) {

	# Create a list of tags

	my $tag = $bm->tag() || "unsorted";
	$tag =~ s/\s{2,*}/ /g;

	my @tags = sort $sort split(/[\s,]/,$tag);

	# Pull the first tag off the list
	# and use it as the actual bookmark

	&_addtag(\%ordered, shift @tags, $bm);

	# Everything else is just an alias

	map { 
	    &_addtag(\%ordered, $_, &mk_bookmarkid($bm));
	} @tags;
    }

    return \%ordered;
}

sub mk_bookmarkid {
    return Net::Delicious::Export::Post::Bookmarkid->new($_[0]);
}


sub _addtag {
    my $dict = shift;
    my $tag  = shift;
    my $bm   = shift;

    # print STDERR "[add tag] '$tag' '$bm'\n";

    my @tree  = ($tag =~ m!/!) ? grep { /^\w/ } split("/",$tag) : ($tag);
    my $count = scalar(@tree);

    if ($count == 1) {
	$dict->{$tag} ||= [];
	&_addbm($dict->{$tag}, $bm);
	return;
    }

    my $ref     = $dict;
    my $current = 1;

    map {

      if ($current == $count) {
	  $ref->{$_} ||= [];
	  &_addbm($ref->{$_},$bm);
      }

      else {
	$ref->{$_} ||= {};
	$ref = $ref->{$_};
      }
      
      $current++;

    } @tree;
}

sub _addbm {
    my $list = shift;
    my $bm   = shift;
    
    @$list = sort $by_time (@$list,$bm);
}

package Net::Delicious::Export::Post::Bookmarkid;
use base qw (Net::Delicious::Post);

use MD5;

use overload q("") => sub { shift->bookmarkid() };

sub new {
    my $pkg = shift;
    my $bm  = shift;

    my %id = %$bm;
    $id{bookmarkid} = MD5->hexhash($bm->href());

    return bless \%id, $pkg;
}

sub bookmarkid {
    my $self = shift;
    return $self->{bookmarkid};
}

return 1;