#!/usr/bin/php5
'_', '.' => ''));
$str = strtr($str,
"ÀÁÂÃÄÅàáâãäåÇçÒÓÔÕÖØòóôõöøÈÉÊËèéêëÌÍÎÏìíîïÙÚÛÜùúûü¾ÝÿýÑñ",
"AAAAAAaaaaaaCcOOOOOOooooooEEEEeeeeIIIIiiiiUUUUuuuuYYyyNn");
$str = preg_replace('/[^a-zA-Z0-9_-]/', '', $str);
return $str;
}
function renameImage($file, $path)
{
if (!preg_match('!^([0-9]+)[ _.-].*\.jpg$!', $file, $match))
return false;
$id = (int) $match[1];
if (!preg_match('!^([0-9]+)_[a-f0-9]+.*\.jpg$!', $file) && !FORCE_NON_FLICKR)
{
echo "Will not treat {$file} because it's not a native flickr photo.\n"
. "If {$id} is a real flickr ID, you can try to use the --force argument.\n";
return false;
}
$url = 'http://flickr.com/photo.gne?id='.$id;
$content = @file_get_contents($url);
if (!$content)
die("Can't connect to $url\n");
$title = false;
$author = false;
$copy = false;
if (preg_match('!
(.+)
!iU', $content, $match))
{
$title = html_entity_decode($match[1]);
}
if (preg_match("!var geo_possessed_username = '(.+)';!iU", $content, $match))
{
$author = $match[1];
}
if (preg_match('!rdf:resource="http://creativecommons\.org/licenses/([^/]+)/[0-9.]+/"!i', $content, $match))
{
$copy = 'CC-'.$match[1];
}
elseif (preg_match('!.*]*>©!s', $content))
{
$copy = 'Copyright';
}
if (!$author || !$title || !$copy)
{
echo "Can't find informations for {$url} "
."(that's probably a private image or is subject to safesearch)\n";
return false;
}
$new_name = strtr(RENAME_PATTERN, array(
'%TITLE' => cleanName($title),
'%ID' => $id,
'%LICENSE' => $copy,
'%AUTHOR' => cleanName($author),
)
);
rename($path . '/' . $file, $path . '/' . $new_name);
echo "$file -> $new_name\n";
return true;
}
$path = false;
foreach ($_SERVER['argv'] as $k => $v)
{
if ($k < 1) continue;
if ($v == '-f' || $v == '--force')
define('FORCE_NON_FLICKR', true);
else
$path = $v;
}
if (!defined('FORCE_NON_FLICKR'))
define('FORCE_NON_FLICKR', false);
if (!$path)
{
$path = $_SERVER['PWD'];
}
else
{
$path = realpath($path);
}
$path = preg_replace('!/+$!', '', $path);
if (!file_exists($path))
die("$path does not exists\n");
if (is_dir($path))
{
$dir = dir($path);
while ($file = $dir->read())
{
if ($file[0] == '.')
continue;
renameImage($file, $path);
}
$dir->close();
}
elseif (is_file($path))
{
if (!renameImage(basename($path), dirname($path)))
{
die("Can't process $path (is it a flickr photo ?)\n");
}
}
?>