<filename>nextcloud-upload.py
#!/usr/local/bin/python3
#coding=utf-8

# requirements: pip3 install pyocclient

# nextcloud settings
# use app token and not your 'real' credentials!
nextcloud_address = 'https://nxXYZ1234.your-next.cloud'
nextcloud_username = 'your-user.name'
nextcloud_password = '<PASSWORD>'
nextcloud_folder = 'NextDropr'


import sys,os,time,owncloud

# a very primitive debugging method..
def microLog(msg):
	thisisjust='ignoreme'
	#print(msg)
	#os.system("say "+msg)

try:
	oc = owncloud.Client(nextcloud_address)
	oc.login(nextcloud_username,nextcloud_password)
except Exception as e:
	microLog(e)
	sys.exit(1)


if len(sys.argv) == 1:
	microLog('no file found')
	sys.exit(1)

if len(sys.argv) > 2:
	microLog('only 1 file is supported, not %i'%len(sys.argv))
	microLog(sys.argv[1])
	sys.exit(1)

local_file = sys.argv[1]
filename = os.path.basename(local_file)
remote_file = nextcloud_folder+"/"+filename


def createBackupDir(directory):
	try:
		oc.mkdir(directory)
		return True
	except Exception as e:
		error_msg = "failed to create directory '%s' on nextcloud server. error: %s"%(directory,e)
		microLog(error_msg)
		sys.exit(1)



def is_dir_existing(directory):
	microLog("checking if directory '%s' exists on nextcloud.."%directory)
	try:
		dirlist = oc.list(directory)
		return True
	except Exception as e:
		if e == 'HTTP error: 404':
			microLog("directory '%s' does not exist on nextcloud, need to create it")
		return False



microLog("checking '%s' (nextcloud_folder) directory on server.."%nextcloud_folder)
if not is_dir_existing(nextcloud_folder):
	createBackupDir(nextcloud_folder)


microLog("uploading backup file to nexcloud now ..")
try:
	oc.put_file(remote_file,local_file)
except Exception as e:
	error_msg = "failed to upload file from '%s' (local) to '%s' (nextcloud), because: %s"%(local_file,remote_file,e)
	microLog(error_msg)
	sys.exit(1)


link_info = oc.share_file_with_link(remote_file)
print(link_info.get_link()+"/preview")

sys.exit(0)

<filename>nextcloud-upload.py
#!/usr/local/bin/python3
#coding=utf-8

# requirements: pip3 install pyocclient

# nextcloud settings
# use app token and not your 'real' credentials!
nextcloud_address = 'https://nxXYZ1234.your-next.cloud'
nextcloud_username = 'your-user.name'
nextcloud_password = '<PASSWORD>'
nextcloud_folder = 'NextDropr'


import sys,os,time,owncloud

# a very primitive debugging method..
def microLog(msg):
	thisisjust='ignoreme'
	#print(msg)
	#os.system("say "+msg)

try:
	oc = owncloud.Client(nextcloud_address)
	oc.login(nextcloud_username,nextcloud_password)
except Exception as e:
	microLog(e)
	sys.exit(1)


if len(sys.argv) == 1:
	microLog('no file found')
	sys.exit(1)

if len(sys.argv) > 2:
	microLog('only 1 file is supported, not %i'%len(sys.argv))
	microLog(sys.argv[1])
	sys.exit(1)

local_file = sys.argv[1]
filename = os.path.basename(local_file)
remote_file = nextcloud_folder+"/"+filename


def createBackupDir(directory):
	try:
		oc.mkdir(directory)
		return True
	except Exception as e:
		error_msg = "failed to create directory '%s' on nextcloud server. error: %s"%(directory,e)
		microLog(error_msg)
		sys.exit(1)



def is_dir_existing(directory):
	microLog("checking if directory '%s' exists on nextcloud.."%directory)
	try:
		dirlist = oc.list(directory)
		return True
	except Exception as e:
		if e == 'HTTP error: 404':
			microLog("directory '%s' does not exist on nextcloud, need to create it")
		return False



microLog("checking '%s' (nextcloud_folder) directory on server.."%nextcloud_folder)
if not is_dir_existing(nextcloud_folder):
	createBackupDir(nextcloud_folder)


microLog("uploading backup file to nexcloud now ..")
try:
	oc.put_file(remote_file,local_file)
except Exception as e:
	error_msg = "failed to upload file from '%s' (local) to '%s' (nextcloud), because: %s"%(local_file,remote_file,e)
	microLog(error_msg)
	sys.exit(1)


link_info = oc.share_file_with_link(remote_file)
print(link_info.get_link()+"/preview")

sys.exit(0)

#!/usr/local/bin/python3 #coding=utf-8 # requirements: pip3 install pyocclient # nextcloud settings # use app token and not your 'real' credentials! # a very primitive debugging method.. #print(msg) #os.system("say "+msg)