2020-04-25 15:52:51 -05:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
from datetime import datetime as dt
|
|
|
|
from lib.setup import import_config_file
|
|
|
|
from lib.authentication import authenticate_nextcloud
|
|
|
|
|
|
|
|
|
2020-04-29 13:16:35 -05:00
|
|
|
def setup_archive_data(config, timestamp):
|
|
|
|
if(config['webdav_hostname'] and config['webdav_login'] and config['webdav_password']):
|
|
|
|
client = authenticate_nextcloud(config)
|
2020-04-25 15:52:51 -05:00
|
|
|
else:
|
2020-04-25 15:54:50 -05:00
|
|
|
print('Nextcloud WebDAV information missing/incomplete, skipping...')
|
|
|
|
return
|
2020-04-25 15:52:51 -05:00
|
|
|
ts = dt.fromtimestamp(int(int(timestamp)/1000))
|
|
|
|
date_ts = ts.strftime('%Y_%m_%d')
|
|
|
|
time_ts = ts.strftime('%H_%M_%S')
|
2020-04-29 13:16:35 -05:00
|
|
|
return client, date_ts, time_ts
|
2020-04-25 15:52:51 -05:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-04-29 13:16:35 -05:00
|
|
|
def nextcloud_upload_media(config, upload_path, archive_filename, timestamp):
|
|
|
|
client, date_ts, time_ts = setup_archive_data(config, timestamp)
|
2020-04-25 15:52:51 -05:00
|
|
|
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}')
|