function updateUsersFromFav($photo_id, $page = 1) { include_once 'Flickr/API.php'; global $db; static $photo_id_prev = null; static $flickr = null; static $lastFav = null; static $num_user = null; static $nb_users = null; static $photographer_nsid = null; if (!is_null($photo_id_prev) && $photo_id_prev != $photo_id) { $lastFav = null; $num_user = null; $nb_users = null; $photographer_nsid = null; } $photo_id_prev = $photo_id; if (is_null($flickr)) { $flickr = new Flickr_API(array('api_key' => FLICKR_APIKEY)); } if (is_null($lastFav)) { // Get last favorite already in database $date_faved = $db->getOne("SELECT date_updated FROM favorites WHERE user_nsid = '" . FLICKR_USER_NSID . "' AND photo_id = '" . $photo_id . "'"); if (is_null($date_faved) || PEAR::isError($date_faved)) { $lastFav = -1; } else { $lastFav = $date_faved; } } if (is_null($num_user)) { $num_user = 0; } if (is_null($nb_users)) { $nb_users = 0; } if (is_null($photographer_nsid)) { $photographer_nsid = $db->getOne("SELECT photographer_nsid FROM favorites WHERE photo_id = '" . $photo_id . "' LIMIT 0,1"); } if (SHOW_DEBUG) { echo '<p><strong>API Request</strong></p><dl><dt>method</dt><dd>flickr.favorites.getPublicList</dd><dt>photo_id<dt><dd>' . $photo_id . '</dd><dt>per_page</dt><dd>50</dd><dt>page</dt><dd>' . $page . '</dd></dl>'; } $response = $flickr->callMethod('flickr.photos.getFavorites', array('email' => FLICKR_ACCOUNT_EMAIL, 'password' => FLICKR_ACCOUNT_PASSWORD, 'photo_id' => $photo_id, 'per_page' => 50, 'page' => $page)); if ($response) { if ($response->attributes['stat'] == 'ok') { $data = $response->getNodeAt('photo'); $pages = intval($data->attributes['pages']); foreach ($data->children as $child) { if ($child->name == 'person') { $num_user++; $user_nsid = $child->attributes['nsid']; $date_faved = $child->attributes['favedate']; if ($date_faved < $lastFav) { $db->query("UPDATE favorites SET date_updated = " . time() . " WHERE user_nsid = '" . FLICKR_USER_NSID . "' AND photo_id = '" . $photo_id . "'"); return $nb_users; } else { if (addUser($user_nsid)) { $nb_users++; } addFav($user_nsid, $photo_id, $photographer_nsid, $date_faved); } } } if ($page < $pages) { updateUsersFromFav($photo_id, $page + 1); } else { $db->query("UPDATE favorites SET date_updated = " . time() . " WHERE user_nsid = '" . FLICKR_USER_NSID . "' AND photo_id = '" . $photo_id . "'"); } } else { if (SHOW_DEBUG) { $errorCode = $flickr->getErrorCode(); $errorMessage = $flickr->getErrorMessage(); echo '<p>Error ' . $errorCode . ': ' . $errorMessage . '</p>'; } } } elseif ($flickr->getErrorCode() == 1) { // Photo not found $db->query("DELETE FROM favorites WHERE photo_id = '" . $photo_id . "'"); $db->query("DELETE FROM ignored WHERE photo_id = '" . $photo_id . "'"); } else { if (SHOW_DEBUG) { echo '<p>Error: No response for flickr.photos.getFavorites with per_page=50 and page=' . $page . '</p>'; echo '<p>HTTP code: ' . $flickr->_http_code . '</p>'; echo '<p>HTTP head: ' . print_r($flickr->_http_head, true) . '</p>'; echo '<p>HTTP body: ' . htmlspecialchars($flickr->_http_body) . '</p>'; $errorCode = $flickr->getErrorCode(); $errorMessage = $flickr->getErrorMessage(); echo '<p>Error ' . $errorCode . ': ' . $errorMessage . '</p>'; var_dump($response); } // We should count errors and remove photo after nth $db->query("UPDATE photos SET date_updated = " . time() . " WHERE photo_id = '" . $photo_id . "'"); return 0; } return $nb_users; }
$nb = updateFavsFromUser(FLICKR_USER_NSID); echo '<p>' . $nb . ' fav' . ($nb > 1 ? 's' : '') . ' added!</p>' . "\n"; flush(); if ($olderUser = $db->getOne("SELECT user_nsid FROM users WHERE user_nsid != '" . FLICKR_USER_NSID . "' AND ignored=0 ORDER BY date_updated LIMIT 0,1")) { echo '<h3>Updating <a href="http://www.flickr.com/photos/' . $olderUser . '/favorites/">' . $olderUser . '\'s favorites</a>...</h3>' . "\n"; flush(); $nb = updateFavsFromUser($olderUser); echo '<p>' . $nb . ' fav' . ($nb > 1 ? 's' : '') . ' added!</p>' . "\n"; flush(); } if ($olderPhoto = $db->getOne("SELECT photo_id FROM favorites WHERE user_nsid = '" . FLICKR_USER_NSID . "' ORDER BY date_updated LIMIT 0,1")) { echo '<h3>Updating users who have favorited ' . $olderPhoto . '...</h3>' . "\n"; flush(); echo '<ol class="gallery">' . getPhotoHTML($olderPhoto) . '</ol>' . "\n"; flush(); $nb = updateUsersFromFav($olderPhoto); echo '<p style="clear: left">' . $nb . ' user' . ($nb > 1 ? 's' : '') . ' added!</p>' . "\n"; flush(); } require 'inc/close.inc.php'; echo '<p>Done in ' . (time() - $begin) . ' seconds.</p>'; if (!SHOW_DEBUG) { ?> <p>This page will self refresh in 10 seconds...</p> <script language="javascript"> setTimeout("window.location.reload()", 1000*10); </script> <?php }