Skip to content

Instantly share code, notes, and snippets.

@hrpunio
Created September 23, 2013 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrpunio/6669079 to your computer and use it in GitHub Desktop.
Save hrpunio/6669079 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Get content of SET or Pool
#
use Flickr::API;
use Compress::Zlib;
use Getopt::Long;
use XML::DOM;
require 'login2flickr.rc';
my $set_id="";
my $pool_id='';
my $method='';
my $ofname='';
my $Photos='';
my $nbrPages = 0;
my $photoIdx = 0;
GetOptions( "set=s" => \$set_id, "pool=s" => \$pool_id, ) ;
if ( $set_id ne "" ) {
$root_ele = 'photoset';
$method = 'flickr.photosets.getPhotos';
$ofname = "set-${set_id}.ph";
print STDERR "*** flickr.photosets.getPhotos for [SET $set_id]\n";
}
elsif ( $pool_id ne "" ) {
$root_ele='photos';
$method = 'flickr.groups.pools.getPhotos';
$ofname = "pool-${pool_id}.ph";
print STDERR "*** flickr.groups.pools.getPhotos for [POOL $pool_id]\n";
} else {
die "*** Podaj id zbioru/pula (set-id/pool-id)\n";
}
## ## ###
my $extras = 'date_taken,views,url_o,url_m,geo,tags';
print STDERR "*** Extra data fetched: $extras\n";
my $api = new Flickr::API({'key' => $api_key, secret => $sharedsecret});
my $parser = XML::DOM::Parser->new();
do
{
my $params = { per_page => 500, page => $nbrPages+1, extras => $extras };
$params->{group_id} = $pool_id if $pool_id;
$params->{photoset_id} = $set_id if $set_id;
##print STDERR "*** Method/params: $method, $params ***\n";
my $response = $api->execute_method($method, $params );
die "Problem: $response->{error_message}\n" if !$response->{success};
## sprawdz czy _content nie jest gzipniety, jezeli to rozpakuj:
my $content_encoding = $response->{_headers}->{'content-encoding'} ;
my $plain_content ;
if ($content_encoding =~ /gzip/ ) {
$plain_content = Compress::Zlib::memGunzip( $response->{_content});
} else { $plain_content = $response->{_content}; }
my $log = $parser->parse($plain_content);
for $ps ( $log->getElementsByTagName( $root_ele ) ) {
if ($ps->getAttributeNode ("page") ) { $page = $ps->getAttributeNode ("page")->getValue(); }
if ($ps->getAttributeNode ("pages") ) { $pages = $ps->getAttributeNode ("pages")->getValue(); }
}
print "*** Page $page of $pages ***\n";
## dla każdego elementu:
for $p_ ( $log->getElementsByTagName('photo') ) {
$element_content='';
$numberAttributes = 0;
## zapisz wszystkie atrybuty
if ($p_ ->getAttributes() !=null){
$numberAttributes = $p_->getAttributes()->getLength();
}
for ($loopIndex =0; $loopIndex < $numberAttributes; $loopIndex++) {
$attribute = ($p_ -> getAttributes())->item($loopIndex);
# http://stackoverflow.com/questions/5387116/parsing-element-includng-attribute-and-text-nodes-of-xml-document-using-perl
##if($attribute->getNodeName() eq "title"){
##$title = $attribute->getNodeValue();
$attrname = $attribute->getNodeName();
$attrvalue = $attribute->getNodeValue();
$element_content .= "'$attrname' => '$attrvalue', ";
}
$Photos .= "{ $element_content },\n";
$photoIdx++;
} ##//
++$nbrPages;
} while ($page < $pages );
## ## ###
print STDERR "Writing to $ofname\n";
open (OFILE, ">$ofname");
print OFILE "## Created with $method\n\@photos = (\n $Photos \n);\n1;\n";
close OFILE;
print STDERR "*** $photoIdx photos written to $ofname ***\n";
##//ENDE//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment