Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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