#!/usr/bin/perl ############################# # usage: # perl flickrdown.pl --key [your-api-key] --url http://www.flickr.com/photos/goodgrief/tags/Glamour/ --dir ./down --limit 100 # perl flickrdown.pl -k [your-api-key] -u http://www.flickr.com/photos/goodgrief/ # perl flickrdown.pl -k [your-api-key] -u http://www.flickr.com/photos/goodgrief/sets/1396354/ ############################# use strict; use Flickr::API::Photosets; #use Flickr::API::People; use Getopt::Long; use LWP::Simple; use Data::Dumper; # parameters my $p = undef; $p->{key} = ''; $p->{dir} = './'; $p->{limit} = 1000; $p->{url} = undef; GetOptions( '--key=s' => \$p->{key}, '--dir=s' => \$p->{dir}, '--limit=i' => \$p->{limit}, '--url=s' => \$p->{url}, ); if(!-e $p->{dir}){ mkdir $p->{dir}; } $p->{dir} .= '/' if(!$p->{dir} !~ /\/$/); $p->{url} =~ /\/([^\/]+?)\/([^\/]+?)\/?$/; if($1 eq 'tags') { &DownloadTag($p, $2); }elsif($1 eq 'sets'){ &DownloadSet($p, $2); }elsif($1 eq 'photos'){ &DownloadUser($p, $2); }else{ die "error: url invalid\n"; } sub DownloadTag{ my $p = shift; my $tag = shift; # user $p->{url} =~ /\/([^\/]+?)\/(?:[^\/]+?)\/(?:[^\/]+?)\/?$/; my $name = $1; my $api = new Flickr::API({'key' => $p->{key}}); my $html = get("http://www.flickr.com/photos/$name/"); $html =~ /onsubmit="_do_header_search\('(.*?)'/;#" my $id = $1; my $get_photos = new Flickr::API::Request({ 'method' => 'flickr.people.getPublicPhotos', 'args' => {user_id => $id}, }); my $photo_tree = $api->execute_request($get_photos); my $i = 1; $Data::Dumper::Indent = 1; for my $p_obj (@{$photo_tree->{tree}->{children}->[1]->{children}}){ next if(!exists $p_obj->{attributes}); die "number of pictures over limit" if($i > $p->{limit}); my $a = $p_obj->{attributes}; my $getphototags = new Flickr::API::Request({ 'method' => 'flickr.tags.getListPhoto', 'args' => {photo_id => $a->{id}}, }); my $tag_tree = $api->execute_request($getphototags); my @tags = (); for(@{$tag_tree->{tree}->{children}->[1]->{children}->[1]->{children}}){ next if(!exists $_->{children}); push @tags, $_->{attributes}->{raw}; } # print join ",", @tags; # print "\n$tag\n"; next if(!grep($_ eq $tag, @tags)); &down($a); $i++; } } sub DownloadSet{ my $p = shift; my $id = shift; # photosets id my $f = Flickr::API::Photosets->new($p->{key}); my $i = 1; my $photos_list = $f->getPhotos($id); for my $pstr (@{$photos_list->{photos}}){ die "number of pictures over limit" if($i > $p->{limit}); &down($pstr); $i++; } } sub DownloadUser{ my $p = shift; my $name = shift; # user my $api = new Flickr::API({'key' => $p->{key}}); =c nanka chigau >< my $peoplestr = Flickr::API::People->new($p->{key}); my $id = $peoplestr->findByUsername($name)->{id}; =cut =c koremo chigau >< >< my $get_id = new Flickr::API::Request({ 'method' => 'flickr.people.findByUsername', 'args' => {username => 'nearnearfuture'}, }); my $id_tree = $api->execute_request($get_id); my $id = $id_tree->{tree}->{children}->[1]->{attributes}->{id}; =cut my $html = get($p->{url}); $html =~ /onsubmit="_do_header_search\('(.*?)'/; #" my $id = $1; my $get_photos = new Flickr::API::Request({ 'method' => 'flickr.people.getPublicPhotos', 'args' => {user_id => $id}, }); my $photo_tree = $api->execute_request($get_photos); my $i = 1; for my $p_obj (@{$photo_tree->{tree}->{children}->[1]->{children}}){ next if(!exists $p_obj->{attributes}); die "number of pictures over limit" if($i > $p->{limit}); my $a = $p_obj->{attributes}; &down($a); $i++; } } sub down{ my $a = shift; my $url = "http://farm$a->{farm}.static.flickr.com/$a->{server}/$a->{id}_$a->{secret}_o.jpg"; print "downloading $a->{id}.jpg...\n"; getstore( $url, "$p->{dir}$a->{id}.jpg" ); }