Skip to content

Instantly share code, notes, and snippets.

@plegall
Last active August 29, 2015 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save plegall/4f7b8c99f06ffa4e83a5 to your computer and use it in GitHub Desktop.
Save plegall/4f7b8c99f06ffa4e83a5 to your computer and use it in GitHub Desktop.
piwigo_build_cache.pl
#!/usr/bin/perl -w
# if you have a self-signed SSL certificate, add this environment variable
# export PERL_LWP_SSL_VERIFY_HOSTNAME=0
# (thanks to ticoli on IRC)
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
use LWP::UserAgent;
use JSON;
#-----------------------------------------------------------------------#
# Load configuration #
#-----------------------------------------------------------------------#
my %opt = ();
GetOptions(
\%opt,
qw/
url=s
username=s
password=s
/
);
#-----------------------------------------------------------------------#
# Play zone #
#-----------------------------------------------------------------------#
# exit();
#-----------------------------------------------------------------------#
# Main loop #
#-----------------------------------------------------------------------#
my $base_url = $opt{url};
my $ua = LWP::UserAgent->new;
$ua->cookie_jar({});
$ua->post(
$base_url.'/ws.php?format=json',
{
method => 'pwg.session.login',
username => $opt{username},
password => $opt{password},
}
);
build_cache('medium');
# build_cache('thumb');
# build_cache('square');
exit();
#-----------------------------------------------------------------------#
# Functions #
#-----------------------------------------------------------------------#
sub build_cache {
my ($type) = @_;
# my ($ua, $base_url);
my $response = $ua->post(
$base_url.'/ws.php?format=json',
{
method => 'pwg.getMissingDerivatives',
types => $type,
max_urls => 100_000
}
);
my @urls = @{ from_json($response->content)->{result}{urls} };
print scalar(@urls).' '.$type.' images to build'."\n";
use Time::HiRes qw/gettimeofday tv_interval/;
my $i = 0;
foreach my $url (@urls) {
my $t1 = [gettimeofday];
printf(
'[%s] photo %s %0'.length(scalar(@urls)).'u/%u generation in progress... ',
$url, # $row->{username},
$type,
++$i,
scalar(@urls)
);
$ua->get($url.'&ajaxload=true');
my $elapsed = tv_interval($t1);
printf('%u ms', $elapsed * 1000);
print "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment