shapeways/lib/Shapeways/Account.pm

100 lines
No EOL
2.3 KiB
Perl

package Shapeways::Account;
use Moose;
use MooseX::Storage;
use Shapeways;
use Shapeways::Logger;
use List::MoreUtils qw/uniq/;
use Data::Dumper;
with Storage(format => 'JSON');
has account_name => (
is => 'ro',
);
has creations => (
is => 'ro',
lazy => 1,
builder => '_get_creations',
);
has favorites => (
is => 'ro',
lazy => 1,
builder => '_get_favorites',
);
# TODO this will make a list of their lists that contains items. probably wants a new class
has lists => (
is => 'ro',
lazy => 1,
builder => '_get_lists',
);
sub _parse_page {
my $self = shift;
my $tree = shift;
# Gotta uniq this since the link is duplicated for text and pictures. easier to just do this than futz with the xpath
my @items = uniq $tree->findvalues('//a[@class="product-url"]/@href');
my $next_page = ($tree->findvalues('//a[span[contains(@class,"results-page-next")]]/@href'))[0];
return {
items => \@items,
next_page => $next_page,
};
}
sub _get_creations {
my $self = shift;
# first page starts here
my $next_page = 'http://www.shapeways.com/designer/'. $self->account_name;
my @item_urls;
# Loop until we failed to find another page
while ($next_page) {
my ($tree_path, $code) = Shapeways->get_url($next_page);
my $data = $self->_parse_page($tree_path);
push @item_urls, @{$data->{items}};
$next_page = $data->{next_page};
}
my @items = map {Shapeways::Item->new_from_url($_)} @item_urls;
return \@items;
}
sub _get_lists {return undef};
sub _get_favorites {return undef};
sub serialize {
my $self = shift;
my %opts = (
mkdir => 1, # make a directory for the files
dirname => $self->account_name,
path => './',
@_
);
# Download the file, rename it via the title, save a corrosponding .txt file with the description, title and original file name
log_info {"Making author dir: ", $self->account_name};
my $dir = $opts{path}.'/'.$opts{dirname}.'/';
log_debug {"dir => ", $dir};
mkdir($dir) or die "Couldn't mkdir $dir => $!";
# TODO save more than the first item
for my $item ($self->creations->[0]) {
# Serialize under the author directory
$item->serialize(path => $dir);
}
}
1;