Permalink
Browse files

add hooks to backup geobookmarks

  • Loading branch information...
1 parent 992eb1e commit d953d4b3570d4ba5bfa148c4beabb78a08dd97e0 straup committed Feb 17, 2012
View
23 bin/backup_geobookmarks.php
@@ -0,0 +1,23 @@
+<?php
+
+ $root = dirname(dirname(__FILE__));
+ ini_set("include_path", "{$root}/www:{$root}/www/include");
+
+ #
+
+ include("include/init.php");
+ loadlib("flickr_backups");
+
+ if (! $GLOBALS['cfg']['enable_feature_backups']){
+ echo "backups are currently disabled\n";
+ exit();
+ }
+
+ foreach (flickr_backups_users() as $user){
+ echo "backup contacts for {$user['username']}\n";
+ $rsp = flickr_backups_get_geobookmarks($user);
+ dumper($rsp);
+ }
+
+ exit();
+?>
View
36 bin/backup_register_all.php
@@ -0,0 +1,36 @@
+<?php
+
+ $root = dirname(dirname(__FILE__));
+ ini_set("include_path", "{$root}/www:{$root}/www/include");
+
+ set_time_limit(0);
+
+ #
+
+ include("include/init.php");
+ loadlib("flickr_backups");
+
+ if (! $GLOBALS['cfg']['enable_feature_backups']){
+ echo "backups are currently disabled\n";
+ exit();
+ }
+
+ $map = flickr_backups_type_map("string keys");
+
+ foreach (flickr_backups_users() as $user){
+
+ $backups = flickr_backups_for_user($user);
+
+ foreach ($map as $label => $type_id){
+
+ if (isset($backups[$label])){
+ continue;
+ }
+
+ echo "backup", "register '{$user['username']}' for {$label} backups\n";
+
+ $rsp = flickr_backups_create($user, $type_id);
+ }
+ }
+
+?>
View
11 schema/alters/20120217.db_users.schema
@@ -0,0 +1,11 @@
+CREATE TABLE `FlickrGeoBookmarks` (
+ `id` BIGINT(20) UNSIGNED NOT NULL,
+ `user_id` INT(11) UNSIGNED NOT NULL,
+ `name` VARCHAR(255),
+ `latitude` DECIMAL(9,6) DEFAULT NULL,
+ `longitude` DECIMAL(9,6) DEFAULT NULL,
+ `accuracy` TINYINT(3) UNSIGNED NOT NULL,
+ `woeid` INT(11) UNSIGNED NOT NULL,
+ `geoperms` TINYINT(3) UNSIGNED NOT NULL,
+ `geocontext` TINYINT(3) UNSIGNED NOT NULL
+) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
View
14 schema/db_users.schema
@@ -97,3 +97,17 @@ CREATE TABLE `FlickrPhotosGeoCorrections` (
KEY `by_user_old` (`user_id`, `old_woeid`),
KEY `by_user_new` (`user_id`, `new_woeid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
+
+DROP TABLE IF EXISTS `FlickrGeoBookmarks`;
+
+CREATE TABLE `FlickrGeoBookmarks` (
+ `id` BIGINT(20) UNSIGNED NOT NULL,
+ `user_id` INT(11) UNSIGNED NOT NULL,
+ `name` VARCHAR(255),
+ `latitude` DECIMAL(9,6) DEFAULT NULL,
+ `longitude` DECIMAL(9,6) DEFAULT NULL,
+ `accuracy` TINYINT(3) UNSIGNED NOT NULL,
+ `woeid` INT(11) UNSIGNED NOT NULL,
+ `geoperms` TINYINT(3) UNSIGNED NOT NULL,
+ `geocontext` TINYINT(3) UNSIGNED NOT NULL
+) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
View
41 www/include/lib_flickr_backups.php
@@ -2,6 +2,7 @@
loadlib("flickr_users");
loadlib("flickr_contacts_import");
+ loadlib("flickr_geobookmarks_import");
loadlib("flickr_photos_import");
loadlib("flickr_faves_import");
@@ -17,6 +18,7 @@ function flickr_backups_type_map($string_keys=0){
0 => 'photos',
1 => 'faves',
2 => 'contacts',
+ 3 => 'geobookmarks'
);
if ($string_keys){
@@ -281,4 +283,43 @@ function flickr_backups_get_contacts(&$user){
}
#################################################################
+
+ function flickr_backups_get_geobookmarks(&$user){
+
+ $backups = flickr_backups_for_user($user);
+
+ if (! isset($backups['geobookmarks'])){
+
+ # return not_okay("backups not registered");
+ }
+
+ $backup = $backups['geobookmarks'];
+ $update = array();
+
+ $start_time = time();
+
+ $flickr_user = flickr_users_get_by_user_id($user['id']);
+
+ $rsp = flickr_geobookmarks_import_for_nsid($flickr_user['nsid'], $more);
+
+ if ($rsp['ok']){
+ $update['date_lastupdate'] = $start_time;
+ $update['details'] = "count: {$rsp['count_imported']}";
+
+ if (! $backup['date_firstupdate']){
+ $update['date_firstupdate'] = $update['date_lastupdate'];
+ }
+ }
+
+ else {
+ $update['details'] = "update failed ($start_time) : {$rsp['error']}";
+ }
+
+ flickr_backups_update($backup, $update);
+
+ return $rsp;
+ }
+
+ #################################################################
+
?>
View
39 www/include/lib_flickr_geobookmarks.php
@@ -0,0 +1,39 @@
+<?php
+
+ #################################################################
+
+ function flickr_geobookmarks_add($bookmark){
+
+ $user = users_get_by_id($bookmark['user_id']);
+ $cluster_id = $user['cluster_id'];
+
+ $insert = array();
+
+ foreach ($bookmark as $k => $v){
+ $insert[$k] = AddSlashes($v);
+ }
+
+ $rsp = db_insert_users($cluster_id, 'FlickrGeoBookmarks', $insert);
+
+ if ($rsp['ok']){
+ $rsp['bookmark'] = $bookmark;
+ }
+
+ return $rsp;
+ }
+
+ #################################################################
+
+ function flickr_geobookmarks_purge_for_user(&$user){
+
+ $cluster_id = $user['cluster_id'];
+ $enc_id = AddSlashes($user['id']);
+
+ $sql = "DELETE FROM FlickrGeoBookmarks WHERE user_id='{$enc_id}'";
+ $rsp = db_write_users($cluster_id, $sql);
+
+ return $rsp;
+ }
+
+ #################################################################
+?>
View
93 www/include/lib_flickr_geobookmarks_import.php
@@ -0,0 +1,93 @@
+<?php
+
+ loadlib("flickr_api");
+ loadlib("flickr_users");
+ loadlib("flickr_geobookmarks");
+ loadlib("flickr_geo_permissions");
+
+ #################################################################
+
+ 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));
+ }
+
+ #################################################################
+
+?>

0 comments on commit d953d4b

Please sign in to comment.