开发者

Flickr API returns inconsistent number of photos when using multiple tags

I'm trying to use my Flickr account as a 'host' for an image gallery. I've tagged 251 photos with a common tag 'golftournament' and each one with the year and any players in the photo.

So, for example, three random photos may have the following tags:

golftournament dan steve 2005 (dan and steve in this photo from 2006)

golftournament 2006 (no players in this photo from 2006)

golftournament 2008 paul dan (paul and dan in this photo from 2008)

When I make an API call, it returns an inconsistent total number of photos if I set the 'tags' part of the API call to tags=golftournament,dan,2005 and the tag_mode to tagmode=all.

Sometimes I get 13 photos in the result, sometimes I get 12 photos and sometimes I actually get the correct number of photos (14)

I'm using a PHP library, but that's irrelevant because I see the same results in the Flickr API Explorer: http://www.flickr.com/services/api/explore/?method=flickr.photos.search)

Is ther开发者_如何转开发e any reason why the Flickr API is so inconsistent in this regard?

Cheers,

Dan


Just to update on this, in the end I simply got all photos from a photoset and then built two arrays of years and players from the tags. Finally, I used what the request was to return the correct photographs. I'm using the excellent phpFlickr library and here's my entire API script:

<?php
// Initialise Flickr API library and authentication
require_once("phpFlickr.php");
$f = new phpFlickr('<api key>', '<secret>');
$f->setToken('<token>');

// Get all photos from the photoset
$page = 0;
$perpage = 500;
$all = array('photos' => array(), 'total' => 0);
while ($perpage * $page < $all['total'] || $all['total'] == 0) {
    $p = $f->photosets_getPhotos('<photoset number>', 'tags', NULL, $perpage, $page + 1);
    $all['total'] = (integer) $p['photoset']['total'];
    $all['photos'] += $p['photoset']['photo'];
    $page++;
}

// Get all the available years/players from the tags
$years = array();
$players = array();
foreach ($all['photos'] as $key => $photo) {
    $photo_tags = explode(' ', $photo['tags']);
    $all['photos'][$key]['tags'] = $photo_tags;
    foreach ($photo_tags as $tag) {
        if (preg_match('/^[0-9]{4}$/', $tag)) {
            if (!in_array($tag, $years)) {
                $years[] = $tag;
            }
        } else {
            if (!in_array($tag, $players)) {
                $players[] = $tag;
            }
        }
    }
}
rsort($years);
sort($players);

// Set year/player tags if set
$tags = array();
if (isset($_GET['year']) && in_array($_GET['year'], $years)) {
    $tags['year'] = $_GET['year'];
}
if (isset($_GET['player']) && in_array($_GET['player'], $players)) {
    $tags['player'] = $_GET['player'];
}

// Build output array and filter by year/person
$output = array(
    'years'     => $years,
    'players'   => $players,
    'photos'    => array()
);
foreach ($all['photos'] as $key => $photo) {
    if (!isset($tags['year']) || in_array($tags['year'], $photo['tags'])) {
        if (!isset($tags['player']) || in_array($tags['player'], $photo['tags'])) {
            $output['photos'][] = array(
                'thumbnail' => $f->buildPhotoURL($photo, 'Square'),
                'image'     => $f->buildPhotoURL($photo, 'Large'),
                'tags'      => implode(' ', $photo['tags'])
            );
        }
    }
}
$output['totals']['total'] = count($output['photos']);

// Calculate paging
$output['perpages'] = array(32, 64, 128, 256);
if (isset($_GET['perpage']) && in_array($_GET['perpage'], $output['perpages'])) {
    $output['perpage'] = $_GET['perpage'];
} else {
    $output['perpage'] = $output['perpages'][0];
}
if ($output['totals']['total'] <= $output['perpage']) {
    $output['pages'] = 1;
} else {
    $output['pages'] = ceil($output['totals']['total'] / $output['perpage']);
}
if (isset($_GET['page']) && intval($_GET['page']) > 0 && intval($_GET['page']) <= $output['pages']) {
    $page = $_GET['page'];
} else {
    $page = 1;
}
$output['totals']['recordstart'] = (($page == 1) ? 0 : (($page - 1) * $output['perpage']));
$output['totals']['recordend'] = ($output['totals']['recordstart'] + $output['perpage']) - 1;
if ($output['totals']['recordend'] > ($output['totals']['total'] - 1)) {
    $output['totals']['recordend'] = $output['totals']['total'] - 1;
}
$photos = array();
foreach ($output['photos'] as $index => $photo) {
    if ($index >= $output['totals']['recordstart'] && $index <= $output['totals']['recordend']) {
        $photos[] = $photo;
    }
}
unset($output['photos']);
$output['photos'] = $photos;

ob_start("ob_gzhandler");
echo json_encode($output);

Hope someone finds this useful!

Dan


Apparently the flickr search functionality is bugged at the moment and flickr is working on a fix.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜