function _get_nsid($flickr_user, $more = array()) { # TO DO: put this all in a function somewhere and # call/queue it when a user signs up... # As db_main:Users $user = users_get_by_id($flickr_user['user_id']); if (!$user) { return; } $method = 'flickr.people.getInfo'; $args = array('user_id' => $flickr_user['nsid']); $ret = flickr_api_call($method, $args); if (!$ret['ok']) { dumper($args); dumper($ret); return; } $rsp = $ret['rsp']['person']; $path_alias = $rsp['path_alias']; $username = $rsp['username']['_content']; echo "[{$user['id']}] path alias: {$path_alias} screen name: {$username}\n"; if ($path_alias != $flickr_user['path_alias']) { $update = array('path_alias' => $path_alias); $rsp = flickr_users_update_user($flickr_user, $update); echo "[{$user['id']}] update path alias: {$rsp['ok']}\n"; # just let this fail silently if there's a duplicate flickr_users_path_aliases_create($user, $path_alias); } if ($username != $user['username']) { $update = array('username' => $username); $rsp = users_update_user($user, $update); echo "[{$user['id']}] update username: {$rsp['ok']}\n"; } }
function flickr_users_path_aliases_is_available($alias) { if (flickr_users_path_aliases_get_by_alias($alias)) { return 0; } $method = "flickr.urls.lookupUser"; $url = "http://www.flickr.com/photos/{$alias}"; $args = array('url' => $url); $rsp = flickr_api_call($method, $args); return $rsp['ok'] ? 0 : 1; }
function flickr_geobookmarks_import_for_nsid($nsid, $more = array()) { $flickr_user = flickr_users_get_by_nsid($nsid); $user = users_get_by_id($flickr_user['user_id']); if (!$user) { return not_okay("Not a valid user"); } $flickr_user = flickr_users_get_by_user_id($user['id']); $method = 'flickr.people.geoBookmarks.getList'; $args = array('auth_token' => $flickr_user['auth_token']); $rsp = flickr_api_call($method, $args); if (!$rsp['ok']) { return $rsp; } if (!$rsp['rsp']['bookmarks']['count']) { return okay(); } $bookmarks = array(); # mark everything as private for now since none of that stuff # got turned on before I left, sad face... (20120217/straup) $geo_perms = flickr_geo_permissions_map("string keys"); $geo_private = $geo_perms['private']; foreach ($rsp['rsp']['bookmarks']['bookmark'] as $bm) { $bm['user_id'] = $user['id']; $bm['name'] = $bm['label']; $bm['geocontext'] = $bm['context']; $bm['geoperms'] = $geo_private; $bm['woeid'] = 0; unset($bm['label']); unset($bm['pretty_name']); unset($bm['context']); $geo_method = 'flickr.places.findByLatLon'; $geo_args = array('lat' => $bm['latitude'], 'lon' => $bm['longitude'], 'accuracy' => $bm['accuracy']); $geo_rsp = flickr_api_call($geo_method, $geo_args); if ($geo_rsp['ok']) { # I still miss xpath... $bm['woeid'] = $geo_rsp['rsp']['places']['place'][0]['woeid']; } $bookmarks[] = $bm; } $rsp = flickr_geobookmarks_purge_for_user($user); if (!$rsp['ok']) { return $rsp; } $count = 0; foreach ($bookmarks as $bm) { $rsp = flickr_geobookmarks_add($bm); $count += $rsp['ok']; } return okay(array('count_imported' => $count)); }
function flickr_contacts_import_for_nsid($nsid, $more = array()) { $flickr_user = flickr_users_get_by_nsid($nsid); $user = users_get_by_id($flickr_user['user_id']); if (!$user) { return array('ok' => 0, 'error' => 'not a valid user'); } $method = 'flickr.contacts.getList'; $count_contacts = 0; $args = array('auth_token' => $flickr_user['auth_token'], 'per_page' => 100, 'page' => 1); $pages = null; while (!isset($pages) || $pages >= $args['page']) { $rsp = flickr_api_call($method, $args); if (!$rsp) { return array('ok' => 0, 'error' => 'The Flickr API is wigging out...'); } if (!isset($pages)) { $pages = $rsp['rsp']['contacts']['pages']; } $contacts = $rsp['rsp']['contacts']['contact']; if (!is_array($contacts)) { return array('ok' => 0, 'error' => 'The Flickr API did not return any contacts'); } foreach ($contacts as $contact) { $contact_nsid = $contact['nsid']; $contact_username = $contact['username']; $flickr_contact = flickr_users_get_by_nsid($contact_nsid); if (!$flickr_contact) { $password = random_string(32); $user_contact = users_create_user(array("username" => $contact_username, "email" => "{$contact_username}@donotsend-flickr.com", "password" => $password)); # $method = 'flickr.people.getInfo'; $args = array('user_id' => $contact_nsid); $rsp = flickr_api_call($method, $args); $path_alias = $rsp['ok'] ? $rsp['rsp']['person']['path_alias'] : ''; # $flickr_contact = flickr_users_create_user(array('user_id' => $user_contact['id'], 'nsid' => $contact_nsid, 'path_alias' => $path_alias)); } $rel = flickr_contacts_calculate_relationship($contact); # echo "{$contact_username} : {$rel} ({$contact['friend']} {$contact['family']})\n"; $insert = array('user_id' => $user['id'], 'contact_id' => $flickr_contact['user_id'], 'rel' => $rel); $contact = flickr_contacts_add_contact($insert); $count_contacts++; } $args['page'] += 1; } return array('ok' => 1, 'count_imported' => $count_contacts); }
function geo_flickr_get_woeid($woeid) { $cache_key = "flickr_woeid_{$woeid}"; $cache = cache_get($cache_key); if ($cache['ok']) { return $cache['data']; } $args = array('woe_id' => $woeid); $rsp = flickr_api_call('flickr.places.getInfo', $args); if (!$rsp['ok']) { return; } $loc = $rsp['rsp']['place']; cache_set($cache_key, $loc, 'set locally'); return $loc; }
function api_flickr_favorites_remove() { $flickr_user = api_utils_flickr_ensure_token_perms($GLOBALS['cfg']['user'], 'write'); $photo_id = post_int64("photo_id"); if (!$photo_id) { api_output_error(999, "Missing photo ID"); } $method = 'flickr.favorites.remove'; $args = array('photo_id' => $photo_id, 'auth_token' => $flickr_user['auth_token']); $rsp = flickr_api_call($method, $args); # Just ignore if not in faves already... if (!$rsp['ok'] && $rsp['error_code'] != '1') { api_output_error(999, $rsp['error']); } $out = array('photo_id' => $photo_id); api_output_ok($out); }
function flickr_push_unsubscribe($subscription) { $flickr_user = flickr_users_get_by_user_id($subscription['user_id']); $callback = "{$GLOBALS['cfg']['abs_root_url']}push/{$subscription['secret_url']}/"; $method = 'flickr.push.unsubscribe'; $map = flickr_push_topic_map(); $topic = $map[$subscription['topic_id']]; $args = array('auth_token' => $flickr_user['auth_token'], 'topic' => $topic, 'verify' => 'sync', 'verify_token' => $subscription['verify_token'], 'callback' => $callback); if ($topic_args = $subscription['topic_args']) { if (!is_array($topic_args)) { $topic_args = json_decode($topic_args, "as hash"); } $args = array_merge($args, $topic_args); } $rsp = flickr_api_call($method, $args); return $rsp; }
function _flickr_lookup_id_by_url($url, $type) { $cache_key = "flickr_lookup_" . md5($url); $cache = cache_get($cache_key); if ($cache['ok']) { return $cache['data']; } $method = 'flickr.urls.lookup' . ucwords($type); $args = array('url' => $url); $_rsp = flickr_api_call($method, $args); if (!$_rsp['ok']) { return null; } $id = $_rsp['rsp'][$type]['id']; cache_set($cache_key, $id); return $id; }
function flickr_photos_metadata_fetch(&$photo, $inflate = 0) { loadlib("flickr_api"); loadlib("flickr_users"); $flickr_user = flickr_users_get_by_user_id($photo['user_id']); $method = 'flickr.photos.getInfo'; $args = array('photo_id' => $photo['id'], 'auth_token' => $flickr_user['auth_token']); $more = array(); if (!$inflate) { $more['raw'] = 1; } $rsp = flickr_api_call($method, $args, $more); if ($rsp['ok']) { $data = $inflate ? $rsp['rsp'] : $rsp['body']; $rsp = okay(array('data' => $data)); } return $rsp; }
function flickr_faves_import_for_nsid($nsid, $more = array()) { $flickr_user = flickr_users_get_by_nsid($nsid); $user = users_get_by_id($flickr_user['user_id']); if (!$user) { return array('ok' => 0, 'error' => 'not a valid user'); } $method = 'flickr.favorites.getList'; $args = array('user_id' => $flickr_user['nsid'], 'auth_token' => $flickr_user['auth_token'], 'extras' => 'original_format,tags,media,date_upload,date_taken,geo,owner_name', 'per_page' => 100, 'page' => 1); if (isset($more['min_fave_date'])) { $args['min_fave_date'] = $more['min_fave_date']; } $pages = null; $count = 0; while (!isset($pages) || $pages >= $args['page']) { $rsp = flickr_api_call($method, $args); if (!$rsp['ok']) { return $rsp; } if (!isset($pages)) { $pages = $rsp['rsp']['photos']['pages']; } $photos = $rsp['rsp']['photos']['photo']; if (!is_array($photos)) { return array('ok' => 0, 'error' => 'no photos'); } foreach ($photos as $photo) { $ph_rsp = flickr_photos_import_photo($photo); if (!$ph_rsp['ok']) { return $ph_rsp; } $fave_rsp = flickr_faves_add_fave($user, $ph_rsp['photo'], $photo['date_faved']); if ($fave_rsp['ok']) { $count++; } } $args['page'] += 1; } return array('ok' => 1, 'count_imported' => $count); }
include "include/init.php"; loadlib("flickr_api"); # http://www.flickr.com/services/api/flickr.people.getInfo.html # http://www.flickr.com/services/api/misc.buddyicons.html $nsid = get_str("nsid"); if (!$nsid) { error_404(); } $cache_key = "flickr_buddyicon_{$nsid}"; $cache = cache_get($cache_key); if ($cache['ok']) { $buddyicon = $cache['data']; } else { $method = "flickr.people.getInfo"; $args = array("user_id" => $nsid); $rsp = flickr_api_call($method, $args); if (!$rsp['ok']) { error_500(); } $icon_server = $rsp['rsp']['person']['iconserver']; $icon_farm = $rsp['rsp']['person']['iconfarm']; if (!$icon_server) { $buddyicon = "http://www.flickr.com/images/buddyicon.jpg"; } else { $buddyicon = "http://farm{$icon_farm}.static.flickr.com/{$icon_server}/buddyicons/{$nsid}.jpg"; } cache_set($cache_key, $buddyicon); } header("location: {$buddyicon}"); exit;
function _flickr_places_getinfo($woeid) { $method = "flickr.places.getInfo"; $args = array('woe_id' => $woeid); $rsp = flickr_api_call($method, $args); return $rsp; }
function _flickr_photos_import_flickr_meta_urls($photo, $more = array()) { $req = array(); # basic photo info if ($more['auth_token']) { $auth_token = $more['auth_token']; } else { $flickr_user = flickr_users_get_by_user_id($photo['user_id']); $auth_token = $flickr_user['auth_token']; } $method = 'flickr.photos.getInfo'; $args = array('auth_token' => $auth_token, 'photo_id' => $photo['id']); list($url, $args) = flickr_api_call_build($method, $args); $api_call = $url . "?" . http_build_query($args); $req['info'] = $api_call; # fetch comments, which is to say check to see if there # are any new photos worth storing $fetch_comments = 1; if ($more['min_date']) { $method = 'flickr.photos.comments.getList'; $args = array('photo_id' => $photo['id'], 'min_comment_date' => $more['min_date']); $rsp = flickr_api_call($method, $args); if ($rsp['ok'] && !isset($rsp['rsp']['comments']['comment'])) { $fetch_comments = 0; } } if ($fetch_comments) { $method = 'flickr.photos.comments.getList'; $args = array('photo_id' => $photo['id']); list($url, $args) = flickr_api_call_build($method, $args); $api_call = $url . "?" . http_build_query($args); $req['comments'] = $api_call; } return $req; }
function import_flickr_spr_paginate($method, $args, $more = array()) { $defaults = array('root' => 'photos', 'max_photos' => $GLOBALS['cfg']['import_max_records'], 'ensure_geo' => 0); $more = array_merge($defaults, $more); $root = $more['root']; $photos = array(); $count_photos = 0; $to_remove = array('secret', 'server', 'farm', 'isprimary', 'place_id', 'isfriend', 'isfamily', 'ispublic', 'owner', 'geo_is_family', 'geo_is_friend', 'geo_is_contact', 'geo_is_public', 'datetakengranularity'); $page = 1; $pages = null; while (!isset($pages) || $page <= $pages) { # If any sort of geo filter is passed to the API # Flickr will silently set this number to 250 $args['per_page'] = 500; $args['page'] = $page; $_rsp = flickr_api_call($method, $args); if (!$_rsp['ok']) { break; } $rsp = $_rsp['rsp']; if (!isset($pages)) { $pages = $rsp[$root]['pages']; } foreach ($rsp[$root]['photo'] as $ph) { # why didn't we just add a "has_geo" attribute # to the API responses... (20110425/straup) if ($ph['accuracy'] == 0) { continue; } foreach ($to_remove as $key) { if (isset($ph[$key])) { unset($ph[$key]); } } $ph['flickr:id'] = $ph['id']; unset($ph['id']); $ph['description'] = $ph['description']['_content']; $photos[] = $ph; $count_photos += 1; if (isset($more['max_photos']) && $count_photos >= $more['max_photos']) { break; } } if (isset($more['max_photos']) && $count_photos >= $more['max_photos']) { break; } $page += 1; } return $photos; }
function api_flickr_photos_geo_possibleCorrections() { $photo_id = get_int64("photo_id"); $photo = _api_flickr_photos_geo_get_photo($photo_id); $type = get_str("place_type"); if (!$type) { api_output_error(999, "Missing place type"); } if (!flickr_places_is_valid_placetype($type)) { api_output_error(999, "Invalid place type"); } # TO DO: calculate based on $type $radius = 1.5; $bbox = geo_utils_bbox_from_point($photo['latitude'], $photo['longitude'], $radius, 'km'); $bbox = implode(",", array($bbox[1], $bbox[0], $bbox[3], $bbox[2])); $method = 'flickr.places.placesForBoundingBox'; $args = array('bbox' => $bbox, 'place_type' => $type); $rsp = flickr_api_call($method, $args); if (!$rsp['ok']) { api_output_error(999, "Flickr API error"); } $possible = array(); if ($rsp['rsp']['places']['total']) { foreach ($rsp['rsp']['places']['place'] as $place) { $possible[] = array('woeid' => $place['woeid'], 'placetype' => $place['place_type'], 'name' => $place['_content']); } } $parent = flickr_places_parent_placetype($type); $out = array('place_type' => $type, 'parent_place_type' => $parent, 'places' => $possible); return api_output_ok($out); }
function flickr_contacts_import_for_nsid($nsid, $more = array()) { $flickr_user = flickr_users_get_by_nsid($nsid); $user = users_get_by_id($flickr_user['user_id']); if (!$user) { return array('ok' => 0, 'error' => 'not a valid user'); } $method = 'flickr.contacts.getList'; $all_contacts = array(); $count_contacts = 0; $args = array('auth_token' => $flickr_user['auth_token'], 'per_page' => 100, 'page' => 1); $pages = null; while (!isset($pages) || $pages >= $args['page']) { $api_ok = 0; $api_error = ''; # Can I just say this is so profoundly annoying. Why why why # are API calls to a federated database table failing? Anyway. # (20120201/straup) $retries = 0; $max_retries = 10; while (!$api_ok) { $retries += 1; $rsp = flickr_api_call($method, $args); $api_ok = $rsp['ok']; if (!$api_ok) { $api_error = "The Flickr API is wigging out: {$rsp['error']}"; } else { $contacts = $rsp['rsp']['contacts']['contact']; if (!is_array($contacts)) { $api_error = "The Flickr API did not return any contacts"; $api_ok = 0; } } echo "page: {$args['page']}/{$pages} tries: {$retries}/{$max_retries} ok: {$api_ok}\n"; if (!$api_ok) { if ($retries == $max_retries) { return not_okay("Unable to fetch contacts: {$api_error}"); } } } if (!isset($pages)) { $pages = $rsp['rsp']['contacts']['pages']; } foreach ($contacts as $contact) { $contact_nsid = $contact['nsid']; $contact_username = $contact['username']; $flickr_contact = flickr_users_get_by_nsid($contact_nsid); if (!$flickr_contact) { $password = random_string(32); $user_contact = users_create_user(array("username" => $contact_username, "email" => "{$contact_username}@donotsend-flickr.com", "password" => $password)); # $method = 'flickr.people.getInfo'; $args = array('user_id' => $contact_nsid); $rsp = flickr_api_call($method, $args); $path_alias = $rsp['ok'] ? $rsp['rsp']['person']['path_alias'] : ''; # $flickr_contact = flickr_users_create_user(array('user_id' => $user_contact['id'], 'nsid' => $contact_nsid, 'path_alias' => $path_alias)); } $rel = flickr_contacts_calculate_relationship($contact); # echo "{$contact_username} : {$rel} ({$contact['friend']} {$contact['family']})\n"; $insert = array('user_id' => $user['id'], 'contact_id' => $flickr_contact['user_id'], 'rel' => $rel); $all_contacts[] = $insert; } $args['page'] += 1; } if (isset($more['purge_existing_contacts'])) { $rsp = flickr_contacts_purge_contacts($user); if (!$rsp['ok']) { return not_okay("failed to purge existing contacts: {$rsp['error']}"); } } # echo "import " . count($all_contacts) . " contacts\n"; foreach ($all_contacts as $insert) { if (flickr_contacts_add_contact($insert)) { $count_contacts++; } } return array('ok' => 1, 'count_imported' => $count_contacts); }