|
| 1 | +import os |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from time import time, sleep |
| 5 | + |
1 | 6 | from .service import Service |
2 | | -from typing import List |
| 7 | +from ..lib.utils import Utils |
3 | 8 |
|
4 | 9 | from copy import deepcopy |
| 10 | +from typing import List |
| 11 | +from pprint import pprint |
5 | 12 |
|
6 | 13 | class FrameioHelpers(Service): |
7 | 14 | def get_updated_assets(self, account_id, project_id, timestamp): |
@@ -34,81 +41,110 @@ def get_updated_assets(self, account_id, project_id, timestamp): |
34 | 41 | endpoint = '/search/library' |
35 | 42 | return self.client._api_call('post', endpoint, payload=payload) |
36 | 43 |
|
37 | | - def get_assets_recursively(self, asset_id, slim, state: List): |
| 44 | + def get_assets_recursively(self, asset_id, slim=True): |
38 | 45 | assets = self.client.assets.get_children(asset_id, slim) |
39 | | - |
40 | | - print("Number of folders at top level", len(assets)) |
| 46 | + print("Number of assets at top level", len(assets)) |
41 | 47 |
|
42 | 48 | for asset in assets: |
43 | | - print(asset['_type'], asset['name']) |
| 49 | + print(f"Type: {asset['_type']}, Name: {asset['name']}, Children: {len(asset['children'])}") |
| 50 | + total_bytes = 0 |
44 | 51 |
|
45 | | - # We only get the first three items when we use "include=children" \ |
46 | | - # which is rather misleading |
47 | | - if asset['_type'] == "folder": |
48 | | - try: |
49 | | - if len(asset['children']) != asset['item_count']: |
50 | | - # Recursively fetch the contents of the folder |
51 | | - # No need to return the state here since it's shared memory |
52 | | - self.recursive_asset_fetch(asset['id'], slim, state) |
53 | | - else: |
54 | | - # Copy the asset before we clear out the children |
55 | | - asset_copy = deepcopy(asset) |
56 | | - asset_copy.pop('children') |
57 | | - state.append(asset_copy) |
58 | | - except KeyError as e: |
59 | | - # print(e) |
60 | | - print(asset.keys()) |
61 | | - |
62 | | - try: |
63 | | - children = asset['children'] |
64 | | - for asset in children: |
65 | | - # if asset['_type'] == "folder": |
66 | | - # # Recursively fetch the contents of the folder |
67 | | - # # No need to return the state here since it's shared memory |
68 | | - # self.recursive_asset_fetch(asset['id'], slim, state) |
69 | | - |
70 | | - if asset['_type'] == "file": |
71 | | - # Append the non-folder asset |
72 | | - state.append(asset) |
73 | | - |
74 | | - if asset['_type'] == "verson_stack": |
75 | | - print("Grabbing top item from version stack") |
76 | | - versions = self.client.assets.get_children(asset['id']) |
77 | | - state.append(versions[0]) |
78 | | - |
79 | | - except KeyError as e: |
80 | | - print(e) |
81 | | - # Children not found for this asset, continuing |
82 | | - pass |
| 52 | + if asset['_type'] == "file": |
| 53 | + # Don't do nothing, it's a file! |
| 54 | + continue |
83 | 55 |
|
84 | | - # if asset['_type'] == "folder": |
85 | | - # # Recursively fetch the contents of the folder |
86 | | - # # No need to return the state here since it's shared memory |
87 | | - # self.recursive_asset_fetch(asset['id'], slim, state) |
| 56 | + if asset['_type'] == "verson_stack": |
| 57 | + print("Grabbing top item from version stack") |
| 58 | + versions = self.client.assets.get_children(asset['id']) |
| 59 | + asset = versions[0] # re-assign on purpose |
| 60 | + continue |
88 | 61 |
|
89 | | - # if asset['_type'] == "file": |
90 | | - # # Append the non-folder asset |
91 | | - # state.append(asset) |
92 | | - |
93 | | - # if asset['_type'] == "verson_stack": |
94 | | - # print("Grabbing top item from version stack") |
95 | | - # versions = self.client.assets.get_children(asset['id']) |
96 | | - # state.append(versions[0]) |
97 | | - |
98 | | - return state |
| 62 | + # We only get the first three items when we use "include=children" |
| 63 | + if asset['_type'] == "folder": |
| 64 | + # try: |
| 65 | + if asset['item_count'] > 3: |
| 66 | + # Recursively fetch the contents of the folder because we have to |
| 67 | + # asset = deepcopy(asset) |
| 68 | + # asset.pop('children') |
| 69 | + asset['children'] = self.get_assets_recursively(asset['id'], slim) |
| 70 | + # asset.update({'children': self.get_assets_recursively(asset['id'], slim)}) |
| 71 | + print("Grabbed more items for this sub dir") |
| 72 | + |
| 73 | + else: |
| 74 | + for i in asset['children']: |
| 75 | + # If a folder is found, we still need to recursively search it |
| 76 | + if i['_type'] == "folder": |
| 77 | + i['children'] = self.get_assets_recursively(i['id'], slim) |
| 78 | + |
| 79 | + # except KeyError as e: |
| 80 | + # # No children found in this folder, move on |
| 81 | + # print(e) |
| 82 | + # continue |
| 83 | + |
| 84 | + return assets |
99 | 85 |
|
100 | 86 | def build_project_tree(self, project_id, slim=False): |
101 | 87 | # if slim == True: |
102 | 88 | # self.client.assets.get_children() |
103 | 89 |
|
104 | 90 | # Get project info |
105 | 91 | project = self.client.projects.get(project_id) |
106 | | - root_asset_id = project['root_asset_id'] |
107 | 92 |
|
108 | 93 | # Get children |
109 | | - initial_tree = self.get_assets_recursively(root_asset_id, slim, state=[]) |
| 94 | + initial_tree = self.get_assets_recursively(project['root_asset_id'], slim) |
110 | 95 |
|
111 | 96 | return initial_tree |
112 | 97 |
|
| 98 | + def download_project(self, project_id, destination): |
| 99 | + project = self.client.projects.get(project_id) |
| 100 | + initial_tree = self.get_assets_recursively(project['root_asset_id'], slim=True) |
| 101 | + self.recursive_downloader(destination, initial_tree) |
| 102 | + # pprint(initial_tree) |
| 103 | + # print(f"Downloading {Utils.format_bytes(total_bytes, type='size')}") |
| 104 | + |
| 105 | + def recursive_downloader(self, directory, asset, count=0): |
| 106 | + # TODO resolve this clusterfuck of downloads |
| 107 | + print(f"Directory {directory}") |
| 108 | + |
| 109 | + try: |
| 110 | + # First check to see if we need to make the directory |
| 111 | + target_directory = os.path.join(os.path.curdir, directory) |
| 112 | + if not os.path.isdir(target_directory): |
| 113 | + os.mkdir(os.path.abspath(target_directory)) |
| 114 | + |
| 115 | + except Exception as e: |
| 116 | + target_directory = os.path.abspath(os.path.join(os.path.curdir, directory)) |
| 117 | + print(e) |
| 118 | + |
| 119 | + if type(asset) == list: |
| 120 | + for i in asset: |
| 121 | + self.recursive_downloader(directory, i) |
| 122 | + |
| 123 | + else: |
| 124 | + try: |
| 125 | + if asset['_type'] == 'folder': |
| 126 | + if len(asset['children']) >= 0: |
| 127 | + count += 1 |
| 128 | + # Create the new folder that these items will go in before it's too late |
| 129 | + if not os.path.exists(os.path.join(target_directory, asset['name'])): |
| 130 | + print("Path doesn't exist") |
| 131 | + new_path = Path(target_directory, str(asset['name']).replace('/', '-')) |
| 132 | + print(new_path.absolute) |
| 133 | + print("Making new directory") |
| 134 | + Path.mkdir(new_path) |
| 135 | + sleep(2) |
| 136 | + |
| 137 | + # Pass along the new directory they'll be living in and the children |
| 138 | + self.recursive_downloader(f"{directory}/{str(asset['name']).replace('/', '-')}", asset['children']) |
| 139 | + |
| 140 | + if asset['_type'] == 'file': |
| 141 | + count += 1 |
| 142 | + return self.client.assets.download(asset, target_directory, multi_part=True, concurrency=10) |
| 143 | + |
| 144 | + except Exception as e: |
| 145 | + print(e) |
| 146 | + |
| 147 | + return True |
| 148 | + |
113 | 149 | if __name__ == "__main__": |
114 | 150 | pass |
0 commit comments