home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Neil's C++ Stuff
/
2015-03-neilstuff.zip
/
arch
/
up2picasa.php
< prev
Wrap
PHP Script
|
2015-03-14
|
5KB
|
201 lines
<?php
date_default_timezone_set('America/Los_Angeles');
error_reporting(E_ALL | E_STRICT);
/*$fn = '040308_09281.jpg';
$exif = exif_read_data('2008-04/' . $fn, 'EXIF', true);
$t = strtotime($exif['EXIF']['DateTimeOriginal']) - 25200;
echo($fn . ': ' . date(DATE_RSS, $t) . "\n");
foreach ($exif as $title => $sec) {
foreach ($sec as $nm => $v) {
echo("{$title}.{$nm}=$v\n");
}
}
exit(1);*/
$errcount = 0;
$errmax = 10;
if ($argc < 2) {
die("Syntax: <uid> <pwd>\n");
}
require('../../obremsdk/php/picasa.php');
$p = new Picasa();
$uid = $argv[1];
$p->Login($uid, $argv[2], 'NeilObremski-PicBackup');
$archfols = array();
$archfolts = 0;
$dh = opendir('.');
while (false !== ($archfol = readdir($dh))) {
if (!preg_match('/^([0-9]{4})-[0-9]{2}$/', $archfol, $m)) {
continue;
}
$archfols []= $archfol;
}
sort($archfols, SORT_STRING);
$lastyear = false;
foreach ($archfols as $archfol) {
$year = substr($archfol, 0, strpos($archfol, '-'));
$albt = $alb = false;
if ($year != $lastyear) {
if ($lastyear) {
echo("Flushing $lastyear\n");
$p->Flush(); // keep memory down
}
$lastyear = $year;
}
echo("Year: {$year} ({$archfol})\n");
$ydh = opendir("{$archfol}");
$files = array();
while (false !== ($fn = readdir($ydh))) {
// only process JPEG's
if (!preg_match('/\.jpg$/', $fn)) {
continue;
}
// skip avatars
if (preg_match('/^[0-9]{2}[- ]/', $fn)) {
continue;
}
$files []= $fn;
}
echo("Sorting\n");
sort($files, SORT_STRING);
$archfolts = strtotime("$archfol-01T00:00:00") * 1000;
if (!$archfolts) {
die("Unable to generate default image timestamp from $archfol\n");
}
foreach ($files as $fn) {
echo(" : $fn ...\n");
$fullfn = "{$archfol}/$fn";
// see if photo already exists (search all parts for this year)
$pic = $p->FindFile($uid, $fn, "/$year/");
$checksum = @$pic['gphoto:checksum'];
if ($pic) {
echo(" exists [{$checksum}]\n");
continue;
}
// determine if metadata needs to be set for timestamp
$meta = metainf($fullfn, $fn);
$summary = false;
if (false !== $alb && 0 == $alb['gphoto:numphotosremaining']) {
$alb = false;
}
// find next album with enough room
for ($i = 1; false === $alb && $i < 10; $i++) {
$albt = "{$year}p{$i}";
// select album else create it
if (false === ($alb = $p->FindAlbum($uid, $albt))) {
echo("Creating Album \"{$albt}\"\n");
// set gphoto:timestamp
$mstime = strtotime("{$year}-01-01T00:00:00") * 1000 + $i;
$alb = $p->CreateAlbum($uid, $albt, "Photos from $year",
array('gphoto:timestamp' => $mstime));
if (false === $alb) {
die($p->Err() . ": " . $p->Msg() . "\n");
}
echo("Created Album \"{$albt}\" ({$alb['gphoto:id']})\n");
}
if (!array_key_exists('gphoto:numphotosremaining', $alb)) {
var_dump($alb);
die("FUCK!");
}
if (0 == $alb['gphoto:numphotosremaining']) {
$alb = false;
}
}
if (!$alb) {
die("No albums with room\n");
}
// upload
echo(" uploading\n");
$pic = $p->UploadPhoto($alb, $fullfn, $summary, $meta);
if (!$pic) {
++$errcount;
echo(" " . $p->Err() . ": " . $p->Msg() . " ~ {$errcount}/{$errmax}\n");
if (0 == $p->Err()) {
var_dump($alb);
exit(1);
}
if ('Not a valid image.' == $p->Msg()) {
echo(" deleting and decrementing error count\n");
$errcount--;
unlink($fullfn);
}
if ($errcount >= $errmax) {
exit(1);
}
} else {
$npr = (--$alb['gphoto:numphotosremaining']);
echo(" @ \"{$pic['title']}\" ({$pic['gphoto:id']}/{$pic['gphoto:checksum']}) ~ $npr\n");
}
} // while (loop through jpeg's)
} // while (loop through arch YYYY-MM folders)
function metainf($fullfn, $fn)
{
global $archfolts;
$exif = exif_read_data($fullfn, 'EXIF', true);
$fdt = @$exif['EXIF']['DateTimeOriginal'];
if (!($checksum = sha1_file($fullfn))) {
die("Unable to generate file checksum!\n");
}
$meta = array('gphoto:checksum' => $checksum);
if ($fdt) {
$fdt = strtotime($fdt) - 25200;
echo(" timestamp = " . date(DATE_RSS, $fdt) . " ($fdt)\n");
} else {
if (preg_match('/(200[0-9])([0-9]{2})([0-9]{2})-([0-9]{2})([0-9]{2})([0-9]{2})/', $fn, $m)) {
// blackjack-20070908-135558.jpg
$tss = $m[0];
$fdt = strtotime("{$m[1]}-{$m[2]}-{$m[3]}T{$m[4]}:{$m[5]}:{$m[6]}");
if (!$fdt) {
die("Unable to convert blackjack time!\n");
}
} 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)) {
// 1127071727.jpg
$tss = $m[0];
$fdt = strtotime("20{$m[3]}-{$m[1]}-{$m[2]}T{$m[4]}:{$m[5]}:00");
if (!$fdt) {
die("Unable to convert LG time!\n");
}
} else {
$fdt = $archfolts;
$tss = 'default';
}
echo(" timestamp = $tss ($fdt)\n");
$meta['gphoto:timestamp'] = $fdt . '000'; // in milliseconds
}
return $meta;
}
?>