forked from orangeturtle739/photos-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolder_structure.py
executable file
·96 lines (75 loc) · 2.77 KB
/
folder_structure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#! /usr/bin/env python3
# This script reads Folder structure from Photos.app
# and generates a JSON file to be reproduced on export, creating
# a structure identical with the albuns inside it.
#
import json
import progressbar
import os
import sys
import sqlite3
# SOME CONSTANTS USED
ALBUM_TABLE = "RKAlbum"
FOLDER_TABLE = "RKFolder"
UUID_FIELD = "uuid"
NAME_FIELD = "name"
TRASH_FIELD = "isInTrash"
ALBUM_FOLDER_FIELD = "folderUuid"
FOLDER_PATH_FIELD = "folderPath"
FOLDER_MODELID = "modelId"
IGNORED_FOLDERS = [
'LibraryFolder',
'TopLevelAlbums',
'MediaTypesSmartAlbums',
'TopLevelSlideshows',
'TrashFolder']
JSON_FILENAME = "folders.json"
def split_path(path):
folders = path.split('/')
return folders
def run(lib_dir, output_dir):
db_path = os.path.join(lib_dir, 'database')
main_db_path = os.path.join(db_path, 'photos.db')
main_db = sqlite3.connect(main_db_path)
main_db.row_factory = sqlite3.Row
folders_table = main_db.cursor()
folders_table.execute('SELECT * FROM ' + FOLDER_TABLE + ' WHERE isInTrash=0')
# will store modelID -> [FOLDERNAME, PATH]
db_folder_dict = {}
# PASSO2: Para cada pasta listada,
for folder in iter(folders_table.fetchone, None):
folder_path = folder[FOLDER_PATH_FIELD]
folder_name = folder[NAME_FIELD]
folder_uuid = folder[UUID_FIELD]
folder_modelid = folder[FOLDER_MODELID]
# add to dictionary
db_folder_dict[folder_modelid] = [
folder_uuid, folder_name, folder_path]
# Dict ready. Let's substitute the Paths and return a simpler dict
final_dict = {}
for key, val in db_folder_dict.items():
# get the path in numbers
path_numbered = val[2]
# the key for the final dict
key_uuid = val[0]
name = val[1]
path_described = ""
for number in path_numbered.split('/'):
if number != "": # the last will always be empty, ignore
int_number = int(number)
folder_name = db_folder_dict[int_number][1]
folder_uuid = db_folder_dict[int_number][0]
if folder_uuid not in IGNORED_FOLDERS:
if path_described != "":
path_described += os.path.sep
path_described += folder_name # Change folder number to Folder name
final_dict[key_uuid] = [name, path_described]
# mount and store final JSON on file
json_folders = json.dumps(final_dict)
json_path = os.path.join(output_dir, JSON_FILENAME)
json_file = open(json_path, "w")
json_file.write(json_folders)
json_file.close()
# Usage: ./folder_structure.py <photo_library> <output_dir>
if __name__ == '__main__':
run(sys.argv[1], sys.argv[2])