49 lines
1.9 KiB
Python
49 lines
1.9 KiB
Python
![]() |
#!/usr/bin/python3
|
||
|
|
||
|
from datetime import datetime as dt
|
||
|
from lib.setup import import_config_file
|
||
|
from lib.authentication import authenticate_nextcloud
|
||
|
|
||
|
|
||
|
def setup_archive_data(timestamp):
|
||
|
config = import_config_file()
|
||
|
if(config['nextcloud']['webdav_hostname'] and config['nextcloud']['webdav_login'] and config['nextcloud']['webdav_password']):
|
||
|
client = authenticate_nextcloud(config['nextcloud'])
|
||
|
else:
|
||
|
print(
|
||
|
'Please fill out the necessary information for using Nextcloud through WebDAV.')
|
||
|
print('\thttps://docs.nextcloud.com/server/16/user_manual/files/access_webdav.html')
|
||
|
exit(1)
|
||
|
ts = dt.fromtimestamp(int(int(timestamp)/1000))
|
||
|
date_ts = ts.strftime('%Y_%m_%d')
|
||
|
time_ts = ts.strftime('%H_%M_%S')
|
||
|
return client, config['nextcloud_upload_path'], date_ts, time_ts
|
||
|
|
||
|
|
||
|
def setup_archive_dir(client, upload_path, date_ts):
|
||
|
if(not client.check(upload_path)):
|
||
|
client.mkdir(f'{upload_path}')
|
||
|
client.mkdir(f'{upload_path}/{date_ts}')
|
||
|
else:
|
||
|
if(not client.check(f'{upload_path}/{date_ts}')):
|
||
|
client.mkdir(f'{upload_path}/{date_ts}')
|
||
|
|
||
|
|
||
|
def attempt_upload_media(client, upload_dir, upload_filename, upload_filetype, archive_filename):
|
||
|
file_dne = True
|
||
|
count = 1
|
||
|
while(file_dne):
|
||
|
temp_filename = f'{upload_dir}/{upload_filename}_{count}.{upload_filetype}'
|
||
|
if(not client.check(temp_filename)):
|
||
|
client.upload_file(temp_filename, f'{archive_filename}')
|
||
|
file_dne = False
|
||
|
else:
|
||
|
count += 1
|
||
|
|
||
|
|
||
|
def nextcloud_upload_media(archive_filename, timestamp):
|
||
|
client, upload_path, date_ts, time_ts = setup_archive_data(timestamp)
|
||
|
setup_archive_dir(client, upload_path, date_ts)
|
||
|
attempt_upload_media(client, f'{upload_path}/{date_ts}',
|
||
|
f'{date_ts}_{time_ts}', f'{archive_filename.split(".")[1]}', f'data/{archive_filename}')
|