Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding file creation and modified date support + more debugging output #47

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ composer.phar
/log/
.env
.idea
config
photosets/
38 changes: 31 additions & 7 deletions src/TheFox/FlickrCli/Command/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,19 @@ protected function downloadByAlbumTitle(): int
}

/** @var $photo SimpleXMLElement */
$totalPhotos = count($xmlPhotoList->photoset->photo);
$currentPhoto = 0;
foreach ($xmlPhotoList->photoset->photo as $photo) {
$currentPhoto++;
pcntl_signal_dispatch();
if ($this->getExit()) {
break;
}

$this->getLogger()->debug(sprintf('[media] %d/%d photo %s', $page, $fileCount, $photo['id']));
$downloaded = $this->downloadPhoto($photo, $destinationPath);
$debugInfo = " $currentPhoto/$totalPhotos at the page $page/$xmlPhotoListPagesTotal "
. "- photo set: $photosetTitle";
$downloaded = $this->downloadPhoto($photo, $destinationPath, null, $debugInfo);
if ($downloaded && isset($downloaded->filesize)) {
$totalDownloaded += $downloaded->filesize;
}
Expand Down Expand Up @@ -275,8 +280,12 @@ protected function downloadByAlbumTitle(): int
* @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)
{
private function downloadPhoto(
SimpleXMLElement $photo,
string $destinationPath,
string $basename = null,
string $debugInfo
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting a default value in the 3rd argument and then setting no default argument in the 4th should be avoided. This is in general not best practice. Either setting also a default value for $debugInfo or switch: ... string $debugInfo, string $basename = null). Since $destinationPath and $basename are both path related I would prefer to also set a default value for $debugInfo.

You see, this line

$photo = $this->downloadPhoto($photo, $destinationPath, $id, "");

doesn't look very nice. Because the empty string "" can be avoided.

Having a default value and a caller like this,

$photo = $this->downloadPhoto($photo, $destinationPath, $id);

also looks nicer.

) {
$id = (string)$photo->attributes()->id;

$apiFactory = $this->getApiService()->getApiFactory();
Expand Down Expand Up @@ -416,8 +425,9 @@ private function downloadPhoto(SimpleXMLElement $photo, string $destinationPath,
}

$this->getLogger()->info(sprintf(
"[%s] %s, farm %s, server %s, %s, '%s', %s",
"[%s%s] %s, farm %s, server %s, %s, '%s', %s",
$media,
$debugInfo,
$id,
$farm,
$server,
Expand Down Expand Up @@ -485,7 +495,7 @@ private function downloadPhoto(SimpleXMLElement $photo, string $destinationPath,
);
} else {
// Otherwise, just show the amount downloaded and speed.
printf("[file] %s %10s\x1b[0K\r", number_format($downloaded), $downloadedDiffStr);
printf("[file$debugInfo] %s %10s\x1b[0K\r", number_format($downloaded), $downloadedDiffStr);
}
}
fclose($fh);
Expand All @@ -506,6 +516,12 @@ private function downloadPhoto(SimpleXMLElement $photo, string $destinationPath,

/** @var SimpleXMLElement $photo */
$photo = $xmlPhoto->photo;

// update the timstamps of the file
$update_date = (int) $photo->dates['lastupdate'];
$taken_date = strtotime((string) $photo->dates['taken']);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use

strval($photo->dates['taken'])

instead of

(string) $photo->dates['taken']

here. The same goes for (int). Use intval() instead.

touch($filePath, $taken_date, $update_date);
// var_dump($taken_date); exit;

return $photo;
}
Expand Down Expand Up @@ -585,7 +601,15 @@ private function downloadPhotoById(SimpleXMLElement $photo)
{
$id = $photo['id'];
$idHash = md5($id);
$destinationPath = sprintf('%s/%s/%s/%s/%s/%s', $this->destinationPath, $idHash[0], $idHash[1], $idHash[2], $idHash[3], $id);
$destinationPath = sprintf(
'%s/%s/%s/%s/%s/%s',
$this->destinationPath,
$idHash[0],
$idHash[1],
$idHash[2],
$idHash[3],
$id
);

$filesystem = new Filesystem();
if (!$filesystem->exists($destinationPath)) {
Expand All @@ -594,7 +618,7 @@ private function downloadPhotoById(SimpleXMLElement $photo)

// Save the actual file.
$apiFactory = $this->getApiService()->getApiFactory();
$photo = $this->downloadPhoto($photo, $destinationPath, $id);
$photo = $this->downloadPhoto($photo, $destinationPath, $id, "");
if (false === $photo) {
$this->getLogger()->error(sprintf('Unable to get metadata about photo: %s', $id));
return;
Expand Down