Skip to content

Instantly share code, notes, and snippets.

@nczz nczz/flickr.php
Created Feb 11, 2020

Embed
What would you like to do?
[Flickr] 請求 Flickr 全部資料與本機還原的做法
<?php
/**
* 作者: Chun
* 說明: 此程式使用於請求( https://www.flickr.com/account )下載完整的 Flickr 檔案,整理照片檔案。
* 使用方法: 將所有檔案解壓縮至 Flickr 資料夾後,將此程式放於 Flickr 上一層。
* 執行程式指令: php -f flickr.php (將此段程式儲存並命名為 flickr.php)
**/
function filename_safe($name) {
$except = array('\\', '/', ':', '*', '?', '"', '<', '>', '|', ' ');
return str_replace($except, '_', $name);
}
function mkdir_p($dirName, $rights = 0777) {
$dirs = explode('/', $dirName);
$dir = '';
foreach ($dirs as $part) {
$dir .= filename_safe($part) . '/';
if (!is_dir($dir) && strlen($dir) > 0) {
mkdir($dir, $rights);
}
}
}
$count_single_album_files = 0;
foreach (glob('Flickr/photo_*.json') as $json) {
$data = json_decode(file_get_contents($json), true);
$media_file_path = "";
$media_file_count = 0;
$media_place_folder = "";
$json_filename = basename($json);
foreach (glob('Flickr/*' . $data['id'] . '*.{jpg,mp4,png,avi,gif,mov}', GLOB_BRACE) as $media) {
$media_file_path = $media;
$media_file_count += 1;
}
if ($media_file_count >= 2) {
//重複 ID ,先暫停,觀察看看
print_r("Oops!" . PHP_EOL);
print_r($data);
exit;
}
$media_filename = basename($media_file_path);
// 不屬於任何相簿的話,使用拍照年作為相簿
if (empty($data['albums']) && $data['date_taken'] != "") {
$date_time = date_parse_from_format('Y-m-d H:i:s', $data['date_taken']);
$year = $date_time['year'];
if ($date_time['error_count'] == 0 && $year != "") {
$media_place_folder = 'Flickr/' . $year;
if (!is_dir($media_place_folder)) {
mkdir_p($media_place_folder);
}
rename('Flickr/' . $media_filename, $media_place_folder . '/' . $media_filename);
rename('Flickr/' . $json_filename, $media_place_folder . '/' . $json_filename);
$count_single_album_files += 1;
print_r($data['id'] . " -> 完成!( " . $count_single_album_files . " )" . PHP_EOL);
}
} else {
//有相簿的話,抓最長名稱也應該算是最有意義的相簿名稱吧(!?)
$album_name = ""; //找最長的相簿名稱
foreach ($data['albums'] as $key => $album) {
if (mb_strlen($album['title']) > mb_strlen($album_name)) {
$album_name = $album['title'];
}
}
$media_place_folder = 'Flickr/' . filename_safe($album_name);
if (!is_dir($media_place_folder)) {
mkdir_p($media_place_folder);
}
rename('Flickr/' . $media_filename, $media_place_folder . '/' . $media_filename);
rename('Flickr/' . $json_filename, $media_place_folder . '/' . $json_filename);
$count_single_album_files += 1;
print_r($data['id'] . " -> 完成!( " . $count_single_album_files . " )" . PHP_EOL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.