aboutsummaryrefslogtreecommitdiff
path: root/vault.py
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2023-11-01 22:54:27 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2023-11-01 22:54:27 +0100
commit2417a6b7603524dc5cd30d29b153f91024b9443d (patch)
tree9be5ea8e5baba96dd9159217da6badf6157fb595 /vault.py
parent89ba3497f07a8ea43d209b583f39fcc286acc923 (diff)
downloadmitjafelicijan.com-2417a6b7603524dc5cd30d29b153f91024b9443d.tar.gz
Move to Jekyll
Diffstat (limited to 'vault.py')
-rw-r--r--vault.py54
1 files changed, 0 insertions, 54 deletions
diff --git a/vault.py b/vault.py
deleted file mode 100644
index 5610c87..0000000
--- a/vault.py
+++ /dev/null
@@ -1,54 +0,0 @@
1import xml.etree.ElementTree as ET
2import requests
3
4url = "https://mitjafelicijan.fra1.digitaloceanspaces.com/"
5
6def 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
12response = requests.get(url)
13if 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, 300)
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)
53else:
54 print(f"Failed to fetch XML data. Status code: {response.status_code}")