|
@@ -1,28 +1,80 @@
|
|
|
+from pathlib import Path
|
|
|
+
|
|
|
from O365 import Account
|
|
|
+from O365.drive import Folder
|
|
|
+
|
|
|
+CLIENT_ID = "925f74dc-f96a-4718-9ca7-d6cc3fa43e1e"
|
|
|
+CLIENT_SECRET = "SS~8Q~QpBZV9toZuwkzW1XGGen2Hn833spNMtdq5"
|
|
|
+FLOW_TYPE = "credentials"
|
|
|
+TENANT_ID = "2ad0dff5-07ce-4cc2-a852-99ce8b91c218"
|
|
|
+
|
|
|
+USER_ID = "bedner@global-cube.net"
|
|
|
+# password = "Platinum512!!"
|
|
|
+
|
|
|
+
|
|
|
+def get_remote_files(folder: Folder, prefix=""):
|
|
|
+ res = {}
|
|
|
+ for item in folder.get_items():
|
|
|
+ name = item.name
|
|
|
+ if prefix != "":
|
|
|
+ name = prefix + "\\" + name
|
|
|
+
|
|
|
+ if item.is_file:
|
|
|
+ res[name] = item.object_id
|
|
|
+ if item.is_folder:
|
|
|
+ res.update(get_remote_files(item, name))
|
|
|
+ return res
|
|
|
+
|
|
|
+
|
|
|
+def get_or_create_subfolder(remote_folder: Folder, folder: str):
|
|
|
+ if folder == "":
|
|
|
+ return remote_folder
|
|
|
+
|
|
|
+ if "\\" in folder:
|
|
|
+ folder, subfolder = folder.split("\\", 1)
|
|
|
+ else:
|
|
|
+ subfolder = ""
|
|
|
+
|
|
|
+ for f in remote_folder.get_child_folders():
|
|
|
+ if f.name == folder:
|
|
|
+ return get_or_create_subfolder(f, subfolder)
|
|
|
+ new_folder = remote_folder.create_child_folder(folder)
|
|
|
+ return get_or_create_subfolder(new_folder, subfolder)
|
|
|
+
|
|
|
+
|
|
|
+def main():
|
|
|
+ account = Account((CLIENT_ID, CLIENT_SECRET), auth_flow_type=FLOW_TYPE, tenant_id=TENANT_ID)
|
|
|
+ account.authenticate()
|
|
|
+
|
|
|
+ storage = account.storage(resource=USER_ID)
|
|
|
+ my_drive = storage.get_default_drive()
|
|
|
+ remote_folder = my_drive.get_item_by_path("/Keyloop_Datentransfer")
|
|
|
|
|
|
+ # folder.create_child_folder
|
|
|
+ local_folder = Path("datev/export")
|
|
|
|
|
|
-client_id = "925f74dc-f96a-4718-9ca7-d6cc3fa43e1e"
|
|
|
-client_secret = "SS~8Q~QpBZV9toZuwkzW1XGGen2Hn833spNMtdq5"
|
|
|
-flow_type = "credentials"
|
|
|
+ local_files = set([str(p.relative_to(local_folder)) for p in local_folder.rglob("*") if p.is_file()])
|
|
|
+ # uploaded_file = remote_folder.upload_file(item="tox.ini")
|
|
|
|
|
|
-user_id = "bedner@global-cube.net"
|
|
|
-password = "Platinum512!!"
|
|
|
+ remote_files_dict = get_remote_files(remote_folder)
|
|
|
+ remote_files = set(remote_files_dict.keys())
|
|
|
|
|
|
-account = Account(
|
|
|
- (client_id, client_secret), auth_flow_type="credentials", tenant_id="2ad0dff5-07ce-4cc2-a852-99ce8b91c218"
|
|
|
-)
|
|
|
+ delete_candidates = remote_files.difference(local_files)
|
|
|
+ create_candidates = local_files.difference(remote_files)
|
|
|
|
|
|
-account.authenticate()
|
|
|
+ for filename in delete_candidates:
|
|
|
+ print(filename)
|
|
|
+ my_drive.get_item(remote_files_dict[filename]).delete()
|
|
|
|
|
|
-storage = account.storage(resource=user_id)
|
|
|
-my_drive = storage.get_default_drive()
|
|
|
-folder = my_drive.get_item_by_path("/Keyloop_Datentransfer")
|
|
|
-archive = my_drive.get_item_by_path("/Keyloop_Datentransfer/Archiv/2023")
|
|
|
-print(archive.name)
|
|
|
+ for filename in sorted(create_candidates):
|
|
|
+ print(filename)
|
|
|
+ full_filename = local_folder / filename
|
|
|
+ subfolder = ""
|
|
|
+ if "\\" in filename:
|
|
|
+ subfolder, filename = filename.rsplit("\\", 1)
|
|
|
+ remote_subfolder = get_or_create_subfolder(remote_folder, subfolder)
|
|
|
+ remote_subfolder.upload_file(item=full_filename)
|
|
|
|
|
|
-# folder.create_child_folder
|
|
|
-uploaded_file = folder.upload_file(item="tox.ini")
|
|
|
|
|
|
-for item in folder.get_items():
|
|
|
- print(item.name)
|
|
|
- print("--> ", item.object_id)
|
|
|
+if __name__ == "__main__":
|
|
|
+ main()
|