Ignore:
Timestamp:
Jun 15, 2012, 3:52:59 PM (12 years ago)
Author:
plg
Message:

make sure piwigo_import_tree.pl is always correctly connected to Piwigo before any other action on the API

fix indentation issues

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/piwigo_import_tree/piwigo_import_tree.pl

    r15730 r15731  
    5858
    5959# Login to Piwigo
    60 my $form = {
    61     method => 'pwg.session.login',
    62     username => $conf{username},
    63     password => $conf{password},
    64 };
    65 
    66 $result = $ua->post(
    67     $conf{base_url}.'/ws.php?format=json',
    68     $form
    69 );
     60piwigo_login();
    7061
    7162# Fill an "album path to album id" cache
     
    7566    $conf{base_url}.'/ws.php?format=json',
    7667    {
    77                 method => 'pwg.categories.getList',
    78                 recursive => 1,
    79                 fullname => 1,
    80         }
     68        method => 'pwg.categories.getList',
     69        recursive => 1,
     70        fullname => 1,
     71    }
    8172);
    8273
    8374my $albums_aref = from_json($response->content)->{result}->{categories};
    8475foreach my $album_href (@{$albums_aref}) {
    85         $piwigo_albums{ $album_href->{name} } = $album_href->{id};
     76    $piwigo_albums{ $album_href->{name} } = $album_href->{id};
    8677}
    8778# print Dumper(\%piwigo_albums)."\n\n";
    8879
    8980if (defined $opt{parent_album_id}) {
    90         foreach my $album_path (keys %piwigo_albums) {
    91                 if ($piwigo_albums{$album_path} == $opt{parent_album_id}) {
    92                         $conf{parent_album_id} = $opt{parent_album_id};
    93                         $conf{parent_album_path} = $album_path;
    94                 }
    95         }       
    96         if (not defined $conf{parent_album_path}) {
    97                 print "Parent album ".$opt{parent_album_id}." does not exist\n";
    98                 exit();
    99         }
     81    foreach my $album_path (keys %piwigo_albums) {
     82        if ($piwigo_albums{$album_path} == $opt{parent_album_id}) {
     83            $conf{parent_album_id} = $opt{parent_album_id};
     84            $conf{parent_album_path} = $album_path;
     85        }
     86    }
     87
     88    if (not defined $conf{parent_album_path}) {
     89        print "Parent album ".$opt{parent_album_id}." does not exist\n";
     90        exit();
     91    }
    10092}
    10193
     
    110102#---------------------------------------------------------------------
    111103
     104sub piwigo_login {
     105    $ua->post(
     106        $conf{base_url}.'/ws.php?format=json',
     107        {
     108            method => 'pwg.session.login',
     109            username => $conf{username},
     110            password => $conf{password},
     111        }
     112    );
     113}
     114
    112115sub fill_photos_of_album {
    113         my %params = @_;
    114        
    115         if (defined $photos_of_album{ $params{album_id} }) {
    116                 return 1;
    117         }
    118        
    119         my $response = $ua->post(
     116    my %params = @_;
     117
     118    if (defined $photos_of_album{ $params{album_id} }) {
     119        return 1;
     120    }
     121
     122    piwigo_login();
     123    my $response = $ua->post(
    120124        $conf{base_url}.'/ws.php?format=json',
    121125        {
     
    124128        }
    125129    );
    126    
     130
    127131    foreach my $image_href (@{from_json($response->content)->{result}{images}{_content}}) {
    128132        $photos_of_album{ $params{album_id} }{ $image_href->{file} } = 1;
     
    131135
    132136sub photo_exists {
    133         my %params = @_;
     137    my %params = @_;
    134138
    135139    fill_photos_of_album(album_id => $params{album_id});
    136    
     140
    137141    if (defined $photos_of_album{ $params{album_id} }{ $params{file} }) {
    138142        return 1;
     
    144148
    145149sub add_album {
    146         my %params = @_;
    147        
    148         # print Dumper(\%params);
    149        
     150    my %params = @_;
     151
    150152    my $form = {
    151153        method => 'pwg.categories.add',
    152154        name => $params{name},
    153155    };
    154    
     156
    155157    if (defined $params{parent}) {
    156158        $form->{parent} = $params{parent};
    157159    }
    158160
     161    piwigo_login();
    159162    my $response = $ua->post(
    160163        $conf{base_url}.'/ws.php?format=json',
     
    193196    }
    194197
     198    piwigo_login();
    195199    my $response = $ua->post(
    196200        $conf{base_url}.'/ws.php?format=json',
Note: See TracChangeset for help on using the changeset viewer.