Announcement

#1 2022-07-19 12:24:09

iconberg
Member
2016-02-24
20

How duplicate an album with subalbums

Hello,

we have an album "2021" with subablums with product pictures for 2021.

Now we want to create a duplicate album "2022" of the album "2021" album tree,
like we can do with "Associate with album" for a single subalbum in "Manage album photos".

So we can afterwards update the new "2022" with new pictures and remove old linked ones if they are obsolete.

Without doing it for all 506 subalbums,
is there  way to do this for a parent album?
An sql will be a solution too.


sincerly

Offline

 

#2 2022-07-20 12:00:48

iconberg
Member
2016-02-24
20

Re: How duplicate an album with subalbums

I found there is a piwigo api and also a piwigo modul for easy usage with python.
The api is great.

Now i have a script that duplicates an album with subalbums and collect every image for each album to associate with the duplicate album.

But i cant find the api call to associate an image with an album, where is it?

Code:

USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK
USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK
#USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK

from piwigo import Piwigo
import itertools
from pprint import pprint


#change to your site and creditials
mysite = Piwigo('https://xxxxxxxxxxxxxxxxxx')
mysite.pwg.session.login(username="xxxxxxxxx", password="xxxxxxxxxxxxx")

#select top album for associated copy
#select destination top album to copy subalbums in (must already exists)
top_source_album_name = '2021'
top_dest_album_name = '2022'


def get_album_childs(cat_id=0):
    return [ album for album in mysite.pwg.categories.getList(cat_id=cat_id).get('categories')
               if album['id'] != cat_id ]


def album_exists(albumname, parent_id):
    return albumname in [ album['name'] for album in get_album_childs(parent_id) ]


def create_album(albumname, parent_id):
    return mysite.pwg.categories.add(name=albumname, parent=parent_id)
    

def get_images(source_id):
    images = []
    for page in itertools.count(0):
        data = mysite.pwg.categories.getImages(cat_id=source_id,
                                               per_page=100,
                                               page=page,
                                               recursive=False)
        page_images = [ i['id'] for i in data.get('images') ]
        if len(page_images) > 0:
            images.append(page_images)
        else:
            return images
    

def duplicate_album_branch(source_id, dest_id, level=1):
    levelstr = '   ' * level
    print(f"{'---' * level}Duplicate {source_id} -> {dest_id}")
    albums = get_album_childs(source_id)    

    for album in albums:
        new_album = album['name']
        new_source_id = album['id']

        if album_exists(new_album, dest_id):
            print(f"{levelstr}{new_album}' already exists")
        else:
            r = create_album(new_album, dest_id)
            print(f"{levelstr}{new_album} ({r['id']})")

            duplicate_album_branch(new_source_id, r['id'], level+1)


albums = get_album_childs()
album = [ album for album in albums if album['name'] == top_source_album_name ][0]
dest_album = [ album for album in albums if album['name'] == top_dest_album_name ][0]

print('Associated copy')
print(f"From: {album['name']} ({album['id']})")
print('Sub albums:', album['nb_categories'])
print(f"To: {dest_album['name']} ({dest_album['id']})")
print()

duplicate_album_branch(album['id'], dest_album['id'])

Offline

 

#3 2022-07-20 13:27:55

erAck
Only trying to help
2015-09-06
2035

Re: How duplicate an album with subalbums

pwg.images.setInfo with parameters categories="category_id" and multiple_value_mode="append"; see your Piwigo's tools/ws.htm


Running Piwigo at https://erack.net/gallery/

Offline

 

#4 2022-07-20 13:39:42

iconberg
Member
2016-02-24
20

Re: How duplicate an album with subalbums

Thank you :)

Offline

 

#5 2022-07-21 13:41:38

iconberg
Member
2016-02-24
20

Re: How duplicate an album with subalbums

Here is my script if somebody has the same need for it.
Its a bit slow when it comes do associate the images.

Code:

USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK
USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK
#USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK USE AT YOUR OWN RISK

from piwigo import Piwigo
import itertools
from pprint import pprint


#change to your site and creditials
mysite = Piwigo('https://xxxxxxxxxxxxxxxxxxxxxxxxx')
mysite.pwg.session.login(username="xxxxxxxxxxx", password="xxxxxxxxx")

#select top album for associated copy
#select destination top album to copy subalbums in (must already exists)
top_source_album_name = '2021'
top_dest_album_name = '2022'


def get_album_childs(cat_id=0):
    return [ album for album in mysite.pwg.categories.getList(cat_id=cat_id).get('categories')
               if album['id'] != cat_id ]


def album_exists(albumname, parent_id):
    return albumname in [ album['name'] for album in get_album_childs(parent_id) ]


def create_album(albumname, parent_id):
    return mysite.pwg.categories.add(name=albumname, parent=parent_id)
    

def get_images(source_id):
    images = []
    for page in itertools.count(0):
        data = mysite.pwg.categories.getImages(cat_id=source_id,
                                               per_page=100,
                                               page=page,
                                               recursive=False)
        page_images = [ i['id'] for i in data.get('images') ]
        if len(page_images) > 0:
            images.extend(page_images)
        else:
            return images


def associate_images(images, dest_id):
    for image in set(images):
        r = mysite.pwg.images.setInfo(image_id=image,
                                      categories=dest_id,
                                      multiple_value_mode="append")
    

def duplicate_album_branch(source_id, dest_id, level=1):
    levelstr = '   ' * level
    print(f"{'---' * level}Duplicate {source_id} -> {dest_id}")
    albums = get_album_childs(source_id)    

    for album in albums:
        new_album = album['name']
        new_source_id = album['id']

        if album_exists(new_album, dest_id):
            print(f"{levelstr}{new_album}' already exists")
        else:
            r = create_album(new_album, dest_id)
            print(f"{levelstr}{new_album} ({r['id']})")
            images = get_images(new_source_id)
            print(f"{'   ' * level}Image count {len(images)}")
            associate_images(images, r['id'])

            duplicate_album_branch(new_source_id, r['id'], level+1)


albums = get_album_childs()
album = [ album for album in albums if album['name'] == top_source_album_name ][0]
dest_album = [ album for album in albums if album['name'] == top_dest_album_name ][0]

print('Associated copy')
print(f"From: {album['name']} ({album['id']})")
print('Sub albums:', album['nb_categories'])
print(f"To: {dest_album['name']} ({dest_album['id']})")
print()

duplicate_album_branch(album['id'], dest_album['id'])

Offline

 

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact