function __construct($params = array()) { parent::__construct($params); }
$favoritesSuggestions = $db->getAll("SELECT DISTINCT photo_id, nb FROM favorites WHERE photo_id NOT IN (" . $myFavs . ") ORDER BY nb DESC, photo_id LIMIT 0,3"); if ($total > 0) { echo '<ol class="gallery">'; foreach ($favoritesSuggestions as $data) { echo getPhotoHTML($data['photo_id'], $data['nb']); } echo '</ol>'; } ?> <h3>Contacts suggestions</h3> <p>These contacts suggestions are users from which you have favorited the most photos, with a minimum of <?php echo CONTACTS_SUGGESTIONS_TRIGGER; ?> .</p> <?php $flickr = new Flickr_API(array('api_key' => FLICKR_APIKEY)); $contactsListResponse = $flickr->callMethod('flickr.contacts.getPublicList', array('email' => FLICKR_ACCOUNT_EMAIL, 'password' => FLICKR_ACCOUNT_PASSWORD, 'user_id' => FLICKR_USER_NSID, 'per_page' => 1000, 'page' => 1)); $contactsString = ''; if ($contactsListResponse && $contactsListResponse->attributes['stat'] == 'ok') { $contactsList = $contactsListResponse->getNodeAt('contacts'); foreach ($contactsList->children as $contact) { if (isset($contact->attributes['nsid'])) { $contactsString .= ', "' . $contact->attributes['nsid'] . '"'; } } $contactsString = substr($contactsString, 2); } $contactsSuggestionsQuery = "SELECT DISTINCT photographer_nsid, COUNT(photo_id) AS nb FROM favorites WHERE user_nsid = '" . FLICKR_USER_NSID . "' AND photographer_nsid NOT IN (" . $contactsString . ") GROUP BY photographer_nsid HAVING nb >= " . CONTACTS_SUGGESTIONS_TRIGGER . " ORDER BY nb DESC, photographer_nsid LIMIT 0,10"; $contactsSuggestions = $db->getAll($contactsSuggestionsQuery); echo '<ol>'; foreach ($contactsSuggestions as $contactNsid) {
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; }
function getPhoto($photoId) { if (!is_numeric($photoId)) { $photo = 'bad_id: ' . print_r($photoId, true); } else { include_once 'Cache/Lite.php'; $cachePhoto = new Cache_Lite(array('cacheDir' => './cache/photos/', 'lifeTime' => CACHE_LIFETIME_PHOTO, 'automaticCleaningFactor' => 100, 'hashedDirectoryLevel' => 2)); if ($photo = $cachePhoto->get('photo-' . $photoId)) { $photo = unserialize($photo); } else { include_once 'Flickr/API.php'; $flickr = new Flickr_API(array('api_key' => FLICKR_APIKEY)); $response = $flickr->callMethod('flickr.photos.getInfo', array('photo_id' => $photoId)); if ($response) { if ($response->attributes['stat'] == 'ok') { $photo = array('id' => $photoId); $data = $response->getNodeAt('photo/visibility'); $photo['ispublic'] = intval($data->attributes['ispublic']); if ($photo['ispublic']) { $data = $response->getNodeAt('photo'); $server = $data->attributes['server']; $secret = $data->attributes['secret']; $data = $response->getNodeAt('photo/owner'); $photo['owner'] = array(); $photo['owner']['NSID'] = $data->attributes['nsid']; $photo['owner']['username'] = cleanString($data->attributes['username']); $photo['owner']['realname'] = cleanString($data->attributes['realname']); $data = $response->getNodeAt('photo/title'); $photo['title'] = cleanString($data->content); $photo['75x75'] = 'http://static.flickr.com/' . $server . '/' . $photo['id'] . '_' . $secret . '_s.jpg'; $photo['thumbnail'] = 'http://static.flickr.com/' . $server . '/' . $photo['id'] . '_' . $secret . '_t.jpg'; $photo['small'] = 'http://static.flickr.com/' . $server . '/' . $photo['id'] . '_' . $secret . '_m.jpg'; $photo['medium'] = 'http://static.flickr.com/' . $server . '/' . $photo['id'] . '_' . $secret . '.jpg'; $photo['url'] = 'http://www.flickr.com/photos/' . $photo['owner']['NSID'] . '/' . $photo['id'] . '/'; $cachePhoto->save(serialize($photo)); } else { include_once 'inc/database.inc.php'; $GLOBALS['db']->query("DELETE FROM favorites WHERE photo_id='" . $photoId . "'"); $GLOBALS['db']->query("DELETE FROM ignored WHERE photo_id='" . $photoId . "'"); $GLOBALS['db']->query("DELETE FROM photos WHERE photo_id='" . $photoId . "'"); $photo = 'private'; } } else { $errorCode = $flickr->getErrorCode(); $errorMessage = $flickr->getErrorMessage(); if ($errorCode == 1) { include_once 'inc/database.inc.php'; $GLOBALS['db']->query("DELETE FROM favorites WHERE photo_id='" . $photoId . "'"); $GLOBALS['db']->query("DELETE FROM ignored WHERE photo_id='" . $photoId . "'"); $GLOBALS['db']->query("DELETE FROM photos WHERE photo_id='" . $photoId . "'"); $photo = 'removed'; } else { $photo = 'API Error : ' . $errorMessage . '(code ' . $errorCode . ')'; } } } else { $errorCode = $flickr->getErrorCode(); $errorMessage = $flickr->getErrorMessage(); if ($errorCode == 1) { include_once 'inc/database.inc.php'; $GLOBALS['db']->query("DELETE FROM favorites WHERE photo_id='" . $photoId . "'"); $GLOBALS['db']->query("DELETE FROM ignored WHERE photo_id='" . $photoId . "'"); $GLOBALS['db']->query("DELETE FROM photos WHERE photo_id='" . $photoId . "'"); $photo = 'removed'; } else { $photo = 'API Error : ' . $errorMessage . '(code ' . $errorCode . ')'; } } } } return $photo; }