-
Notifications
You must be signed in to change notification settings - Fork 0
/
nextcloud.py
50 lines (39 loc) · 1.55 KB
/
nextcloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import pyrclone
import tempfile
import os
def create_rclone_config(nextcloud_url, nextcloud_user, nextcloud_pass):
# Create a temporary rclone configuration file
with tempfile.NamedTemporaryFile(mode="w", delete=False) as config_file:
config = f"""
[nextcloud]
type = webdav
url = {nextcloud_url}
vendor = nextcloud
user = {nextcloud_user}
pass = {nextcloud_pass}
"""
config_file.write(config)
return config_file.name
def sync_to_nextcloud(local_folder, remote_folder):
# Get Nextcloud username and password from the .env file
nc_username = os.environ['NEXTCLOUD_USER']
nc_password = os.environ['NEXTCLOUD_PASS']
nc_url = os.environ['NEXTCLOUD_URL']
# Generate a temporary Rclone configuration file
rclone_config_path = create_rclone_config(nc_url, nc_username, nc_password)
try:
# Set the RCLONE_CONFIG environment variable to the temporary config file
os.environ["RCLONE_CONFIG"] = rclone_config_path
# Initialize a connection to Rclone
rclone = pyrclone.Rclone()
# Synchronize the local folder with the remote folder
result = rclone.sync(local_folder, f"nextcloud:{remote_folder}")
print(result)
finally:
# Delete the temporary configuration file
if os.path.exists(rclone_config_path):
os.remove(rclone_config_path)
# Example usage
local_folder = "markdown_files"
remote_folder = ".Notes/Current"
sync_to_nextcloud(local_folder, remote_folder)