Skip to content

Instantly share code, notes, and snippets.

@mjaschen
Last active November 16, 2018 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjaschen/bd72be52ccad8979c0474e71ea23492f to your computer and use it in GitHub Desktop.
Save mjaschen/bd72be52ccad8979c0474e71ea23492f to your computer and use it in GitHub Desktop.
Create directory structure which mimics flickr's album structure including all photos.
<?php
declare(strict_types=1);
define('FLICKR_DATA_DIR', '72157673198390467_ce7d69eecfc5_part1');
define('OUTPUT_DIR', __DIR__ . '/output');
$albums = json_decode(file_get_contents(FLICKR_DATA_DIR . '/albums.json'));
foreach ($albums->albums as $album) {
$albumDir = OUTPUT_DIR . '/' . str_replace(["'", '/'], ['', '-'], $album->title);
echo $albumDir . PHP_EOL;
if (!is_dir($albumDir) && !mkdir($albumDir, 0777, true) && !is_dir($albumDir)) {
throw new \RuntimeException(sprintf('Cannot create directory "%s"', $albumDir));
}
foreach ($album->photos as $photoId) {
if ('0' === $photoId) {
continue;
}
$photo = json_decode(file_get_contents(sprintf('%s/photo_%s.json', FLICKR_DATA_DIR, $photoId)));
$file = $albumDir . '/' . pathinfo($photo->original, PATHINFO_BASENAME);
if (file_exists($file)) {
continue;
}
echo ' +-> ' . $photo->name . ', downloading ... ';
file_put_contents($file, fopen($photo->original, 'rb'));
echo 'done' . PHP_EOL;
}
}
@mjaschen
Copy link
Author

mjaschen commented Nov 16, 2018

Usage

  • adjust FLICKR_DATA_DIR (contains the JSON files from a flickr take-out) and OUTPUT_DIR
  • run script with PHP ≥7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment