1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- 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")
- 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")
- remote_files_dict = get_remote_files(remote_folder)
- remote_files = set(remote_files_dict.keys())
- delete_candidates = remote_files.difference(local_files)
- create_candidates = local_files.difference(remote_files)
- for filename in delete_candidates:
- print(filename)
- my_drive.get_item(remote_files_dict[filename]).delete()
- 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)
- if __name__ == "__main__":
- main()
|