Skip to content

Instantly share code, notes, and snippets.

@Yoxem
Last active January 16, 2019 15:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yoxem/8e9bbb54f353de7a79f43507e680aa7b to your computer and use it in GitHub Desktop.
Save Yoxem/8e9bbb54f353de7a79f43507e680aa7b to your computer and use it in GitHub Desktop.
catagorize_flickr_file.py - catagorizing Flickr backup images/videos by album name.
#!/usr/bin/env python3
#-*-coding:utf-8-*-
'''
Author: Yoxem (yoxem.tem98 [A@T] nctu.edu.tw)
Date: 20190116
License: MIT License
DESCRIPTION
------------------------
catagorize_flickr_file.py -
catagorize Flickr backup images/videos by album name. eg:
categorized
|- album_name_1
| |- img_name_1
| |- img_name_2
| |- ...
|
|- album_name_2
| |- img_name_101
| |- img_name_102
| |- ...
|
|- album_name_3
|- ...
...
INSTRUCTIION
----------------
Before executing the program by "python3 rename_file_2.py":
1. Create a folder "flickr", then create "catagorized", "image_base", "personal_data" folders
inside the "flickr" folder.
2. follow the instructions of paragraph "Export everything from Flickr" in
https://www.macworld.com/article/3153944/ to download Account data zip files
and Photos and videos zip files.
3. Extract account data zip files, and move all the .json files to "flickr/personal_data" folder.
4. Extract all the image & video zip files, and move all the image & video files to "flickr/image_base" folder.
6. Download catagorize_flickr_file.py to [/path/to/flickr/folder].
5. Make sure you have python3, pip3, and module delegator (installed by "pip3 install delegator.py") installed.
6. Open the terminal, "cd [/path/to/flickr/folder]; python3 catagorize_flickr_file.py"
7. And all the catagorized images and folders are in "flickr/catagorized" folder.
LICENSE
---------------
Copyright (C) 2019 Yoxem Chen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
import delegator # pip3 install delegator.py
import json
import os
import re
import shutil
file_type_list = ['jpg', 'png', 'jpeg', 'mp4'] # types of the files in your flickr account
personal_data_list = os.listdir(os.path.join(".","personal_data"))
photo_json_match = lambda x : re.match("photo_.*.json", x)
photo_json_list = list(filter(photo_json_match, personal_data_list))
for photo_json_file in photo_json_list:
with open(os.path.join("personal_data", photo_json_file)) as f:
data = json.load(f)
if data["albums"] == []:
album_str = "無相簿"
else:
album_str = data["albums"][0]["title"].replace("/", "/")
# workaround for long-name albums
if len(album_str) > 40:
album_str = album_str[:40]
catagorized_path = os.path.join(".", "catagorized")
try:
os.makedirs(catagorized_path)
except FileExistsError:
pass
if os.path.isdir(os.path.join(catagorized_path, album_str)):
pass
else:
os.makedirs(os.path.join(catagorized_path, album_str))
img_file_str_base = data["id"]
img_old_file_str = delegator.chain("ls image_base | grep " + str(img_file_str_base)).out[:-1]
file_type_str_pattern = ".+\\.(" + "|".join(file_type_list) + ")$"
file_type = re.match(file_type_str_pattern, data["original"]).group(1)
img_file_str = img_file_str_base + "." + file_type
img_name = data["name"].replace("/", "/")
old_path = os.path.join("image_base", img_old_file_str)
new_path = os.path.join(catagorized_path, album_str, img_name + "." + file_type)
print("Copying ", old_path, "to", new_path)
shutil.copyfile(old_path, new_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment