Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Owner

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