Announcement

#1 2013-05-04 00:27:17

tip2tail
Member
2012-06-23
5

WebAPI Latest 10 images - Order and Limit

Hello,

Just starting out with the WebAPI.  I'm looking to get the details of the last 10 images uploaded to the Piwigo installation.  Is this possible?

I am running my own installation of Piwigo on my own server.  Photos are uploaded by pLoader.

Can anyone assist?

tip2tail

Offline

 

#2 2016-05-04 10:24:39

eliz82
Member
Romania
2016-04-27
281

Re: WebAPI Latest 10 images - Order and Limit

i think pwg.categories.getImages is the function you want

http://yoursite.com/piwigo/ws.php?forma … _available

the id need to empty if you want the gallery root, you specify 10 per page, sort by date_available . you will get an xml list with the last 10 photos uploaded in your gallery.
you just need to parse that xml with php, store what are you interested in an array, then show the results from the array.

Offline

 

#3 2016-05-04 22:14:21

eliz82
Member
Romania
2016-04-27
281

Re: WebAPI Latest 10 images - Order and Limit

i had some free time and made a little php script to show last 10 images uploaded to a piwigo gallery. maybe it will help someone, as probably they are many people who want to show their gallery images to other php software.

menalto gallery2 had a very simple way of doing this using an extension called imageblock.
http://codex.galleryproject.org/Gallery … al_Options

in gallery2 just use php function fopen or curl a link like this:

Code:

http://example.com/gallery2/main.php?g2_view=imageblock.External&g2_blocks=recentImage|randomImage&g2_itemId=7

maybe someone will write something similar for piwigo to make the users life more easy.

for piwigo the code is more complicated. i used xml output from piwigo web api because is more humanly readable so is more appropriate for beginners:

Code:

<?php
$objXml = new SimpleXmlElement(file_get_contents("http://piwigo.org/demo/ws.php?format=rest&method=pwg.categories.getImages&per_page=10"));
foreach($objXml->images->image as $value) {
  echo '<a href="' . $value->categories->category->attributes()->page_url . '">' . '<img src="' . $value->derivatives->square->url . '"></a>' . PHP_EOL;
}
?>

DEMO (and the code is more expanded and explicated): http://phpfiddle.org/lite/code/h54b-7u04
just press Run button to run the code

p.s.   for this code to work simplexml PHP module must be activated, and fopen must be enabled in php.ini . otherwise you can use curl to open the xml, and another method (like DOM) to loop it. http://www.w3schools.com/php/php_xml_dom.asp

Last edited by eliz82 (2016-05-05 13:53:59)

Offline

 

#4 2016-05-05 00:22:12

teekay
Member
2013-06-12
427

Re: WebAPI Latest 10 images - Order and Limit

I'll add one using jQuery:

Code:

<div class="last10images">
</div>

<script type="text/javascript">
var pwg_api = "/ws.php?format=json&method=pwg.categories.getImages&per_page=10";
$.getJSON(pwg_api, function (json) {
    var images = json.result.images;

    $.each( images, function ( i, image ) {
       var src = image.derivatives.square.url;
       var name = image.name;
       $('.last10images').append('<img src="' + src + '" alt="' + name + '"></img>');
    });
});
</script>

The only dependency (besides jQuery of course) is CORS, means the Piwigo Webserver has to set an Access-Control-Allow-Origin header. The piwigo.org demo server doesn't, so a jsfiddle won't work.

I tested above in an AdditionalPages page.

Offline

 

#5 2016-05-05 07:17:13

eliz82
Member
Romania
2016-04-27
281

Re: WebAPI Latest 10 images - Order and Limit

very nice.
seems it's easy to enable CORS on Apache. http://enable-cors.org/server_apache.html . just put a line in piwigo .htaccess file .

Offline

 

#6 2016-05-05 10:21:17

teekay
Member
2013-06-12
427

Re: WebAPI Latest 10 images - Order and Limit

Hmmm, just realized: the API method spits out the oldest images, or some random ones, but not the latest.

Found this: http://piwigo.org/forum/viewtopic.php?id=25841 - never made it into an issue on github, though.

But the trick there works: pwg.categories.getImages&per_page=10&recursive=true&order=date_available%20desc

Last edited by teekay (2016-05-05 10:28:57)

Offline

 

#7 2016-05-05 16:50:05

eliz82
Member
Romania
2016-04-27
281

Re: WebAPI Latest 10 images - Order and Limit

teekay wrote:

the API method spits out the oldest images, or some random ones, but not the latest.
Found this: http://piwigo.org/forum/viewtopic.php?id=25841 - never made it into an issue on github, though.

As you can see in my demo code it's showing the last 10 uploaded photos from the root page without specify recursive or order. Also I tested on my gallery installation and results are the same.

arent you seeing in the xml code the last 10
http://piwigo.org/demo/ws.php?format=re … er_page=10
that you see in the gallery?
http://piwigo.org/demo/index.php?/recent_pics

Maybe is something in gallery config, or you have a different gallery version, or a plugin that interfere with the output.

Offline

 

#8 2016-05-05 18:51:25

teekay
Member
2013-06-12
427

Re: WebAPI Latest 10 images - Order and Limit

Interesting. You're right, the data on piwigo.org/demo looks the same with or without &order=date_available%20desc. On my own piwigo instance, I get totally different results.

EDIT: I found out why this happens. The API method takes into account the default sort order you have configured in Configuration -> Options -> Common. If you set "Date created: Old -> New", the API returns oldest created first, etc..

The Piwigo demo site seems to be set to "date published: New -> Old", which is why it returns the most recent 10 pictures by default. Mine isn't.

Setting &order= in the API call overrides this. And that little SQL-Injection-like hack to append %20desc makes it possible to have the selected order descending (%20 being a url-escaped space, results in "order by date_available desc" SQL statement in the method)

Last edited by teekay (2016-05-05 19:23:23)

Offline

 

#9 2016-05-05 20:44:47

eliz82
Member
Romania
2016-04-27
281

Re: WebAPI Latest 10 images - Order and Limit

teekay wrote:

I found out why this happens. The API method takes into account the default sort order you have configured in Configuration -> Options -> Common. If you set "Date created: Old -> New", the API returns oldest created first, etc.

This kind of things should be written somewhere in a API documentation.
http://piwigo.org/doc/doku.php?id=dev:webapi:start

Offline

 

#10 2016-05-11 12:26:42

eliz82
Member
Romania
2016-04-27
281

Re: WebAPI Latest 10 images - Order and Limit

teekay wrote:

EDIT: I found out why this happens. The API method takes into account the default sort order you have configured in Configuration -> Options -> Common. If you set "Date created: Old -> New", the API returns oldest created first, etc..
The Piwigo demo site seems to be set to "date published: New -> Old", which is why it returns the most recent 10 pictures by default. Mine isn't.
Setting &order= in the API call overrides this.

I have found a workaround, that retrieve the last 10 pictures no mater what default sort order is set by admin in gallery administration.

PLG already made something similar for categories (albums):
http://piwigo.org/forum/viewtopic.php?p … 34#p158834
But "pwg.categories.getImages" &per_page is limited to 500, so you do not get a "true" recursive mode.  Also "pwg.categories.getList" don't have "&order" query, thats why you need to parse entire output and get the dates from it (like PLG does).

So my logic was to calculate the last page of "pwg.categories.getImages&order=date_available" and use the "&page" parameter to get the last page.
Steps are:
- calculate the total number of pictures in the gallery using "pwg.categories.getList" api call (as "pwg.getInfos" is Admin only);
- because "total number of pictures/pictures wanted" can be float, we can end by not having 10 pictures on the last page but only 1;
- so we request both last and the previous pages;
- put bot responses and array and merge the two arrays;
- reverse the array (now we have Descending order);
- slice the array to the number of pictures wanted;
- loop and show elements from the array;

DEMO CODE: http://phpfiddle.org/lite/code/4fv8-5jgq

Code:

<?php
$per_page = 10; //CONFIG: number of latest pictures you want to show
$site = "http://piwigo.org/demo/ws.php?format=php&method="; //CONFIG: your site Api address

$resp_arr_cat = unserialize(file_get_contents($site . "pwg.categories.getList&public=true")); //api request to count the pictures
$count_images = 0;
foreach($resp_arr_cat["result"]["categories"] as $value) { //loop to get the total number of pictures
  $count_images += $value["total_nb_images"]; 
}
$pages = intval($count_images/$per_page); //total number of pages
echo '<p>Calculated total images: ' . $count_images . ' => Calculated number of pages: ' . $pages . ' with ' . $per_page . ' pictures per page</p>';

$get_api_img_last = $site . "pwg.categories.getImages&recursive=true&order=date_available&per_page=" . $per_page . "&page=" . $pages;
$get_api_img_pre = $site . "pwg.categories.getImages&recursive=true&order=date_available&per_page=" . $per_page . "&page=" . ($pages-1);
echo '<p>The calculated Web Api request is: <br><strong>' . $get_api_img_last . '<br>+<br>' . $get_api_img_pre . '</strong></p>';

$resp_arr_img_last = unserialize(file_get_contents($get_api_img_last)); //get the last page and put to array
$resp_arr_img_pre = unserialize(file_get_contents($get_api_img_pre)); //get the previous page and put to array

$resp_arr_img = array_merge_recursive($resp_arr_img_pre, $resp_arr_img_last); //merge the two arrays
$resp_arr_img_rev = array_slice(array_reverse($resp_arr_img["result"]["images"]), 0, $per_page); //reverse the array to have Descending order, then slice it

foreach ($resp_arr_img_rev as $value) {
    echo '<a target="_blank" href="' . $value["categories"]["0"]["page_url"] . '"><img src="' . $value["derivatives"]["square"]["url"] . '" /></a>' . PHP_EOL;
}
?>

Last edited by eliz82 (2016-05-14 08:00:28)

Offline

 

#11 2016-05-21 15:34:28

eliz82
Member
Romania
2016-04-27
281

Re: WebAPI Latest 10 images - Order and Limit

Seems you can use "&order=date_available%20desc" in the web api query, to get the images in descending order. Even if you don't make any changes in the Piwigo files (at least in the Piwigo 2.7.4 and 2.8.1)
So forget of my complicated method from the previous post.

I lost like hours working on a piwigo extension, trying all kind of strange methods to return the images in a descending order.
And then I looked at the PiwigoPress code and ... surprise.

Good job in not specifying this anywhere, in the ws.htm (even that specific field has an info button) and neither in the wiki documentation.

And another surprise. I observed the maximum retrieved pictures by web api can be 500. I first believed this is hardcoded in the Piwigo code. To discover last night that you can set that option in config.inc.php

Code:

// Maximum number of images to be returned foreach call to the web service
$conf['ws_max_images_per_page'] = 500;

You should really write this things in the Api wiki.
http://piwigo.org/doc/doku.php?id=dev:webapi:start
You are letting us to learn in the hard way how to work with piwigo web api.

Last edited by eliz82 (2016-05-21 20:07:17)

Offline

 

#12 2016-05-21 18:40:22

teekay
Member
2013-06-12
427

Re: WebAPI Latest 10 images - Order and Limit

Yes it works without changing any Piwigo code. It should have mentioned this in my comment #6 above.

Offline

 

#13 2016-05-21 19:54:57

eliz82
Member
Romania
2016-04-27
281

Re: WebAPI Latest 10 images - Order and Limit

No, in the comment 6 you have putted a link to a guy post that patched the core files of Piwigo 2.7.4 creating a solution with %20desc to work.
Asking users of my extension to modify Piwigo core files was a solution unacceptable for me. So i just lost a lot of time with writing stupid workaround code.
To see later that PiwigoPress is using simply "%20desc" and it works on my 2.7.4 without any changes to core files.

So somebody applied that patch to Piwigo 2.7.4 without releasing a new version ?

Or that %20desc method was already working without that guy modification of the core files?

Offline

 

#14 2016-05-21 20:08:12

teekay
Member
2013-06-12
427

Re: WebAPI Latest 10 images - Order and Limit

eliz82 wrote:

No, in the comment 6 you have putted a link to a guy post that patched the core files of Piwigo 2.7.4 creating a solution with %20desc to work.

Seems you just misread his post. To quote in my own words: he initially worked around it by using %20desc, but says it would be good to have it as feature, so he included a patch to implement it as a parameter for the API method. That patch was what I meant with "it never made it into an issue on github". Nobody cared, that feature never got implemented, but %20desc still works as it did before.

Wasting time sucks, really. You seem to have some good knowledge of PHP (i don't, not at all), so why not take that patch, make it work against current master and get a pull request out on github? :)

Offline

 

#15 2016-05-21 21:04:21

eliz82
Member
Romania
2016-04-27
281

Re: WebAPI Latest 10 images - Order and Limit

teekay wrote:

he initially worked around it by using %20desc, but says it would be good to have it as feature, so he included a patch to implement it as a parameter for the API method. That patch was what I meant with "it never made it into an issue on github".

Then indeed seems i misread his post. Making another query parameter is the way this should work. &sort=asc or &sort=desc
But this is not an excuse to the lack of documentation that Piwigo Api has. Even worse there is an info button to that "&order" that give you the illusion that those parameters are all that you can use. Better that info button didn't existed at all. In that way I would probably ask on the forums how to use or have a look to PiwigoPress or PiwigoMedia code from the start.

teekay wrote:

You seem to have some good knowledge of PHP (i don't, not at all), so why not take that patch, make it work against current master and get a pull request out on github? :)

At the moment i'm making tests with Piwigo to try to switch from my PhpBB3+Menalto Gallery2 with 100 photographers community to PhpBB3+Piwigo.
At the results are not good. Seems they are bugs in Piwigo external authentication+community plugin. And also the community plugin seems to be very basic.
So i cannot invest to much time in Piwigo until the bugs are fixed and more futures are added to make it suitable for my community.

Offline

 

Board footer

Powered by FluxBB

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