Skip to content

Commit

Permalink
Add new 'checksums' command, to add sha1 or md5 machine tags.
Browse files Browse the repository at this point in the history
Refs #20
  • Loading branch information
samwilson committed Jan 18, 2018
1 parent a5c5749 commit ae0b3fa
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 4 deletions.
2 changes: 2 additions & 0 deletions bin/flickr-cli
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ if (isset($autoloader)) {

use Symfony\Component\Console\Application;

use TheFox\FlickrCli\Command\ChecksumsCommand;
use TheFox\FlickrCli\Command\PiwigoCommand;
use TheFox\FlickrCli\FlickrCli;
use TheFox\FlickrCli\Command\AlbumsCommand;
Expand All @@ -32,6 +33,7 @@ use TheFox\FlickrCli\Command\UploadCommand;
$application = new Application(FlickrCli::NAME, FlickrCli::VERSION);
$application->add(new AlbumsCommand());
$application->add(new AuthCommand());
$application->add(new ChecksumsCommand());
$application->add(new DeleteCommand());
$application->add(new DownloadCommand());
$application->add(new FilesCommand());
Expand Down
2 changes: 1 addition & 1 deletion src/TheFox/FlickrCli/Command/AuthCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private function getPermissionType(): string
$question = 'Please select from the following three options';
$choices = [
'read' => 'download photos',
'write' => 'upload photos',
'write' => 'upload or edit photos or their metadata',
'delete' => 'download and/or delete photos from Flickr',
];

Expand Down
116 changes: 116 additions & 0 deletions src/TheFox/FlickrCli/Command/ChecksumsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace TheFox\FlickrCli\Command;

use Exception;
use Rezzza\Flickr\ApiFactory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;

class ChecksumsCommand extends DownloadCommand
{

/** @var string */
protected $tmpDir;

/** @var ApiFactory */
protected $apiFactory;

protected function configure()
{
FlickrCliCommand::configure();
$this->setName('checksums');
$this->setDescription('Add checksum machine tags to photos already on Flickr.');
$this->addOption('hash', null, InputOption::VALUE_OPTIONAL, 'The hash function to use. Default: sha1', 'sha1');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
FlickrCliCommand::execute($input, $output);
$this->apiFactory = $this->getApiService()->getApiFactory();

// Set up the temporary directory.
$this->tmpDir = sys_get_temp_dir() . '/flickr-cli';
$filesystem = new Filesystem();
if (!$filesystem->exists($this->tmpDir)) {
$filesystem->mkdir($this->tmpDir, 0755);
}

// Get all photos.
$page = 1;
do {
pcntl_signal_dispatch();
if ($this->getExit()) {
break;
}
$params = [
'user_id' => 'me',
'page' => $page,
'per_page' => 500,
'extras' => 'o_url, tags',
];
$photos = $this->apiFactory->call('flickr.people.getPhotos', $params);
$this->getOutput()->writeln("Page $page of " . $photos->photos['pages']);
foreach ($photos->photos->photo as $photo) {
$this->processPhoto($photo);
}
$page++;
} while ($photos->photos['page'] !== $photos->photos['pages']);

// Clean up the temporary directory.
foreach (scandir($this->tmpDir) as $file) {
unlink($file);
}
rmdir($this->tmpDir);

return $this->getExit();
}

protected function processPhoto($photo)
{
// Find the hash function.
$hash = $this->getInput()->getOption('hash');
$hashFunction = $hash . '_file';
if (!function_exists($hashFunction)) {
throw new Exception("Hash function not available: $hashFunction");
}

// See if the photo has already got a checksum tag.
preg_match("/checksum:$hash=(.*)/", $photo['tags'], $matches);
if (isset($matches[1])) {
// If it's already got a tag, do nothing more.
$this->getLogger()->debug(sprintf('Already has checksum: %s', $photo['id']));
return;
}

$this->getLogger()->info(sprintf('Adding checksum machine tag to: %s', $photo['id']));

// Download the file.
$tmpFilename = 'checksumming';
$downloaded = $this->downloadPhoto($photo, $this->tmpDir, $tmpFilename);
if (false === $downloaded) {
$this->getLogger()->error(sprintf('Unable to download: %s', $photo['id']));
return;
}

// Calculate the file's hash.
$filename = $this->tmpDir . '/' . $tmpFilename . '.' . $downloaded['originalformat'];
$fileHash = $hashFunction($filename);

// Upload the new tag.
$tagAdded = $this->apiFactory->call('flickr.photos.setTags', [
'photo_id' => $photo['id'],
'tags' => "checksum:$hash=$fileHash"
]);
if (isset($tagAdded->err)) {
throw new Exception($tagAdded->err['msg']);
}
}
}
7 changes: 4 additions & 3 deletions src/TheFox/FlickrCli/Command/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Rych\ByteSize\ByteSize;
use TheFox\FlickrCli\FlickrCli;

final class DownloadCommand extends FlickrCliCommand
class DownloadCommand extends FlickrCliCommand
{
/**
* @var string The destination directory for downloaded files. No trailing slash.
Expand Down Expand Up @@ -246,12 +246,13 @@ private function downloadByAlbumTitle(): int
* additional 'filesize' property will be set on the return element.
*
* @param SimpleXMLElement $photo
* @param string $destinationPath
* @param string $destinationPath The full filesystem path to the directory into which to download the photo. Must
* already exist.
* @param string $basename The filename to save the downloaded file to (without extension).
* @return SimpleXMLElement|boolean Photo metadata as returned by Flickr, or false if something went wrong.
* @throws Exception
*/
private function downloadPhoto(SimpleXMLElement $photo, string $destinationPath, string $basename = null)
protected function downloadPhoto(SimpleXMLElement $photo, string $destinationPath, string $basename = null)
{
$id = (string)$photo->attributes()->id;

Expand Down

0 comments on commit ae0b3fa

Please sign in to comment.