Last active
March 19, 2024 11:49
-
-
Save rwunsch/dc67372b6b937f13563ac8b5970c3b89 to your computer and use it in GitHub Desktop.
Python script to create a "tree"-like view (file) as output of all folders and subfolders for AEM Cloud Service (AEMaaCS) - uses ASSETS API .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
def get_folder_structure(token, base_url, debug_mode=False, path="/api/assets"): | |
if debug_mode: | |
print(f"Fetching folder structure from {base_url}{path}") | |
else: | |
print(".", end="", flush=True) | |
headers = { | |
"Authorization": f"Bearer {token}", | |
"Content-Type": "application/json" | |
} | |
url = f"{base_url}{path}.json" | |
if debug_mode: | |
print(f"Asset API request link: {url}") | |
response = requests.get(url, headers=headers) | |
response.raise_for_status() | |
data = response.json() | |
structure = [] | |
if "class" in data and "assets/folder" in data["class"]: | |
folder_name = data["properties"]["name"] | |
if debug_mode: | |
print(f"Current object is a folder with name: {folder_name} and path: {path}") | |
else: | |
print(".", end="", flush=True) | |
for item in data.get("entities", []): | |
if "class" in item and "assets/folder" in item["class"]: | |
sub_folder_name = item["properties"]["name"] | |
sub_folder_path = f"{path}/{sub_folder_name}" | |
if debug_mode: | |
print(f"Found sub-folder: {sub_folder_name} with subfolder-path: {sub_folder_path}") | |
structure.append(sub_folder_name) | |
sub_structure = get_folder_structure(token, base_url, debug_mode=debug_mode, path=sub_folder_path) | |
structure.append(sub_structure) | |
return structure | |
def print_folder_structure(structure, depth=0, file=None): | |
for item in structure: | |
if isinstance(item, list): | |
print_folder_structure(item, depth + 1, file) | |
else: | |
print(" " * depth + "├── " + item, file=file) | |
def load_config(filename="config.json"): | |
with open(filename) as config_file: | |
config = json.load(config_file) | |
return config.get("base_url"), config.get("token"), config.get("debug_mode", False) | |
def main(): | |
base_url, token, debug_mode = load_config() | |
try: | |
print("Fetching folder structure...") | |
structure = get_folder_structure(token, base_url, debug_mode=debug_mode) | |
print("\nFolder structure fetched successfully.") | |
with open("folder_structure.txt", "w") as f: | |
print("Printing folder structure...") | |
print_folder_structure(structure, file=f) | |
print("Folder structure saved to folder_structure.txt") | |
except Exception as e: | |
print("Error:", e) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"stage": "prod", | |
"debug_mode": "False", | |
"base_url": "https://author-pXXXXXX-eXXXXXX.adobeaemcloud.com/", | |
"token": "<token-from-developer-console>" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
├── appdata | |
├── archive | |
├── crunch | |
├── company | |
├── FY21 | |
├── p1---december | |
├── wk1---2911 | |
├── test | |
├── zoom | |
├── crunch | |
├── company | |
├── test2610 | |
├── workfront | |
├── test | |
├── previous-qa-tests | |
├── projects | |
├── test | |
├── test-flush-cache |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment