home *** CD-ROM | disk | FTP | other *** search
/ Neil's C++ Stuff / 2015-03-neilstuff.zip / arch / up2picasa.php < prev   
PHP Script  |  2015-03-14  |  5KB  |  201 lines

  1. <?php
  2.  
  3. date_default_timezone_set('America/Los_Angeles');
  4. error_reporting(E_ALL | E_STRICT);
  5.  
  6. /*$fn = '040308_09281.jpg';
  7. $exif = exif_read_data('2008-04/' . $fn, 'EXIF', true);
  8. $t = strtotime($exif['EXIF']['DateTimeOriginal']) - 25200;
  9. echo($fn . ': ' . date(DATE_RSS, $t) . "\n");
  10. foreach ($exif as $title => $sec) {
  11.     foreach ($sec as $nm => $v) {
  12.         echo("{$title}.{$nm}=$v\n");
  13.     }
  14. }
  15. exit(1);*/
  16.  
  17. $errcount = 0;
  18. $errmax = 10;
  19.  
  20. if ($argc < 2) {
  21.     die("Syntax: <uid> <pwd>\n");
  22. }
  23.  
  24. require('../../obremsdk/php/picasa.php');
  25.  
  26. $p = new Picasa();
  27. $uid = $argv[1];
  28. $p->Login($uid, $argv[2], 'NeilObremski-PicBackup');
  29.  
  30. $archfols = array();
  31. $archfolts = 0;
  32. $dh = opendir('.');
  33. while (false !== ($archfol = readdir($dh))) {
  34.     
  35.     if (!preg_match('/^([0-9]{4})-[0-9]{2}$/', $archfol, $m)) {
  36.         continue;
  37.     }
  38.     $archfols []= $archfol;
  39. }
  40. sort($archfols, SORT_STRING);
  41.  
  42. $lastyear = false;
  43.  
  44. foreach ($archfols as $archfol) {
  45.  
  46.     $year = substr($archfol, 0, strpos($archfol, '-'));
  47.     $albt = $alb = false;
  48.  
  49.     if ($year != $lastyear) {
  50.         if ($lastyear) {
  51.             echo("Flushing $lastyear\n");
  52.             $p->Flush(); // keep memory down
  53.         }
  54.         $lastyear = $year;
  55.     }
  56.  
  57.     echo("Year: {$year} ({$archfol})\n");
  58.     $ydh = opendir("{$archfol}");
  59.     $files = array();
  60.     while (false !== ($fn = readdir($ydh))) {
  61.  
  62.         // only process JPEG's
  63.         if (!preg_match('/\.jpg$/', $fn)) {
  64.             continue;
  65.         }
  66.         
  67.         // skip avatars
  68.         if (preg_match('/^[0-9]{2}[- ]/', $fn)) {
  69.             continue;
  70.         }
  71.  
  72.         $files []= $fn;
  73.     }
  74.  
  75.     echo("Sorting\n");
  76.     sort($files, SORT_STRING);
  77.     $archfolts = strtotime("$archfol-01T00:00:00") * 1000;
  78.     if (!$archfolts) {
  79.         die("Unable to generate default image timestamp from $archfol\n");
  80.     }
  81.  
  82.     foreach ($files as $fn) {
  83.  
  84.         echo("  : $fn ...\n");
  85.         $fullfn = "{$archfol}/$fn";
  86.  
  87.         // see if photo already exists (search all parts for this year)
  88.         $pic = $p->FindFile($uid, $fn, "/$year/");
  89.         $checksum = @$pic['gphoto:checksum'];
  90.         if ($pic) {
  91.             echo("    exists [{$checksum}]\n");
  92.             continue;
  93.         }
  94.  
  95.         // determine if metadata needs to be set for timestamp
  96.         $meta = metainf($fullfn, $fn);
  97.         $summary = false;
  98.  
  99.         if (false !== $alb && 0 == $alb['gphoto:numphotosremaining']) {
  100.             $alb = false;
  101.         }
  102.  
  103.         // find next album with enough room
  104.         for ($i = 1; false === $alb && $i < 10; $i++) {
  105.             $albt = "{$year}p{$i}";
  106.  
  107.             // select album else create it
  108.             if (false === ($alb = $p->FindAlbum($uid, $albt))) {
  109.                 echo("Creating Album \"{$albt}\"\n");
  110.                 // set gphoto:timestamp
  111.                 $mstime = strtotime("{$year}-01-01T00:00:00") * 1000 + $i;
  112.                 $alb = $p->CreateAlbum($uid, $albt, "Photos from $year",
  113.                     array('gphoto:timestamp' => $mstime));
  114.                 if (false === $alb) {
  115.                     die($p->Err() . ": " . $p->Msg() . "\n");
  116.                 }
  117.                 echo("Created Album \"{$albt}\" ({$alb['gphoto:id']})\n");
  118.             }
  119.  
  120.             if (!array_key_exists('gphoto:numphotosremaining', $alb)) {
  121.                 var_dump($alb);
  122.                 die("FUCK!");
  123.             }
  124.  
  125.             if (0 == $alb['gphoto:numphotosremaining']) {
  126.                 $alb = false;
  127.             }
  128.         }
  129.  
  130.         if (!$alb) {
  131.             die("No albums with room\n");
  132.         }
  133.  
  134.         // upload
  135.         echo("    uploading\n");
  136.         $pic = $p->UploadPhoto($alb, $fullfn, $summary, $meta);
  137.         if (!$pic) {
  138.             ++$errcount;
  139.             echo("    " . $p->Err() . ": " . $p->Msg() . " ~ {$errcount}/{$errmax}\n");
  140.             if (0 == $p->Err()) {
  141.                 var_dump($alb);
  142.                 exit(1);
  143.             }
  144.             if ('Not a valid image.' == $p->Msg()) {
  145.                 echo("    deleting and decrementing error count\n");
  146.                 $errcount--;
  147.                 unlink($fullfn);
  148.             }
  149.             if ($errcount >= $errmax) {
  150.                 exit(1);
  151.             }
  152.         } else {
  153.             $npr = (--$alb['gphoto:numphotosremaining']);
  154.             echo("    @ \"{$pic['title']}\" ({$pic['gphoto:id']}/{$pic['gphoto:checksum']}) ~ $npr\n");
  155.         }
  156.  
  157.     } // while (loop through jpeg's)
  158.  
  159. } // while (loop through arch YYYY-MM folders)
  160.  
  161. function metainf($fullfn, $fn)
  162. {
  163.     global $archfolts;
  164.  
  165.     $exif = exif_read_data($fullfn, 'EXIF', true);
  166.     $fdt = @$exif['EXIF']['DateTimeOriginal'];
  167.     if (!($checksum = sha1_file($fullfn))) {
  168.         die("Unable to generate file checksum!\n");
  169.     }
  170.     $meta = array('gphoto:checksum' => $checksum);
  171.     if ($fdt) {
  172.         $fdt = strtotime($fdt) - 25200;
  173.         echo("    timestamp = " . date(DATE_RSS, $fdt) . " ($fdt)\n");
  174.     } else {
  175.         
  176.         if (preg_match('/(200[0-9])([0-9]{2})([0-9]{2})-([0-9]{2})([0-9]{2})([0-9]{2})/', $fn, $m)) {
  177.             // blackjack-20070908-135558.jpg
  178.             $tss = $m[0];
  179.             $fdt = strtotime("{$m[1]}-{$m[2]}-{$m[3]}T{$m[4]}:{$m[5]}:{$m[6]}");
  180.             if (!$fdt) {
  181.                 die("Unable to convert blackjack time!\n");
  182.             }
  183.         } else if (preg_match('/^(01|02|03|04|05|06|07|08|09|10|11|12)([0-3][0-9])(0[0-9])([0-9]{2})([0-9]{2})\./', $fn, $m)) {
  184.             // 1127071727.jpg
  185.             $tss = $m[0];
  186.             $fdt = strtotime("20{$m[3]}-{$m[1]}-{$m[2]}T{$m[4]}:{$m[5]}:00");
  187.             if (!$fdt) {
  188.                 die("Unable to convert LG time!\n");
  189.             }
  190.         } else {
  191.             $fdt = $archfolts;
  192.             $tss = 'default';
  193.         }
  194.         echo("    timestamp = $tss ($fdt)\n");
  195.         $meta['gphoto:timestamp'] = $fdt . '000'; // in milliseconds
  196.     }
  197.     return $meta;
  198. }
  199.  
  200. ?>
  201.