Changeset 18600


Ignore:
Timestamp:
Oct 10, 2012, 9:51:51 PM (11 years ago)
Author:
plg
Message:

If a file readme.txt is available in the directory, use it to define album
description.

With option --reload_properties, ability to reload album/photo properties even
if the photos are already uploaded.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/piwigo_import_tree/piwigo_import_tree.pl

    r18577 r18600  
    3232          quiet
    3333          only_write_cache
     34          reload_properties
     35          debug
    3436      /
    3537);
     
    137139
    138140    foreach my $image_href (@{from_json($response->content)->{result}{images}{_content}}) {
    139         $photos_of_album{ $params{album_id} }{ $image_href->{file} } = 1;
     141        $photos_of_album{ $params{album_id} }{ $image_href->{file} } = $image_href->{id};
    140142    }
    141143}
     
    147149
    148150    if (defined $photos_of_album{ $params{album_id} }{ $params{file} }) {
    149         return 1;
     151        return $photos_of_album{ $params{album_id} }{ $params{file} };
    150152    }
    151153    else {
     
    175177}
    176178
     179sub set_album_properties {
     180    my %params = @_;
     181
     182    print '[set_album_properties] for directory "'.$params{dir}.'"'."\n" if $opt{debug};
     183
     184    # avoid to load the readme.txt file 10 times if an album has 10
     185    # sub-albums
     186    our %set_album_properties_done;
     187    if (defined $set_album_properties_done{ $params{id} }) {
     188        print '[set_album_properties] already done'."\n" if $opt{debug};
     189        return;
     190    }
     191    $set_album_properties_done{ $params{id} } = 1;
     192
     193    $params{dir} =~ s{ / }{/}g;
     194
     195    # is there a file "readme.txt" in the directory of the album?
     196    my $desc_filepath = $params{dir}.'/readme.txt';
     197
     198    if (not -f $desc_filepath) {
     199        print "no readme.txt for ".$params{dir}."\n" if $opt{debug};
     200        return;
     201    }
     202
     203    # example of readme.txt:
     204    #
     205    # Title: First public opening
     206    # Date: 2009-09-26
     207    # Copyright: John Connor
     208    #
     209    # Details:
     210    # The first day Croome Court is opened to the public by the National Trust.
     211    # And here is another line for details!
     212
     213    open(IN, '<', $desc_filepath);
     214    my $date_string = undef;
     215    my $is_details = 0;
     216    my $details = '';
     217    while (my $desc_line = <IN>) {
     218        chomp($desc_line);
     219
     220        if ($is_details) {
     221            $details.= $desc_line;
     222        }
     223        elsif ($desc_line =~ /^Date:\s*(.*)$/) {
     224            $date_string = $1;
     225        }
     226        elsif ($desc_line =~ /^Details:/) {
     227            # from now, all the remaining lines are "details"
     228            $is_details = 1;
     229        }
     230    }
     231    close(IN);
     232
     233    if (defined $date_string or $details ne '') {
     234        my $comment = '';
     235
     236        if (defined $date_string) {
     237            $comment = '<span class="albumDate">'.$date_string.'</span><br>'
     238        }
     239        $comment.= $details;
     240
     241        my $form = {
     242            method => 'pwg.categories.setInfo',
     243            category_id => $params{id},
     244            comment => $comment,
     245        };
     246
     247        piwigo_login();
     248
     249        my $response = $ua->post(
     250            $conf{base_url}.'/ws.php?format=json',
     251            $form
     252        );
     253    }
     254}
     255
     256sub set_photo_properties {
     257    my %params = @_;
     258
     259    print '[set_photo_properties] for "'.$params{path}.'"'."\n" if $opt{debug};
     260
     261    # is there any title defined in a descript.ion file?
     262    my $desc_filepath = dirname($params{path}).'/descript.ion';
     263
     264    if (not -f $desc_filepath) {
     265        print '[set_photo_properties] no descript.ion file'."\n" if $opt{debug};
     266        return;
     267    }
     268
     269    my $property = undef;
     270    my $photo_filename = basename($params{path});
     271    open(IN, '<', $desc_filepath);
     272    while (my $desc_line = <IN>) {
     273        if ($desc_line =~ /^$photo_filename/) {
     274            chomp($desc_line);
     275            $property = (split /\t/, $desc_line, 2)[1];
     276        }
     277    }
     278    close(IN);
     279
     280    if (defined $property and $property ne '') {
     281        print '[photo '.$params{id}.'] "'.$params{path}.'", set photo title "'.$property.'"'."\n";
     282
     283        my $form = {
     284            method => 'pwg.images.setInfo',
     285            image_id => $params{id},
     286            single_value_mode => 'replace',
     287            name => $property,
     288        };
     289
     290        piwigo_login();
     291
     292        my $response = $ua->post(
     293            $conf{base_url}.'/ws.php?format=json',
     294            $form
     295        );
     296    }
     297}
     298
    177299sub add_photo {
    178300    my %params = @_;
     
    183305        category => $params{album_id},
    184306    };
    185 
    186     # is there any title defined in a descript.ion file?
    187     my $property = undef;
    188     my $desc_filepath = dirname($params{path}).'/descript.ion';
    189     if (-f $desc_filepath) {
    190         my $photo_filename = basename($params{path});
    191         open(IN, '<', $desc_filepath);
    192         while (my $desc_line = <IN>) {
    193             if ($desc_line =~ /^$photo_filename/) {
    194                 chomp($desc_line);
    195                 $property = (split /\t/, $desc_line, 2)[1];
    196             }
    197         }
    198         close(IN);
    199     }
    200 
    201     if (defined $property and $property ne '') {
    202         $form->{name} = $property;
    203     }
    204307
    205308    print '[album '.$params{album_id}.'] '.$params{path}.' upload starts... ';
     
    216319    my $elapsed = tv_interval($t1);
    217320    print ' upload completed ('.sprintf('%u ms', $elapsed * 1000).')'."\n";
     321
     322    return from_json($response->content)->{result}{image_id};
    218323}
    219324
     
    238343
    239344        foreach my $dir (split '/', $path) {
     345            my $is_new_album = 0;
     346
    240347            if (not defined $piwigo_albums{$up_dir.$dir}) {
    241348                my $id = cached_album(dir => $up_dir.$dir);
     
    245352                if (not defined $id or not grep($_ eq $id, values(%piwigo_albums))) {
    246353                    print 'album "'.$up_dir.$dir.'" must be created'."\n";
     354                    $is_new_album = 1;
    247355                    $id = add_album(name => $dir, parent => $parent_id);
    248356                    cache_add_album(dir => $up_dir.$dir, id => $id);
     
    250358                $piwigo_albums{$up_dir.$dir} = $id;
    251359            }
     360
     361            if ($is_new_album or defined $opt{reload_properties}) {
     362                set_album_properties(dir => $up_dir.$dir, id => $piwigo_albums{$up_dir.$dir});
     363            }
     364
    252365            $parent_id = $piwigo_albums{$up_dir.$dir};
    253366            $up_dir.= $dir.' / ';
     
    264377        my $album_id = $piwigo_albums{$album_key};
    265378
    266         if (photo_exists(album_id => $album_id, file => basename($File::Find::name))) {
     379        my $image_id = photo_exists(album_id => $album_id, file => basename($File::Find::name));
     380        if (not defined $image_id or $image_id < 1) {
     381            $image_id = cached_photo(path => $File::Find::name, dir => $album_key);
     382        }
     383
     384        if (defined $image_id and $image_id >= 1) {
    267385            if (not $opt{quiet}) {
    268386                print $File::Find::name.' already exists in Piwigo, skipped'."\n";
    269387            }
     388
     389            if (defined $opt{reload_properties}) {
     390                set_photo_properties(path => $File::Find::name, id => $image_id);
     391            }
     392
    270393            return 1;
    271394        }
    272395
    273         if (cached_photo(path => $File::Find::name, dir => $album_key)) {
    274             return 1;
    275         }
    276 
    277         if ($opt{only_write_cache}) {
    278             cache_add_photo(path => $File::Find::name, dir => $album_key);
    279             return 1;
    280         }
    281 
    282         add_photo(path => $File::Find::name, album_id => $album_id);
    283         cache_add_photo(path => $File::Find::name, dir => $album_key);
    284         # print 'dirname = '.dirname($path)."\n\n";
     396        $image_id = add_photo(path => $File::Find::name, album_id => $album_id);
     397        set_photo_properties(path => $File::Find::name, id => $image_id);
     398        cache_add_photo(path => $File::Find::name, dir => $album_key, id => $image_id);
    285399    }
    286400}
     
    330444        if ($line =~ m/$photo_filename_md5/) {
    331445            # TODO if needed : search the [id=(\d+)] for photo_id
    332             return 1;
     446            if ($line =~ m/\[id=(\d+)\]/) {
     447                return $1;
     448            }
     449            else {
     450                return -1; # true, but not an image_id
     451            }
    333452        }
    334453    }
Note: See TracChangeset for help on using the changeset viewer.