-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new 'checksums' command, to add sha1 or md5 machine tags.
Refs #20
- Loading branch information
Showing
4 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => $photo['tags'] . " checksum:$hash=$fileHash" | ||
]); | ||
if (isset($tagAdded->err)) { | ||
throw new Exception($tagAdded->err['msg']); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74df637
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not test yet. Why not adding an option to the Download command instead of extending the class?