Created
July 27, 2008 08:40
-
-
Save dann/2767 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use WebService::Simple; | |
use Digest::MD5 qw(md5_hex); | |
use Path::Class qw(dir); | |
use Perl6::Say; | |
use HTTP::Async; | |
use HTTP::Request; | |
# config | |
my $api_key = "Your API Key"; | |
my $user_id = 'Your nsid'; | |
my $api = WebService::Simple->new( | |
base_url => "http://api.flickr.com/services/rest/", | |
param => { api_key => $api_key, } | |
); | |
sub main { | |
fetch_favorite_photos(); | |
} | |
sub fetch_favorite_photos { | |
my $photos = get_favorite_photo_lists(); | |
return unless $photos; | |
my $photo_urls = collect_photo_urls($photos); | |
download_photos($photo_urls); | |
} | |
sub get_favorite_photo_lists { | |
my $response = $api->get( | |
{ method => "flickr.favorites.getPublicList", | |
user_id => $user_id, | |
per_page => 500, | |
} | |
); | |
my $ref = $response->parse_response; | |
return unless $ref->{photos}->{total}; | |
return $ref->{photos}->{photo}; | |
} | |
sub collect_photo_urls { | |
my $photos = shift; | |
my $photo_urls = []; | |
foreach my $photo_id ( keys %{$photos} ) { | |
my $url = _get_photo_url($photo_id); | |
push @{$photo_urls}, $url if $url; | |
} | |
return $photo_urls; | |
} | |
sub _get_photo_url { | |
my $photo_id = shift; | |
my $response = $api->get( | |
{ method => 'flickr.photos.getSizes', | |
photo_id => $photo_id, | |
} | |
); | |
my $ref = $response->parse_response; | |
my $url; | |
foreach my $size ( @{ $ref->{sizes}->{size} } ) { | |
if ( $size->{label} eq 'Original' || $size->{label} eq 'Large' ) { | |
$url = $size->{source}; | |
} | |
} | |
say "Collecting Photo urls: " . $url if $url; | |
$url; | |
} | |
sub download_photos { | |
my $photo_urls = shift; | |
my $ua = LWP::UserAgent->new; | |
my $dir = setup_download_dir(); | |
say "### Downloading photos ###"; | |
foreach my $url ( @{$photo_urls} ) { | |
my $file = $dir->file( md5_hex($url) . ".jpg" ); | |
my $res = $ua->mirror( $url, $file->absolute ); | |
say "Download status : " . $res->status_line . " - $url"; | |
} | |
} | |
sub setup_download_dir { | |
my $dir = dir("photos"); | |
$dir->mkpath; | |
$dir; | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment