diff options
Diffstat (limited to 'vault.py')
| -rw-r--r-- | vault.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/vault.py b/vault.py new file mode 100644 index 0000000..4696ffc --- /dev/null +++ b/vault.py | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | import xml.etree.ElementTree as ET | ||
| 2 | import requests | ||
| 3 | |||
| 4 | url = "https://mitjafelicijan.fra1.digitaloceanspaces.com/" | ||
| 5 | |||
| 6 | def truncate_filename(filename, max_length): | ||
| 7 | if len(filename) <= max_length: | ||
| 8 | return filename | ||
| 9 | file_extension = filename.split('.')[-1] | ||
| 10 | return f"{filename[:max_length - len(file_extension) - 5]}....{file_extension}" | ||
| 11 | |||
| 12 | response = requests.get(url) | ||
| 13 | if response.status_code == 200: | ||
| 14 | xml_data = response.text | ||
| 15 | root = ET.fromstring(xml_data) | ||
| 16 | |||
| 17 | # Handle namespace | ||
| 18 | ns = {'s3': 'http://s3.amazonaws.com/doc/2006-03-01/'} | ||
| 19 | |||
| 20 | # Create an empty dictionary to hold the tree structure | ||
| 21 | tree = {} | ||
| 22 | |||
| 23 | for content in root.findall(".//s3:Contents", ns): | ||
| 24 | key = content.find("s3:Key", ns).text | ||
| 25 | parts = key.split("/") | ||
| 26 | node = tree | ||
| 27 | for part in parts: | ||
| 28 | if part: | ||
| 29 | node = node.setdefault(part, {}) | ||
| 30 | |||
| 31 | # Function to convert the tree structure to markdown list | ||
| 32 | def tree_to_md(tree, indent=0, path=""): | ||
| 33 | md = "" | ||
| 34 | for k, v in tree.items(): | ||
| 35 | if v: # If the node has children, it's a directory | ||
| 36 | md += " " * indent + f"- {k}\n" | ||
| 37 | md += tree_to_md(v, indent + 1, path=f"{path}{k}/") | ||
| 38 | else: # If the node is empty, it's a file | ||
| 39 | file_url = f"{url}{path}{k}" | ||
| 40 | file_name = truncate_filename(k, 80) | ||
| 41 | md += " " * indent + f"- [{file_name}](<{file_url}>)\n" | ||
| 42 | return md | ||
| 43 | |||
| 44 | md = tree_to_md(tree) | ||
| 45 | |||
| 46 | with open("templates/vault.md", "r") as fp: | ||
| 47 | content = fp.read() | ||
| 48 | |||
| 49 | new_content = content.replace("{CONTENT}", md) | ||
| 50 | |||
| 51 | with open("content/pages/vault.md", "w") as fp: | ||
| 52 | fp.write(new_content) | ||
| 53 | else: | ||
| 54 | print(f"Failed to fetch XML data. Status code: {response.status_code}") | ||
